你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32G431RBT6模块总结之LED、LCD、KEY

[复制链接]
STMCU小助手 发布时间:2023-3-2 13:47
LED、LCD、KEY模块为必考模块
考题总结

bb16a124c0724a3b8598710e236b9bc0.png

一、LCD模块

1.把Inc和Src文件中的几个.h文件和.c文件复制到相应文件夹里

8cf4cea493da4003a6d177ef99066a7a.png

18b5898a760b4f1986dff78eae57549f.png

2.由产品手册可知LCD模块和LED模块公用PC8-PC15引脚,为了避免引起冲突,需要对以下三个函数做一下修改:

e88ce51112ab449baadbb52fbb0f9c75.png

ce6a2c25017e4c028ee5adf4ee6443df.png

3.LCD高亮显示
  1. //LCD processing function
  2. void LCD_Proc(void)
  3. {                       
  4.         if(Show_Flag == 1){
  5.                 LCD_SetBackColor(Black);
  6.                 LCD_SetTextColor(White);       
  7.                 LCD_DisplayStringLine(Line1, (uint8_t *)"        Data");
  8.                 LCD_DisplayStringLine(Line3, (uint8_t *)Show_CNBR);
  9.                 LCD_DisplayStringLine(Line5, (uint8_t *)Show_VNBR);
  10.                 LCD_DisplayStringLine(Line7, (uint8_t *)Show_IDLE);
  11.         }else if(Show_Flag == 2){
  12.                 if(HiLi_Flag == 0){
  13.                         LCD_SetBackColor(Black);
  14.                         LCD_SetTextColor(White);               
  15.                         LCD_DisplayStringLine(Line1, (uint8_t *)"        Para");
  16.                         LCD_DisplayStringLine(Line3, (uint8_t *)Show_CNBR_Price);
  17.                         LCD_DisplayStringLine(Line5, (uint8_t *)Show_VNBR_Price);       
  18.                         LCD_DisplayStringLine(Line7, (uint8_t *)"                    ");                               
  19.                 }else if(HiLi_Flag == 1){//
  20.                         LCD_SetBackColor(White);
  21.                         LCD_SetTextColor(Black);
  22.                         LCD_DisplayChar(Line3,(320-(16*10)),Show_CNBR_Price[10]);
  23.                         LCD_DisplayChar(Line3,(320-(16*11)),Show_CNBR_Price[11]);
  24.                         LCD_DisplayChar(Line3,(320-(16*12)),Show_CNBR_Price[12]);
  25.                         LCD_DisplayChar(Line3,(320-(16*13)),Show_CNBR_Price[13]);
  26.                         if(C_Dot_Flag == 1)//做了一个位数的变化,比如3.50变成10.50,判断一下最后一位要不要高亮,如果不要就用空格来代替
  27.                                 LCD_DisplayChar(Line3,(320-(16*14)),Show_CNBR_Price[14]);               
  28.                         else{
  29.                                 LCD_SetBackColor(Black);
  30.                                 LCD_SetTextColor(White);
  31.                                 LCD_DisplayChar(Line3,(320-(16*14)),' ');               
  32.                         }                               
  33.                         LCD_SetBackColor(Black);
  34.                         LCD_SetTextColor(White);
  35.                         LCD_DisplayStringLine(Line5, (uint8_t *)Show_VNBR_Price);       
  36.                 }else if(HiLi_Flag == 2){
  37.                         LCD_SetBackColor(Black);
  38.                         LCD_SetTextColor(White);                       
  39.                         LCD_DisplayStringLine(Line3, (uint8_t *)Show_CNBR_Price);
  40.                        
  41.                         LCD_SetBackColor(White);
  42.                         LCD_SetTextColor(Black);                       
  43.                         LCD_DisplayChar(Line5,(320-(16*10)),Show_VNBR_Price[10]);
  44.                         LCD_DisplayChar(Line5,(320-(16*11)),Show_VNBR_Price[11]);
  45.                         LCD_DisplayChar(Line5,(320-(16*12)),Show_VNBR_Price[12]);
  46.                         LCD_DisplayChar(Line5,(320-(16*13)),Show_VNBR_Price[13]);
  47.                         if(V_Dot_Flag == 1)
  48.                                 LCD_DisplayChar(Line5,(320-(16*14)),Show_VNBR_Price[14]);       
  49.       else{
  50.                                 LCD_SetBackColor(Black);
  51.                                 LCD_SetTextColor(White);
  52.                                 LCD_DisplayChar(Line5,(320-(16*14)),' ');               
  53.                         }                       
  54.                 }
  55.         }
  56. }
复制代码

二、LED模块
1.led.h文件用模板就可以

efb123c0baa642eb9267a110dd5ec39c.png

  1. #include "stm32g4xx_hal.h"

  2. #define ON        1
  3. #define OFF        0

  4. #define LED1                        GPIO_PIN_8
  5. #define LED2            GPIO_PIN_9
  6. #define LED3            GPIO_PIN_10
  7. #define LED4            GPIO_PIN_11
  8. #define LED5            GPIO_PIN_12
  9. #define LED6            GPIO_PIN_13
  10. #define LED7            GPIO_PIN_14
  11. #define LED8            GPIO_PIN_15
  12. #define LEDALL                GPIO_PIN_All

  13. void LED_Ctrl(uint16_t LED, uint8_t LED_Status);
复制代码

2.led.c文件----直接用模板,闪烁、亮灭都用那一个函数

如果要闪烁的话:灯亮了之后加一个HAL_DLAY()函数就行

9d2a4502e2a8475c90b90871ef628b3a.png

  1. #include "led.h"

  2. void LED_Ctrl(uint16_t LED, uint8_t LED_Status)
  3. {
  4.         if(LED_Status == ON){ //ÁÁµÆ
  5.                 HAL_GPIO_WritePin(GPIOC,LED,GPIO_PIN_RESET);
  6.     HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
  7.     HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
  8.   }        
  9.   else{ //ÃðµÆ
  10.                 HAL_GPIO_WritePin(GPIOC,LED,GPIO_PIN_SET);
  11.     HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
  12.     HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
  13.   }
  14. }
复制代码

三、KEY模块
1.key.h-----用模板(不管是中断的方式还是while循环的方式查找是否有按键,都统一了)



  1. #include "stm32g4xx_hal.h"

  2. #define Press(x)  (HAL_GPIO_ReadPin(x))

  3. #define KB1 GPIOB, GPIO_PIN_0
  4. #define KB2 GPIOB, GPIO_PIN_1
  5. #define KB3 GPIOB, GPIO_PIN_2
  6. #define KB4 GPIOA, GPIO_PIN_0

  7. uint8_t Key_Scan(void);
复制代码

2.key.c这个文件只保留了短按

f19fe7f22a9e4740a79c872a74078ba7.png

  1. #include "key.h"

  2. uint8_t Key_Scan(void)
  3. {
  4.         char key_value = 0;

  5.         if(Press(KB1) == 0)        {        //key1
  6.                 HAL_Delay(10); //È¥¶¶
  7.                 if(Press(KB1) == 0){
  8.                         key_value = 1;
  9.                         while(Press(KB1) == 0);        //°´¼üËÉ¿ªÐ§¹û               
  10.                 }
  11.         }else        if(Press(KB2) == 0)        { //key2
  12.                 HAL_Delay(10);
  13.                 if(Press(KB2) == 0){
  14.                         key_value = 2;
  15.                         while(Press(KB2) == 0);       
  16.                 }
  17.         }
  18.         else        if(Press(KB3) == 0)        {        //key3
  19.                 HAL_Delay(10);
  20.                 if(Press(KB3) == 0){
  21.                         key_value = 3;
  22.                         while(Press(KB3) == 0);               
  23.                 }
  24.         }
  25.         else        if(Press(KB4) == 0)        {        //key4
  26.                 HAL_Delay(10);
  27.                 if(Press(KB4) == 0){
  28.                         key_value = 4;
  29.                         while(Press(KB4) == 0);                                               
  30.                 }
  31.         }

  32.         return key_value;
  33. }
复制代码

四、KEY.it模块---按键通过中断的方式实现
1.key_it.h文件

c9bdd3b4e7204b4ba31a385a6d13912f.png

  1. #include "stm32g4xx_hal.h"

  2. #define Press(x)  (HAL_GPIO_ReadPin(x))
  3. #define KB4 GPIOA, GPIO_PIN_0
  4. #define KB1 GPIOB, GPIO_PIN_0
  5. #define KB2 GPIOB, GPIO_PIN_1
  6. #define KB3 GPIOB, GPIO_PIN_2

  7. //¿ÉÐÞ¸Ä
  8. #define     SHORT_DELAY             2                  // ¶Ì°´ÑÓʱ
  9. #define     LONG_DELAY              80                // ³¤°´ÑÓʱ
  10. #define     CONTINUE_DELAY          10                // Á¬Ðø´¥·¢ÑÓʱ

  11. typedef struct {
  12.   uint8_t scan_flag;  //°´¼ü¶¨Ê±É¨Ãè±êʶ
  13.   uint16_t last_key;  //Éϴΰ´¼üÖµ  
  14.         uint8_t pressed;                //°´¼üÊÇ·ñÒѰ´Ï±êʶ
  15.         uint16_t cnt;       //°´¼ü°´ÏÂʱ¼ä³¤¶Ì¼ÆÊ±Æ÷
  16. }KeyScan_t;

  17. uint8_t Key_Scan(void);
复制代码

2.key_it.c文件

d188ac7c7bab4482a94dc7f88b46aabd.png

  1. #include "key_it.h"

  2. // ¼ì²âÄĸö°´¼ü°´ÏÂ
  3. uint8_t Key_Scan(void){
  4.        
  5.         if(Press(KB1) == 0){
  6.                 return 1;
  7.         }
  8.         else if(Press(KB2) == 0){
  9.                 return 2;
  10.         }
  11.         else if(Press(KB3) == 0){
  12.                 return 3;
  13.         }
  14.         else if(Press(KB4) == 0){
  15.                 return 4;
  16.         }
  17.        
  18.         return 0;
  19. }
复制代码

五、主函数---主要是KEY模块有所变动
注意:LCD初始化放在LED初始化前边

1.key_it.h文件
3fa6402312d24438975870b8fee84bd8.png
  1. #include "stm32g4xx_hal.h"

  2. #define Press(x)  (HAL_GPIO_ReadPin(x))
  3. #define KB4 GPIOA, GPIO_PIN_0
  4. #define KB1 GPIOB, GPIO_PIN_0
  5. #define KB2 GPIOB, GPIO_PIN_1
  6. #define KB3 GPIOB, GPIO_PIN_2

  7. //¿ÉÐÞ¸Ä
  8. #define     SHORT_DELAY             2                  // ¶Ì°´ÑÓʱ
  9. #define     LONG_DELAY              80                // ³¤°´ÑÓʱ
  10. #define     CONTINUE_DELAY          10                // Á¬Ðø´¥·¢ÑÓʱ

  11. typedef struct {
  12.   uint8_t scan_flag;  //°´¼ü¶¨Ê±É¨Ãè±êʶ
  13.   uint16_t last_key;  //Éϴΰ´¼üÖµ  
  14.         uint8_t pressed;                //°´¼üÊÇ·ñÒѰ´Ï±êʶ
  15.         uint16_t cnt;       //°´¼ü°´ÏÂʱ¼ä³¤¶Ì¼ÆÊ±Æ÷
  16. }KeyScan_t;

  17. uint8_t Key_Scan(void);
复制代码

2.key_it.c文件

2136d9764f9049d8aea889a54aa74947.png

3.
  1. #include "key_it.h"

  2. // ¼ì²âÄĸö°´¼ü°´ÏÂ
  3. uint8_t Key_Scan(void){
  4.        
  5.         if(Press(KB1) == 0){
  6.                 return 1;
  7.         }
  8.         else if(Press(KB2) == 0){
  9.                 return 2;
  10.         }
  11.         else if(Press(KB3) == 0){
  12.                 return 3;
  13.         }
  14.         else if(Press(KB4) == 0){
  15.                 return 4;
  16.         }
  17.        
  18.         return 0;
  19. }


复制代码

3.main.c文件中的Key_It_Pro()函数---里面有三种情况(短按、长按和持续按)
  1. //Key processing function
  2. void Key_It_Proc(void){
  3.         if(key.scan_flag == 1){
  4.                 key.scan_flag = 0;
  5.                
  6.                 uint8_t cur_key = Key_Scan(); // »ñÈ¡µ±Ç°°´¼üÖµ
  7.                 uint8_t key_down = cur_key & (cur_key ^ key.last_key); // ¼ì²âϽµÑØ£¬Ö»ÓеÚÒ»´Î°´¼ü°´ÏÂʱ²»µÈÓÚ0
  8.                 uint8_t key_up = ~cur_key & (cur_key ^ key.last_key); // ¼ì²âÉÏÉýÑØ£¬±ØÐëÏÈÓа´¼ü°´Ï£¬ÔÙÊÍ·Åʱ²»µÈÓÚ0
  9.                
  10.                 if(key_down != 0){ // °´¼ü°´ÏÂʱ´¥·¢
  11.                         key.pressed = 1; // °´Ï±êʶÖÃ1
  12.                         key.cnt = 0; // ¼ÆÊ±Çå0
  13.                 }
  14.                
  15.                 if(key_up != 0){ // °´¼üÊÍ·Åʱ´¥·¢
  16.                         key.pressed = 0;
  17.                 }
  18.                
  19.                 if(key.pressed == 1){ // °´¼ü´¦ÓÚ°´ÏÂ״̬
  20.                         key.cnt++;
  21.                         if(key.cnt == LONG_DELAY){ // ³¤°´
  22.                                 switch(cur_key){
  23.                                         case 1:
  24.                                                 break;
  25.                                         case 2:
  26.                                                 break;
  27.                                         case 3:
  28.                                                 break;
  29.                                         case 4:
  30.                                                 break;
  31.                                         default:
  32.                                                 break;
  33.                                 }
  34.                         }else if(key.cnt >= LONG_DELAY+CONTINUE_DELAY){        // ³ÖÐø³¤°´
  35.                                 switch(cur_key){
  36.                                         case 1:
  37.                                                 break;
  38.                                         case 2:
  39.                                                 break;
  40.                                         case 3:
  41.                                                 break;
  42.                                         case 4:
  43.                                                 break;
  44.                                         default:
  45.                                                 break;
  46.                                 }
  47.                         }
  48.                 }else {
  49.                         if(key.last_key != 0 && key.cnt >= SHORT_DELAY && key.cnt < LONG_DELAY){ //¶Ì°´
  50.                                 switch(key.last_key){ // ÓÉÓÚÕâ´Î°´¼üÒѾ­Ì§Æð£¬¼üֵΪNOKEY£¬ËùÒÔÖ»ÓÐÅжÏÉϴεļüÖµ
  51.                                         case 1:
  52.                                                 Show_Flag++;
  53.                                                 if(Show_Flag == 3)
  54.                                                         Show_Flag = 1;
  55.                                                 break;
  56.                                         case 2:
  57.                                                 if(Show_Flag == 2)
  58.                                                 {
  59.                                                         CNBR_Price += 0.5f;
  60.                                                         VNBR_Price += 0.5f;                                               
  61.                                                 }
  62.                                                 break;
  63.                                         case 3:
  64.                                                 if(Show_Flag == 2)
  65.                                                 {
  66.                                                         CNBR_Price -= 0.5f;
  67.                                                         VNBR_Price -= 0.5f;
  68.                                                         if(CNBR_Price < 0)
  69.                                                                 CNBR_Price = 0.0f;
  70.                                                         if(VNBR_Price < 0)
  71.                                                                 VNBR_Price = 0.0f;                                                                                       
  72.                                                 }
  73.                                                 break;
  74.                                         case 4:
  75.                                                 break;               
  76.                                         default:
  77.                                                 ;
  78.                                 }
  79.                         }
  80.                 }
  81.                 key.last_key = cur_key;
  82.         }       
  83. }

  84. void Led_Proc(void)
  85. {
  86.         if(IDEL_Size > 0)
  87.                 LED_Ctrl(LED1, ON);
  88.         else
  89.                 LED_Ctrl(LED1, OFF);
  90. }
复制代码

————————————————
版权声明:想要优秀点儿


314cf75bace94c4b93829b7b53cdd5fc.png
c4417ba600084d789995bd1b2f98b212.png
收藏 评论0 发布时间:2023-3-2 13:47

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版