c語言鍵盤掃描程序
4*4
?/*? 4x4
?????????????????
????????????????? #include
????? sbit P1_0=P1^0;
????? sbit P1_1=P1^1;
????? sbit P1_2=P1^2;
????? sbit P1_3=P1^3;
????? sbit P2_7=P2^7;
????? sbit P3_7=P3^7;
????????????????? unsigned char code table[]={0xC0,0xF9,0xA4,0xB0,0x99, //0~4
???????????????????????? 0x92,0x82,0xF8,0x80,0x90, //5~9
???????????????????????? 0x88,0x83,0xC6,0xA1,0x86,0x8E}; //A~F
????????????????? void KeyScan();
????????????????? void delay10ms(unsigned char time);
????????????????? void Dispaly(unsigned char k);
????? void buzzer();
????????????????? unsigned char key,temp;
????????????????? void main() //主程序
????????????????? {
?????????????????? while(1)
?????????????????? {
??????????????????? KeyScan();
?????????????????? }
????????????????? }
????????????????? void KeyScan() //
????????????????? {
?????????????????? P1=0xFF;
?????????????????? P1_0=0;
?????????????????? temp=P1;
?????????????????? temp&=0xF0;
?????????????????? if(temp !=0xF0)
?????????????????? {buzzer();
??????????????????? delay10ms(10);
??????????????????? temp=P1;
??????????????????? temp&=0xF0;
??????????????????? if(temp !=0xF0)
??????????????????? {
???????????????????? temp=P1;
???????????????????? temp&=0xF0;
???????????????????? switch(temp)
???????????????????? {
???????????????????? case 0xE0:
????????????????????? key=0;break;
???????????????????? case 0xD0:
????????????????????? key=4;break;
???????????????????? case 0xB0:
????????????????????? key=8;break;
???????????????????? case 0x70:
????????????????????? key=12;break;
???????????????????? }
???????????????????? Dispaly(key);
??????????????????? }
?????????????????? }
?????????????????? P1=0xFF;
?????????????????? P1_1=0;
?????????????????? temp=P1;
?????????????????? temp&=0xF0;
?????????????????? if(temp !=0xF0)
?????????????????? {buzzer();
??????????????????? delay10ms(10);
??????????????????? temp=P1;
??????????????????? temp&=0xF0;
??????????????????? if(temp !=0xF0)
??????????????????? {
???????????????????? temp=P1;
???????????????????? temp&=0xF0;
???????????????????? switch(temp)
???????????????????? {
???????????????????? case 0xE0:
????????????????????? key=1;break;
???????????????????? case 0xD0:
????????????????????? key=5;break;
???????????????????? case 0xB0:
????????????????????? key=9;break;
???????????????????? case 0x70:
????????????????????? key=13;break;
???????????????????? }
???????????????????? Dispaly(key);
??????????????????? }
?????????????????? }
?????????????????? P1=0xFF;
?????????????????? P1_2=0;
?????????????????? temp=P1;
?????????????????? temp&=0xF0;
?????????????????? if(temp !=0xF0)
?????????????????? {buzzer();
??????????????????? delay10ms(10);
??????????????????? temp=P1;
??????????????????? temp&=0xF0;
??????????????????? if(temp !=0xF0)
??????????????????? {
???????????????????? temp=P1;
???????????????????? temp&=0xF0;
???????????????????? switch(temp)
???????????????????? {
???????????????????? case 0xE0:
????????????????????? key=2;break;
???????????????????? case 0xD0:
????????????????????? key=6;break;
???????????????????? case 0xB0:
????????????????????? key=10;break;
???????????????????? case 0x70:
????????????????????? key=14;break;
???????????????????? }
???????????????????? Dispaly(key);
??????????????????? }
?????????????????? }
?????????????????? P1=0xFF;
?????????????????? P1_3=0;
?????????????????? temp=P1;
?????????????????? temp&=0xF0;
?????????????????? if(temp !=0xF0)
?????????????????? {buzzer();
??????????????????? delay10ms(10);
??????????????????? temp=P1;
??????????????????? temp&=0xF0;
??????????????????? if(temp !=0xF0)
??????????????????? {
???????????????????? temp=P1;
???????????????????? temp&=0xF0;
???????????????????? switch(temp)
???????????????????? {
???????????????????? case 0xE0:
????????????????????? key=3;break;
???????????????????? case 0xD0:
????????????????????? key=7;break;
???????????????????? case 0xB0:
????????????????????? key=11;break;
???????????????????? case 0x70:
????????????????????? key=15;break;
???????????????????? }
???????????????????? Dispaly(key);
??????????????????? }
?????????????????? }
????????????????? }
????????????????? //延時程序
????????????????? void delay10ms(unsigned char time)
????????????????? {
?????????????????? unsigned char i;
?????? while(time--)
????????????????? {
???????????????????? for(i=0;i<120;i++) ;
????????????????? }
????? }
????????????????? void Dispaly(unsigned char k) //顯示程序
????????????????? {
?????????????????? P0=table[k];
?????????????????? P2_7=0;
????????????????? }
????? void buzzer()
????? {
?????? P3_7=0;
?????? delay10ms(20);
?????? P3_7=1;
?????? }
6x3掃描鍵盤驅(qū)動程序(C語言編寫)
〖說明〗18個按鍵掃描驅(qū)動程序,橫向掃描第一行:p0.6??第二行:p0.7??第三行:p2.6
????????縱向掃描:第一行:p0.0??第二行:p0.1??第三行:p0.2??第四行:p0.3??第五行:
????????p0.4??第六行:p0.5
〖文件〗key.c??2002/12/10
〖作者〗鞠春陽
〖信箱〗j(luò)cygood@163.net???jcygood@sina.com
〖版本〗v1.00a?build
--------------------------------------------------------------------------------*/
//#i nclude?"reg51.h"
//#i nclude?"lcd12864.h"
#define???port????p0??//定義p0口
sbit??????p2_6=p2^6;??//定義p2.6口
unsigned?int?zdtime=0;
unsigned?int?pingbaotime=0;
/*定義18個按鍵的返回值*/
#define??runkey?????????????????1
#define??stopkey????????????????2
#define??upkey??????????????????3
#define??downkey????????????????4
#define??a_c????????????????????5
#define??d_f????????????????????6
#define??g_i????????????????????7
#define??j_l????????????????????8
#define??m_o????????????????????9
#define??p_s????????????????????10
#define??t_v????????????????????11
#define??w_z????????????????????12
#define??pageupkey??????????????13
#define??pagedownkey????????????14
#define??enterkey???????????????15
#define??backkey????????????????16
#define??chineseorenglish???????17
#define??cancelkey??????????????18
//按鍵掃描函數(shù)
unsigned?char?getkey()
{
??unsigned?char?a,b;
??unsigned?int?delay=0;?//延時變量
??port=0xff;??????????//p0口復(fù)位
??p2_6=1;?????????????//p2.6復(fù)位
??//掃描第三行
??p2_6=0;
???while(!(port&0x01))
?????{
??????if(++delay>0x1ff0)?{??return(pagedownkey);}
?????}
???while(!(port&0x02))
?????{
??????if(++delay>0x1ff0)?{??return(chineseorenglish);}
?????}
???while(!(port&0x04))
?????{
??????if(++delay>0x1ff0)?{??return(backkey);}
?????}
???while(!(port&0x08))
?????{
??????if(++delay>0x1ff0)?{??return(pageupkey);}
?????}
???while(!(port&0x10))
?????{
??????if(++delay>0x1ff0)?{??return(cancelkey);}
?????}
???while(!(port&0x20))
?????{
??????if(++delay>0x1ff0)?{??return(enterkey);}
?????}
???port=0xff;??????????//p0口復(fù)位
???p2_6=1;?????????????//p2.6復(fù)位
???//掃描第一行
???port=0x7f;
???delay=0;
??while(!(port&0x01))
?????{
??????if(++delay>0x1ff0)?{??return(downkey);}
?????}
??if(delay>0xff0)??{?return(downkey);?}
??while(!(port&0x02))
?????{
??????if(++delay>0x1ff0)?{??return(stopkey);}
?????}
??if(delay>0xff0)??{?return(stopkey);?}
???while(!(port&0x04))
?????{
??????if(++delay>0x5fd0)?{??return(w_z);}
?????}
???if(delay>0xff0)??{?return(w_z);?}
???while(!(port&0x08))
?????{
??????if(++delay>0x5fd0)?{??return(t_v);}
?????}
???if(delay>0xff0)??{?return(t_v);?}
???while(!(port&0x10))
?????{
??????if(++delay>0x5fd0)?{??return(p_s);}
?????}
???if(delay>0xff0)??{?return(p_s);?}
???while(!(port&0x20))
?????{
??????if(++delay>0x5fd0)?{??return(m_o);}
?????}
??if(delay>0xff0)??{?return(m_o);?}
??port=0xff;??????????//p0口復(fù)位
??p2_6=1;?????????????//p2.6復(fù)位
??//掃描第二行
???port=0xbf;
???delay=0;
???while(!(port&0x01))
?????{
??????if(++delay>0x1ff0)?{??return(upkey);}
?????}
???if(delay>0xff0)??{?return(upkey);?}
???while(!(port&0x02))
?????{
??????if(++delay>0x1ff0)?{??return(runkey);}
?????}
???if(delay>0xff0)??{?return(runkey);?}
???while(!(port&0x04))
?????{
??????if(++delay>0x5fd0)?{??return(j_l);}
?????}
???if(delay>0xff0)??{?return(j_l);?}
???while(!(port&0x08))
?????{
??????if(++delay>0x5fd0)?{??return(g_i);}
?????}
???if(delay>0xff0)??{?return(g_i);?}
???while(!(port&0x10))
?????{
??????if(++delay>0x5fd0)?{??return(d_f);}
?????}
???if(delay>0xff0)??{?return(d_f);?}
???while(!(port&0x20))
?????{
??????if(++delay>0x5fd0)?{??return(a_c);}
?????}
???if(delay>0xff0)??{?return(a_c);?}
???return(0);
}
3x4矩陣鍵盤的掃描程序(C語言)
/*? 3x4矩陣鍵盤的掃描程序
按相應(yīng)的按鍵,數(shù)碼管顯示相應(yīng)的數(shù)字,星號鍵和井號鍵分別顯示為E和F
*********************************
? Create by:左秀龍?
? support:
? Tel:0755-82879296
? email: cutedesign@tom.com
? Date: 2005/06/23
*********************************
測試通過*/
#include
unsigned char code table[]={0xC0,0xF9,0xA4,0xB0,0x99,?//0~4
???????0x92,0x82,0xF8,0x80,0x90,?//5~9
???????0x88,0x83,0xA7,0xA1,0x86,0x8E};?//A~F
void KeyScan();
void delay10ms(unsigned char time);
void Dispaly(unsigned char k);
unsigned char key,temp;
void main()?//主程序
{
?while(1)
?{
??KeyScan();
?}
}
void KeyScan()?//按鍵掃描子程序
{
?P1=0xFF;
?P1_3=0;
?temp=P1;
?temp&=0xF0;
?if(temp !=0xF0)
?{
??delay10ms(1);
??temp=P1;
??temp&=0xF0;
??if(temp !=0xF0)
??{
???temp=P1;
???temp&=0xF0;
???switch(temp)
???{
???case 0x70:
????key=1;break;
???case 0xB0:
????key=2;break;
???case 0xD0:
????key=3;break;
???}
???Dispaly(key);
??}
?}
?P1=0xFF;
?P1_2=0;
?temp=P1;
?temp&=0xF0;
?if(temp !=0xF0)
?{
??delay10ms(1);
??temp=P1;
??temp&=0xF0;
??if(temp !=0xF0)
??{
???temp=P1;
???temp&=0xF0;
???switch(temp)
???{
???case 0x70:
????key=4;break;
???case 0xB0:
????key=5;break;
???case 0xD0:
????key=6;break;
???}
???Dispaly(key);
??}
?}
?P1=0xFF;
?P1_1=0;
?temp=P1;
?temp&=0xF0;
?if(temp !=0xF0)
?{
??delay10ms(1);
??temp=P1;
??temp&=0xF0;
??if(temp !=0xF0)
??{
???temp=P1;
???temp&=0xF0;
???switch(temp)
???{
???case 0x70:
????key=7;break;
???case 0xB0:
????key=8;break;
???case 0xD0:
????key=9;break;
???}
???Dispaly(key);
??}
?}
?P1=0xFF;
?P1_0=0;
?temp=P1;
?temp&=0xF0;
?if(temp !=0xF0)
?{
??delay10ms(1);
??temp=P1;
??temp&=0xF0;
??if(temp !=0xF0)
??{
???temp=P1;
???temp&=0xF0;
???switch(temp)
???{
???case 0x70:
????key=14;break;
???case 0xB0:
????key=0;break;
???case 0xD0:
????key=15;break;
???}
???Dispaly(key);
??}
?}
}
//延時程序
void delay10ms(unsigned char time)
{
?unsigned char a,b,c;
?for(a=0;a
void Dispaly(unsigned char k)?//顯示程序
{
?P0=table[k];
?P2_1=0;
}
評論
查看更多