LED的配置那篇博客里已经做了详细的说明,这里就不过多陈述了,不懂的话可以去看看我的那篇博客,嘻嘻~
初始化部分写完之后,再加上51按键部分的底层就可以了(下面给出完整的代码):
- #include "stm32f10x.h"
- #include "key.h"
- u8 KeySta[4] = {1, 1, 1, 1};//存放按键的当前状态
- extern void KeyAction(u8 keycode);
- void KeyInit(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);//
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_8;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- void KeyScan()
- {
- u8 i;
- static u8 keybuff[4] = {0xFF, 0xFF, 0xFF, 0xFF};//注意这里的静态变量
-
- keybuff[0] = (keybuff[0] << 1) | KEY1;
- keybuff[1] = (keybuff[1] << 1) | KEY2;
- keybuff[2] = (keybuff[2] << 1) | KEY3;
- keybuff[3] = (keybuff[3] << 1) | KEY4;
-
- for(i = 0; i < 4; i++)
- {
- if(keybuff[i] == 0x00)
- {
- KeySta[i] = 0;
- }
- else if(keybuff[i] == 0xFF)
- {
- KeySta[i] = 1;
- }
- else
- {}
- }
- }
- void KeyDriver()
- {
- u8 i;
- static u8 backup[4] = {1, 1, 1, 1};
-
- for(i = 0; i < 4; i++)
- {
- if(KeySta[i] != backup[i])
- {
- if(backup[i] == 1)
- {
- KeyAction(i+1);
- }
- backup[i] = KeySta[i];
- }
- }
- }
|