STM32 with keypad control LED blinking

Under the new project HARDWARE folder, create a new folder and key folder led, respectively, and the new led.c key.c saved in the appropriate folder.
Here Insert Picture Description
Then save the two new texts into led.h and key.h saved and led key folder.
keil5 added .c file, add a respective header (
.h files)
to start programming
LED section:

led.h inside the core code.

#define LED0 PAout(6)	// DS0,定义LED0的IO口,PAout(6)指 GPIOA_Pin_6 输出模式
#define LED1 PAout(7)	// DS1	 

void LED_Init(void);//初始化	

Initialization led IO ports:
a lot of peripherals are GPIO initialization similar to the following, such as setting the GPIO pin, mode, speed, otype, the drop-down

void LED_Init(void)
{    	 
  GPIO_InitTypeDef  GPIO_InitStructure;

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能GPIOA的时钟,首要操作!!
  
 //GPIOA6,A7的初始化
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;//位置
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//模式,这里是输出模式
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//输出模式,这里是推挽输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//选择上拉
  GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIO
	
	GPIO_ResetBits(GPIOA,GPIO_Pin_6| GPIO_Pin_7);//设置初始状态,这里是setbits,设置低电平,即灯亮。

}

KEY part:
key.h core code:

#define key0 PEin(4)     //定义IO口
#define key1 PEin(3) 
void key_init(void);     //初始化按键
void KEY_Scan(void);	 //初始化按键扫描函数

初始化按键IO 口的固定模式。

oid key_init(void)
	
{
	GPIO_InitTypeDef  GPIO_InitStructure;

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4| GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//注意按键是输入模式
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOE, &GPIO_InitStructure);
	
	

Very simple one button scan function, STM32 key also provides two modes, double-click support and do not support double-click the selected mode I have not get to know.

void KEY_Scan(void)
 {
		if(key0 == 0)
		{
				delay_ms(5);//消抖
				if(key0==0)
				{
					 LED0 =~LED0;
				   LED1 =~LED1;
				}
		}
 }

The main function parts:

int main(void)
{ 
 
	delay_init(168);		
	LED_Init();	
    key_init();	
	while(1)
	{
     	KEY_Scan();
    }

This article omitted declare some header files, readers add their own it!

Released two original articles · won praise 0 · Views 87

Guess you like

Origin blog.csdn.net/lllllllllljg/article/details/104037170