51單片機(AT89C52)矩陣鍵盤檢測程式

來源:互聯網
上載者:User

矩陣鍵盤檢測思路:行線設為輸出、列線設為輸入獲得列碼,行線設為輸入、列線設為輸出獲得行碼,由行列碼確定索引值。

本程式可改進的地方:按鍵狀態機器中,將等待按鍵釋放語句移到第三個狀態中,可進一步提高按鍵掃描程式效率。

#include<reg52.h>#define uchar unsigned char#define uint unsigned intsbit dula=P3^0;sbit wela=P3^1;#define keyport (P1)#define keystate0 0//按鍵確認狀態#define keystate1 1//按鍵無動作狀態char keystate=1;//按鍵狀態初始化為按鍵無動作狀態uchar keynum=0;void delayms(uint x)//1ms{uint y,z;for(y=x;y>0;y--)for(z=111;z>0;z--);}uchar code table[]={//共陽數位管0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};void smg(){dula=0;wela=0;P2=0x01;wela=1;wela=0;P2=table[keynum];dula=1;dula=0;delayms(1);P2=0x02;wela=1;wela=0;P2=table[keynum];dula=1;dula=0;delayms(1);P2=0x04;wela=1;wela=0;P2=table[keynum];dula=1;dula=0;delayms(1);P2=0x08;wela=1;wela=0;P2=table[keynum];dula=1;dula=0;delayms(1);P2=0x10;wela=1;wela=0;P2=table[keynum];dula=1;dula=0;delayms(1);P2=0x20;wela=1;wela=0;P2=table[keynum];dula=1;dula=0;delayms(1);P2=0x40;wela=1;wela=0;P2=table[keynum];dula=1;dula=0;delayms(1);P2=0x80;wela=1;wela=0;P2=table[keynum];dula=1;dula=0;delayms(1);}void keyscan(){uchar temp1,temp2,column,row;switch(keystate){case keystate0://keystate0,按鍵按下確認狀態,下一狀態為keystate1(按鍵無動作狀態)keyport=0x0f;temp1=keyport;keyport=0xf0;temp2=keyport;if((temp1|temp2)!=0xff){temp1|=temp2;if(!(temp1&0x80))row=3;else if(!(temp1&0x40))row=2;elseif(!(temp1&0x20))row=1;elseif(!(temp1&0x10))row=0;if(!(temp1&0x08))column=3;else if(!(temp1&0x04))column=2;elseif(!(temp1&0x02))column=1;elseif(!(temp1&0x01))column=0;keynum=column*4+row;while(!(keyport==0x0f|keyport==0xf0));keystate=keystate1;break;}elsekeystate=keystate1;break;case keystate1://keystate1,按鍵無動作狀態,下一狀態為keystate0(按鍵無動作狀態)keyport=0x0f;temp1=keyport;keyport=0xf0;temp2=keyport;if((temp1|temp2)!=0xff){keystate=keystate0;}elsekeystate=keystate1;break;}}void main(){while(1){keyscan();smg();}}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.