久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2379|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

STM32源程序-固件庫-矩陣鍵盤

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
STM32 矩陣鍵盤
簡介
1.硬件部分
2.軟件部分

硬件部分
矩陣鍵盤的工作方式

對鍵盤的響應(yīng)取決于鍵盤的工作方式,鍵盤的工作方式應(yīng)根據(jù)實(shí)際應(yīng)用系統(tǒng)中的CPU的工作狀況而定,其選取的原則是既要保證CPU能及時(shí)響應(yīng)按鍵操作,又不要過多占用CPU的工作時(shí)間。通常鍵盤的工作方式有三種,編程掃描、定時(shí)掃描和中斷掃描。

(1)編程掃描方式

編程掃描方式是利用CPU完成其它工作的空余時(shí)間,調(diào)用鍵盤掃描子程序來響應(yīng)鍵盤輸入的要求。在執(zhí)行鍵功能程序時(shí),CPU不再響應(yīng)鍵輸入要求,直到CPU重新掃描鍵盤為止。

(2)定時(shí)掃描方式

定時(shí)掃描方式就是每隔一段時(shí)間對鍵盤掃描一次,它利用單片機(jī)內(nèi)部的定時(shí)器產(chǎn)生一定時(shí)間(例如10ms)的定時(shí),當(dāng)定時(shí)時(shí)間到就產(chǎn)生定時(shí)器溢出中斷。CPU響應(yīng)中斷后對鍵盤進(jìn)行掃描,并在有按鍵按下時(shí)識別出該鍵,再執(zhí)行該鍵的功能程序。

(3)中斷掃描方式

上述兩種鍵盤掃描方式,無論是否按鍵,CPU都要定時(shí)掃描鍵盤,而單片機(jī)應(yīng)用系統(tǒng)工作時(shí),并非經(jīng)常需要鍵盤輸入,因此,CPU經(jīng)常處于空掃描狀態(tài)。

為提高CPU工作效率,可采用中斷掃描工作方式。其工作過程如下:當(dāng)無按鍵按下時(shí),CPU處理自己的工作,當(dāng)有按鍵按下時(shí),產(chǎn)生中斷請求,CPU轉(zhuǎn)去執(zhí)行鍵盤掃描子程序,并識別鍵號。

4x4矩陣鍵盤模塊原理圖
  1. /*********************************************************************
  2. * 文件名  :key4x4.c
  3. * 描述    :按鍵應(yīng)用函數(shù)庫        

  4. * 硬件連接:-------------------------
  5. *          | PB8  - H4          |
  6. *          | PB9  - H3                         |
  7. *          | PB10 - H2          |
  8. *          | PB11 - H1                     |
  9. *          | PB12 - L4          |
  10. *          | PB13 - L3                     |
  11. *          | PB14 - L2          |
  12. *          | PB15 - L1                         |
  13. *           -------------------------
  14. * 庫版本  :ST3.5.0
  15. *********************************************************************/

  16. #include "key4x4.h"
  17. #include "delay.h"

  18. /*
  19. * 函數(shù)名:Key_GPIO_Config
  20. * 描述  :配置按鍵用到的I/O口 GPIO端口可以自行定義
  21. * 輸入  :無
  22. * 輸出  :無
  23. */
  24. void Key4x4_GPIO_Config(void)
  25. {
  26.           GPIO_InitTypeDef GPIO_InitStructure;
  27.          
  28.           /*開啟按鍵端口的時(shí)鐘*/
  29.           RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);

  30.          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; //GPIO端口引腳可以自行定義
  31.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  32.    //配置引腳速度
  33.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  34.     //配置引腳模式  GPIO_Mode_IPU 上拉輸入
  35.         GPIO_Init(GPIOB, &GPIO_InitStructure);

  36.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;        
  37.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;      
  38.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  39.           GPIO_Init(GPIOB, &GPIO_InitStructure);
  40. }
  41. /*
  42. * 函數(shù)名:u8 Key_Scan
  43. * 描述  :按鍵掃描函數(shù)
  44. * 輸入  :無
  45. * 輸出  :無
  46. */
  47. u8 Key_Scan(void)
  48. {
  49.         u16 temp;
  50.         u8 ucBackValue=0;
  51.         //====掃描第一列==========
  52.         GPIO_Write(GPIOB,0xfe00);
  53.         temp=GPIO_ReadInputData(GPIOB); //讀出整個口得到的值
  54.         temp=temp&0xf000;    //屏蔽低4位
  55.         if (temp!=0xf000)      //假如高4位不全是1
  56.         {
  57.                 delay_ms(20);      // 延時(shí)消抖再讀
  58.                 temp=GPIO_ReadInputData(GPIOB);
  59.                 temp=temp&0xf000;
  60.                 if (temp!=0xf000)     //消抖后如果再次高4位確定不是全1
  61.                 {
  62.                         temp=GPIO_ReadInputData(GPIOB)&0xff00; //讀出此次按鍵的值
  63.                         switch(temp)
  64.                         {
  65.                                 case 0xee00:
  66.                                 ucBackValue = 1;break;
  67.                                 case 0xde00:
  68.                                 ucBackValue = 5;break;
  69.                                 case 0xbe00:
  70.                                 ucBackValue = 9;break;
  71.                                 case 0x7e00:
  72.                                 ucBackValue = 13;break;
  73.                                 default:break;
  74.                         }
  75.                         while(temp!=0xf000)   //等待按鍵放開,初始必然被執(zhí)行
  76.                         {
  77.                                 temp=GPIO_ReadInputData(GPIOB);
  78.                                 temp=temp&0xf000;
  79.                         }

  80.                 }
  81.         }

  82.         //====第二列送0==========
  83.         GPIO_Write(GPIOB,0xfd00);
  84.         temp=GPIO_ReadInputData(GPIOB); //讀出整個口得到的值
  85.         temp=temp&0xf000;    //屏蔽低4位
  86.         if (temp!=0xf000)      //假如高4位不全是1
  87.         {
  88.                 delay_ms(20);      // 延時(shí)消抖再讀
  89.                 temp=GPIO_ReadInputData(GPIOB);
  90.                 temp=temp&0xf000;
  91.                 if (temp!=0xf000)     //消抖后如果再次高4位確定不是全1
  92.                 {
  93.                         temp=GPIO_ReadInputData(GPIOB)&0xff00; //讀出此次按鍵的值
  94.                         switch(temp)
  95.                         {
  96.                                 case 0xed00:
  97.                                         ucBackValue = 2; break;
  98.                                 case 0xdd00:
  99.                                         ucBackValue = 6; break;
  100.                                 case 0xbd00:
  101.                                         ucBackValue = 10; break;
  102.                                 case 0x7d00:
  103.                                         ucBackValue = 14; break;
  104.                                 default:break;
  105.                 }
  106.                         while(temp!=0xf000)   //等待按鍵放開
  107.                         {
  108.                                 temp=GPIO_ReadInputData(GPIOB);
  109.                                 temp=temp&0xf000;
  110.                         }
  111.                 }
  112.         }
  113.         //====第3列送0==========
  114.         GPIO_Write(GPIOB,0xfb00);
  115.         temp=GPIO_ReadInputData(GPIOB); //讀出整個口得到的值
  116.         temp=temp&0xf000;    //屏蔽低4位
  117.         if (temp!=0xf000)      //假如高4位不全是1
  118.         {
  119.                 delay_ms(20);      // 延時(shí)消抖再讀
  120.                 temp=GPIO_ReadInputData(GPIOB);
  121.                 temp=temp&0xf000;
  122.                 if (temp!=0xf000)     //消抖后如果再次高4位確定不是全1
  123.                 {
  124.                         temp=GPIO_ReadInputData(GPIOB)&0xff00; //讀出此次按鍵的值
  125.                         switch(temp)
  126.                         {
  127.                                 case 0xeb00:
  128.                                 ucBackValue = 3; break;
  129.                                 case 0xdb00:
  130.                                 ucBackValue = 7; break;
  131.                                 case 0xbb00:
  132.                                 ucBackValue = 11; break;
  133.                                 case 0x7b00:
  134.                                 ucBackValue = 15; break;
  135.                                 default:break;
  136.                 }
  137.                 while(temp!=0xf000)   //等待按鍵放開
  138.                 {
  139.                         temp=GPIO_ReadInputData(GPIOB);
  140.                         temp=temp&0xf000;
  141.                 }
  142.                 }
  143.         }
  144.         //====第4列送0==========
  145.         GPIO_Write(GPIOB,0xf700);
  146.         temp=GPIO_ReadInputData(GPIOB); //讀出整個口得到的值
  147.         temp=temp&0xf000;    //屏蔽低4位
  148.         if (temp!=0xf000)      //假如高4位不全是1
  149.         {
  150.                 delay_ms(20);       // 延時(shí)消抖再讀
  151.                 temp=GPIO_ReadInputData(GPIOB);
  152.                 temp=temp&0xf000;
  153.                 if (temp!=0xf000)     //消抖后如果再次高4位確定不是全1
  154.                 {
  155.                         temp=GPIO_ReadInputData(GPIOB)&0xff00;
  156.                         switch(temp)
  157.                         {
  158.                                 case 0xe700:
  159.                                 ucBackValue = 4; break;
  160.                                 case 0xd700:
  161.                                 ucBackValue = 8; break;
  162.                                 case 0xb700:
  163.                                 ucBackValue = 12; break;
  164.                                 case 0x7700:
  165.                                 ucBackValue = 16; break;
  166.                                 default:break;
  167.                         }
  168.                         while(temp!=0xf000)   //等待按鍵放開
  169.                         {
  170.                                 temp=GPIO_ReadInputData(GPIOB);
  171.                                 temp=temp&0xf000;
  172.                         }
  173.                 }
  174.         }
  175.         return ucBackValue;
  176. }

復(fù)制代碼
  1. #ifndef __KEY_H
  2. #define        __KEY_H

  3. #include "stm32f10x.h"


  4. void Key4x4_GPIO_Config(void);
  5. u8 Key_Scan(void);

  6. #endif /* __KEY_H */
復(fù)制代碼

評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 久久精品亚洲国产奇米99 | 国产精品久久久 | 国产污视频在线 | 欧美天堂 | 午夜私人影院在线观看 | 久久国产精品久久国产精品 | 有码在线| 亚洲欧美日韩在线不卡 | 羞羞视频网站免费观看 | av中文字幕在线观看 | 伊人免费在线观看 | 久久久久久久久毛片 | 在线中文字幕视频 | 日韩二区| 人人干在线 | 国产成人一区二区三区久久久 | 黄色免费网址大全 | 亚洲乱码国产乱码精品精的特点 | 日本色婷婷 | 中文字幕在线观看第一页 | 国产精品中文字幕在线观看 | 国产精品99久久久久久www | 亚洲欧洲在线视频 | 欧美一区二区在线免费观看 | 欧美嘿咻 | 成人一区二区三区在线观看 | 美女露尿口视频 | 91精品国产日韩91久久久久久 | 国产精品国产自产拍高清 | 午夜理伦三级理论三级在线观看 | 欧美激情精品久久久久久变态 | 精品乱码久久久久 | 精品一二三区视频 | 亚洲高清成人在线 | 操久久 | 在线免费观看成年人视频 | 韩日精品在线观看 | 日韩成年人视频在线 | av免费观看在线 | 亚洲国产精品久久久久婷婷老年 | 中文字幕在线网 |