stm32学习记录随笔23.11.5

发布时间 2023-11-06 00:08:10作者: 上够了逼班的老王

按键控制LED灯递增递减,随笔实验视频记录B站:BV1JN4y1r7Fu

main.c

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "LED.h"
#include "KEY.h"

//按键控制LED灯递增递减
int main(void)
{
    int8_t i=0,KeyNum;
    LED_Init();
    Key_Init();
    while(1)
        {
            KeyNum=Key_GetNum();
            if(KeyNum==1)
            {
                i++;
                if(i>8){i=8;}
                LED_On(i-1);
            }
            if(KeyNum==2)
            {
                i--;
                if(i<0){i=0;}
                LED_Off(i);
            }
        }
}

LED.c

#include "stm32f10x.h"                  // Device header
/*
*brief    简  介:初始化LED灯
*param    参  数:无
*retval   返回值:无
*/
void LED_Init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//启用系统寄存器时钟,使能GPIOC组,并启动
    GPIO_InitTypeDef GPIO_InitStructure;    //初始化GPIO_Init后需要定义一个结构体配置信息,根据stm32f10x_gpio.c文件要求需要定义
    /*
    以下三项定义与stm32f10x_gpio.h文件中选择
    Mode模式Pin引脚Speed速度即频率
    */
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;//OUT OD开漏输出
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    

    GPIO_Init(GPIOA,&GPIO_InitStructure);//初始化结构体配置信息,并指向上4列代码配置信息
    GPIO_SetBits(GPIOA,GPIO_Pin_All);//设置高电平,默认熄灭LED
}
/*
*brief    简  介:打开开关LED低电平
*param    参  数:输入0-7直接的数
*retval   返回值:无
*/
void LED_On(int8_t i)
{
    if(i<0 || i>7)
    {
        i=0;
    }
    GPIO_ResetBits(GPIOA,(uint16_t)0x0001<<i);//设置低电平
}
/*
*brief    简  介:关闭LED开关高电平
*param    参  数:输入0-7直接的数
*retval   返回值:无
*/
void LED_Off(int8_t i)
{
    if(i<0 || i>7)
    {
        i=0;
    }
    GPIO_SetBits(GPIOA,(uint16_t)0x0001<<i);//设置高电平        
}
/*
*brief    简  介:获取引脚电平信号,控制当前LED状态取反
*param    参  数:输入0-7直接的数
*retval   返回值:无
*/
void LED_Turn(int8_t i)
{
    if(i<0 || i>7)
    {
        i=0;
    }
    if(GPIO_ReadOutputDataBit(GPIOA,(uint16_t)0x0001<<i)==0)
    {
        GPIO_SetBits(GPIOA,(uint16_t)0x0001<<i);//设置高电平
    }
    else
    {
        GPIO_ResetBits(GPIOA,(uint16_t)0x0001<<i);//设置低电平
    }
}
KEY.c

#include "stm32f10x.h"                  // Device header
#include "Delay.h"

void Key_Init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//启用系统寄存器时钟,使能GPIOC组,并启动
    GPIO_InitTypeDef GPIO_InitStructure;    //初始化GPIO_Init后需要定义一个结构体配置信息,根据stm32f10x_gpio.c文件要求需要定义
    /*
    以下三项定义与stm32f10x_gpio.h文件中选择
    Mode模式Pin引脚Speed速度即频率
    */
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入模式
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    

    GPIO_Init(GPIOC,&GPIO_InitStructure);//初始化结构体配置信息,并指向上4列代码配置信息
}

uint8_t Key_GetNum(void)
{
    uint8_t KeyNum=0;
    if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13)==0)
    {
        Delay_ms(20);
        while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13)==0);
        Delay_ms(20);
        KeyNum=1;    
    }
    if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_14)==0)
    {
        Delay_ms(20);
        while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_14)==0);
        Delay_ms(20);
        KeyNum=2;    
    }
    return KeyNum;
}