標題: STC15單片機矩陣鍵盤的代碼哪里出錯了,大佬可以幫我看看嗎 [打印本頁]

作者: KCLMX    時間: 2025-3-7 23:23
標題: STC15單片機矩陣鍵盤的代碼哪里出錯了,大佬可以幫我看看嗎
這個STC15單片機的矩陣鍵盤代碼哪里出錯了呀,大佬幫我看看吧,給我矩陣鍵盤的代碼也可以

IMG20250307231956.jpg (10.89 MB, 下載次數: 0)

IMG20250307231956.jpg

IMG20250307232002.jpg (11.1 MB, 下載次數: 0)

IMG20250307232002.jpg

作者: WL0123    時間: 2025-3-8 06:55
這個程序用在51內核的MCU具有普適性
  1. #include <reg52.h>
  2. #define uint unsigned int
  3. #define uchar unsigned char
  4. uchar key=0;                                        //定義鍵值全局變量

  5. void keyscan()                                        //按鍵掃描程序
  6. {
  7.         static bit sign=0;                        //按鍵狀態(tài)標志
  8.         static uint count=0;                //消抖計數變量                       
  9.         uchar num=0;                                //臨時變量
  10.         P3=0xf0;                                        //賦值P3 1111 0000
  11.         if(P3!=0xf0)                                //檢測有按鍵按下
  12.         {
  13.                 count++;                                //消抖計數
  14.                 if(count>=100 && sign==0)//100~10000,根據循環(huán)周期調整約10~20ms
  15.                 {                       
  16.                         sign=1;                                //按鍵狀態(tài)標志置1
  17.                         num=P3;                                //保存P3值xxxx 0000,x為0或1
  18.                         num|=0x0f;                        //保存num按位或0x0f值xxxx 1111
  19.                         P3=num;                                //賦值P3 xxxx 1111
  20.                         switch(P3)
  21.                         {
  22.                                 case 0xee: key= 1; break;
  23.                                 case 0xde: key= 2; break;
  24.                                 case 0xbe: key= 3; break;
  25.                                 case 0x7e: key= 4; break;
  26.                                 case 0xed: key= 5; break;
  27.                                 case 0xdd: key= 6; break;
  28.                                 case 0xbd: key= 7; break;
  29.                                 case 0x7d: key= 8; break;
  30.                                 case 0xeb: key= 9; break;
  31.                                 case 0xdb: key=10; break;
  32.                                 case 0xbb: key=11; break;
  33.                                 case 0x7b: key=12; break;
  34.                                 case 0xe7: key=13; break;
  35.                                 case 0xd7: key=14; break;
  36.                                 case 0xb7: key=15; break;
  37.                                 case 0x77: key=16; break;
  38.                         }
  39.                 }
  40.         }
  41.         else                                                //鍵抬起
  42.         {
  43.                 sign=0;                                        //按鍵自鎖標志清0
  44.                 count=0;                                //消抖計數清0
  45.         }
  46. }
  47. void main()                                                //主函數
  48. {
  49.         while(1)
  50.         {
  51.                 keyscan();                                //按鍵掃描程序
  52.                 P1=~key;                                //LED顯示鍵碼1~16,低電平亮
  53.         }
  54. }
復制代碼

作者: johnhk97    時間: 2025-3-8 10:45
這個不好回答!代碼跟硬件需要配合,原理圖都沒有,我個人才疏學淺,不懂回答。

作者: KK12312    時間: 2025-3-14 17:55
代碼上看,沒啥問題,你是現象出問題了還是什么
作者: KK12312    時間: 2025-3-14 18:00
你這個暫時看不出問題,我附上我之前參加藍橋杯時候的矩陣鍵盤代碼#include "ALL.H"
#include "REG52.H"
/*矩陣鍵盤編號
1     2    3    4   P3_0

5     6    7    8   P3_1

9    10   11   12   P3_2

13   14   15   16   P3_3

P4_4 P4_2 P3_5 P3_4

*/
unsigned char Matric_Scan(){
        unsigned char Num=0;
        P3_0=0;
        P3_1=P3_2=P3_3=1;
        P4_4=P4_2=P3_5=P3_4=1;
        if(P4_4==0){
                Num=1;       
        }               
        else if(P4_2==0){
                Num=2;
        }
        else if(P3_5==0){
                Num=3;
        }
        else if(P3_4==0){
                Num=4;
        }
        P3_1=0;
        P3_0=P3_2=P3_3=1;
        P4_4=P4_2=P3_5=P3_4=1;
        if(P4_4==0){
      Num=5;
        }               
        else if(P4_2==0){
                Num=6;
        }
        else if(P3_5==0){
                Num=7;
        }
        else if(P3_4==0){
                Num=8;
        }
        P3_2=0;
        P3_0=P3_1=P3_3=1;
        P4_4=P4_2=P3_5=P3_4=1;
        if(P4_4==0){
      Num=9;
        }               
        else if(P4_2==0){
                Num=10;
        }
        else if(P3_5==0){
                Num=11;
        }
        else if(P3_4==0){
                Num=12;
        }
        P3_3=0;
        P3_0=P3_1=P3_2=1;
        P4_4=P4_2=P3_5=P3_4=1;
        if(P4_4==0){
      Num=13;
        }               
        else if(P4_2==0){
                Num=14;
        }
        else if(P3_5==0){
                Num=15;
        }
        else if(P3_4==0){
                Num=16;
        }
        return Num;
}

void Matric_Show_Num(){
       
        if(Matric_Scan()==1){
                        Display(1,1);
                }
                else if(Matric_Scan()==2){
                        Display(1,2);
                }
                else if(Matric_Scan()==3){
                        Display(1,3);
                }
                else if(Matric_Scan()==4){
                        Display(1,4);
                }
                else if(Matric_Scan()==5){
                        Display(1,5);
                }
                else if(Matric_Scan()==6){
                        Display(1,6);
                }
                else if(Matric_Scan()==7){
                        Display(1,7);
                }
                else if(Matric_Scan()==8){
                        Display(1,8);
                }
                else if(Matric_Scan()==9){
                        Display(1,9);
                }
                else if(Matric_Scan()==10){
                        Display(1,10);
                }
                else if(Matric_Scan()==11){
                        Display(1,11);
                }
                else if(Matric_Scan()==12){
                        Display(1,12);
                }
                else if(Matric_Scan()==13){
                        Display(1,13);
                }
                else if(Matric_Scan()==14){
                        Display(1,14);
                }
                else if(Matric_Scan()==15){
                        Display(1,15);
                }
                else if(Matric_Scan()==16){
                        Display(1,16);
                }
}
應該會有幫助
作者: 譚1234    時間: 2025-3-15 08:20
跳線帽沒接到矩陣?

作者: 譚1234    時間: 2025-3-15 08:25
這寫法看著都頭疼
sbit R1 = P3^0;
sbit R2 = P3^1;
sbit R3 = P3^2;
sbit R4 = P3^3;

sbit C1 = P4^4;
sbit C2 = P4^2;
sbit C3 = P3^5;
sbit C4 = P3^4;
uchar Key_Open(void)
{
  uchar key = 0;

  C1 = 0;C2 = 1;C3 = 1;C4 = 1;
  if(R1 == 0) key = 7;
  if(R2 == 0) key = 6;
  if(R3 == 0) key = 5;
  if(R4 == 0) key = 4;  

  C1 = 1;C2 = 0;C3 = 1;C4 = 1;
  if(R1 == 0) key = 11;
  if(R2 == 0) key = 10;
  if(R3 == 0) key = 9;
  if(R4 == 0) key = 8;

  C1 = 1;C2 = 1;C3 = 0;C4 = 1;
  if(R1 == 0) key = 15;
  if(R2 == 0) key = 14;
  if(R3 == 0) key = 13;
  if(R4 == 0) key = 12;

  C1 = 1;C2 = 1;C3 = 1;C4 = 0;
  if(R1 == 0) key = 19;
  if(R2 == 0) key = 18;
  if(R3 == 0) key = 17;
  if(R4 == 0) key = 16;

  return key;
}
作者: keemee    時間: 2025-3-17 03:50
譚1234 發(fā)表于 2025-3-15 08:25
這寫法看著都頭疼
sbit R1 = P3^0;
sbit R2 = P3^1;

為啥鍵值要加大




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