欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

標題: STM32矩陣鍵盤精簡版源碼 [打印本頁]

作者: zhaoziyi    時間: 2018-4-2 23:40
標題: STM32矩陣鍵盤精簡版源碼
此矩陣鍵盤簡短易懂!

單片機源程序如下:
  1. #include "sys.h"
  2. #include "delay.h"
  3. #include "usart.h"

  4. int key_can(void);
  5. void GPIO_Configuration(void);
  6. int cheak=0;
  7. int main(void)
  8. {               
  9.         u8 ss[]={'1','2','3','A','4','5','6','B','7','8','9','C','*','0','#','D'};
  10.          u8 t=0;
  11.         delay_init();                     //延時函數初始化         
  12. //        NVIC_Configuration();          //設置NVIC中斷分組2:2位搶占優先級,2位響應優先級
  13.         uart_init(9600);         //串口初始化為9600
  14.   GPIO_Configuration();
  15.   printf("請按鍵 \n");
  16.         while(1)
  17.         {   
  18.           t=key_can();
  19.           if(cheak)
  20.                 {
  21.                  printf("\n\r key=:%c \n\r",ss[t]);
  22.                  cheak=0;
  23.                 }
  24.         }
  25. }

  26. /********************************************************************
  27. * 名稱 : Keyscan()
  28. * 功能 : 實現按鍵的讀取。下面這個子程序是按處理 矩陣鍵盤 的基本方法處理的。
  29. * 輸入 : 無
  30. * 輸出 : 按鍵值
  31. * 管腳 :C0-C7
  32. ***********************************************************************/
  33. /*************************************************************

  34.                             ________
  35.                     7 |                   |
  36.                     6 |                   |
  37.                     5 |                   |
  38.                     4 |______|
  39.                              3 2 1 0

  40. *********************************************************************/
  41. int key_can(void)
  42. {
  43.         u8 BufferH[4] = {0x7f,0xbf, 0xdf,0xef};//
  44.         u8 j,temp;
  45.         u8 num=16;               
  46.         
  47.         for(j=0; j<4; j++)
  48.         {
  49.                 GPIO_SetBits(GPIOC,GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);        //上拉輸入默認高電平
  50.                 GPIO_ResetBits(GPIOC,GPIO_Pin_7>>j);    //依次置0
  51.                 temp=GPIOC->IDR;
  52.                 temp=temp&BufferH[j];
  53.                 if(temp!=BufferH[j])
  54.                 {                                                                                                                    
  55.                         delay_ms(5);                        //可以去掉消抖
  56.                         temp=GPIOC->IDR;
  57.                     temp=temp&BufferH[j];
  58.                         if(temp!=BufferH[j])                                 
  59.                   {
  60.                         
  61.                            if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0) == 0)
  62.                            {  
  63.                               num=3+j*4;
  64.                            }
  65.                            else if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_1) == 0)
  66.                            {  
  67.                               num=2+j*4;
  68.                            }
  69.                             else if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_2) == 0)
  70.                            {  
  71.                               num=1+j*4;
  72.                            }
  73.                             else if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_3) == 0 )
  74.                            {
  75.                               num=0+j*4;
  76.                            }
  77.                   }
  78.                   
  79.                   
  80.           while(temp!=BufferH[j])
  81.     {
  82.                         temp=GPIOC->IDR;
  83.                          temp=temp&BufferH[j];
  84.                         cheak=1;
  85.                 }
  86.         }
  87.         }
  88.         return(num);
  89. }



  90. void GPIO_Configuration(void)
  91. {
  92.   GPIO_InitTypeDef GPIO_InitStructure;
  93.   RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOC,ENABLE);            /*矩陣*/

  94.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
  95.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;                         //上拉輸入
  96.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  97.   GPIO_Init(GPIOC, &GPIO_InitStructure);

  98.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
  99.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;           //推挽輸出
  100.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  101.   GPIO_Init(GPIOC, &GPIO_InitStructure);   
  102.   
  103. }
復制代碼

所有資料51hei提供下載:
矩陣鍵盤.rar (301.75 KB, 下載次數: 243)



作者: hfh163    時間: 2018-9-14 13:47
參考用在stm32上
作者: hyg1997    時間: 2019-1-16 10:54
硬件怎么連接?
作者: 彬bin    時間: 2019-7-24 19:19
hyg1997 發表于 2019-1-16 10:54
硬件怎么連接?

可以修改IO口
作者: xibo95    時間: 2020-1-19 12:20
謝謝分享
作者: ly2324    時間: 2020-7-25 17:34
您好,請問temp=temp&BufferH[j];是什么意思呢
作者: ljz444666    時間: 2021-4-29 15:13
謝謝博主幫助!!!




歡迎光臨 (http://www.raoushi.com/bbs/) Powered by Discuz! X3.1