备战蓝桥杯之嵌入式——BUTTON按键实验

2019-07-13 01:13发布

这次要实现的功能是LCD显示我按下的是哪一个键。需要注意一下几个点:1:配置Button的时候要注意是输入模式,因此不需要配置频率。2:配置成上拉输入(IPU)还是下拉输入(IPD)要看硬件连接,按键没按下时检测到高电平则为上拉输入,按键没按下前检测到低电平则为下拉输入3:#define xxxx xx语句后不能加;,否则出现错误“expect expression”4:LCD显示程序中,要想覆盖掉某一段文字,可用空格,如LCD_ShowString(30,150,200,16,16,"KEY0 IS PRESSED     );这里贴一段正点原子的LCD在主函数里的通用写法,方便以后随时调用:#include "delay.h" #include "sys.h" #include "usart.h" #include "lcd.h" int main(void) { u8lcd_id[12]; //存放LCD ID字符串 delay_init(); //延时函数初始化 uart_init(9600); //串口初始化为9600 LCD_Init(); POINT_COLOR=RED; sprintf((char*)lcd_id,"LCDID:%04X",lcddev.id);//将LCD ID打印到lcd_id数组。 POINT_COLOR=RED; LCD_ShowString(30,40,200,24,24,"MiniSTM32 ^_^"); LCD_ShowString(30,70,200,16,16,"TFTLCDTEST"); LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK"); LCD_ShowString(30,110,200,16,16,lcd_id); //显示LCD ID LCD_ShowString(30,130,200,12,12,"2014/3/7"); } 再贴一段switch语句的格式,自己老是忘记: Switch(x) { Case0: LCD_ShowString(30,40,200,24,24,"Mini STM32 ^_^"); break; Case1: LCD_ShowString(30,40,200,24,24,"Mini STM32 ^_^"); break; **** **** } 最后贴出主函数: #include "delay.h" #include "sys.h" #include "usart.h" #include "lcd.h" #include "button.h" #include "led.h" int main(void) { u8temp; u32i=0,j; u8lcd_id[12]; //存放LCD ID字符串 delay_init(); //延时函数初始化 uart_init(9600); //串口初始化为9600 LCD_Init(); Button_Init(); LED_Init(); POINT_COLOR=RED; sprintf((char*)lcd_id,"LCDID:%04X",lcddev.id);//将LCD ID打印到lcd_id数组。 POINT_COLOR=RED; LCD_ShowString(30,40,200,24,24,"MiniSTM32 ^_^"); LCD_ShowString(30,70,200,16,16,"TFTLCDTEST"); LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK"); LCD_ShowString(30,110,200,16,16,lcd_id); //显示LCD ID LCD_ShowString(30,130,200,12,12,"2016/2/8HELLO Monkey Year"); while(1) { i++; temp=Button_Scan(0); switch(temp) { //case0:LCD_ShowString(30,150,200,16,16,"NO KEY IS PRESSED"); break; case1:LCD_ShowString(30,150,200,16,16,"KEY0 IS PRESSED "); break; case2:LCD_ShowString(30,150,200,16,16,"KEY1 IS PRESSED "); break; case3:LCD_ShowString(30,150,200,16,16,"WK_UP IS PRESSED "); break; } if(i>1000000) { if(j%2) GPIO_SetBits(GPIOA,GPIO_Pin_8); elseGPIO_ResetBits(GPIOA,GPIO_Pin_8); j++; i=0; } } }
主函数中我还加入了LED显示,这里需要注意的是LED闪烁不能用延时函数。BTW如果你的光标突然变成了黑 {MOD}小方块,按一下insert(0)即可切换。