以上程序功能很简单,但它演示了一个单片机键盘处理程序的基本思路,程序本身很简单,也不很实用,实际工作中还会有好多要考虑的因素,比如主循环每次都调用灯的循环程序,会造成按钮反应“迟钝”,而如果一直按着键不放,则灯不会再流动,一直要到松开手为止,等等,大家能仔细考虑一下这些问题,再
想想有什么好的解决办法。
2、采用中断方式:如图4所示。各个按钮都接到一个与非上,当有任何一个按钮按下时,都会使与门输出为低电平,从而引起单片机的中断,它的好处是不用在主程序中持续地循环查询,如果有键按下,单片
机再去做对应的处理。
/*我写的这个计算器可以算255范围以内的整数计算,本打算再加入小数与做最后的处理工作。可是又太费时间,所以就放那里了。看见了你的提问,也懒得改一改,你看看能不能用。*/
#include
unsigned char Line,Row,Val; void delay() {
unsigned char count;
for(count=0;count<255;count++) ; }
void delay1() {
unsigned char count,rt; for(count=0;count<40;count++) for(rt=0;rt<100;rt++) ; }
void InitialCPU(void) {
P0=0xFF; P1=0x0F; P2=0xFF; P3=0xFF; }
unsigned char leddata[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; //\
unsigned char ledbai(unsigned char dat) {
unsigned char bai; bai=dat/100; if(bai==0) return 0x00; P2=0x20; P0=leddata[bai]; delay(); P2=0x00; return 0x00; }
unsigned char ledshi(unsigned char dat) {
unsigned char shi; shi=(dat0)/10; if(shi==0) return 0x00; P2=0x40; P0=leddata[shi]; delay(); P2=0x00; return 0x00; }
void ledge(unsigned char dat) {
unsigned char ge; ge=dat; P2=0x80; P0=leddata[ge]; delay(); }
unsigned char bin=1;
void ledplay(unsigned char dat) { //LED显示函数 if(bin==1){ ledbai(dat); bin++; }
else if(bin==2){ ledshi(dat); bin++; }
else if(bin==3){
ledge(dat); bin=1; } }
unsigned char futemp=0,data1=0,data2=0; void InitialTimer0(void) {
TMOD=0x01;
TH0=(65536-1000)/256; TR0=1; //启动T0 EA=1;
ET0=1; //允许T0中断 }
void Timer0(void) interrupt 1 using 3 {
TR0=0;
if(data1==0&&futemp==0&&data2==0){ //LED显示data1,即初始值 ledplay(data1); }
else if(data1!=0&&futemp==0&&data2==0) // data1有值了,显示data1, ledplay(data1);
else if(data1==0&&futemp!=0&&data2==0) //有两种可能,1、数据data1为零;2、直接data1没有给它值。总之显示data1
ledplay(data1);
else if(data1!=0&&futemp!=0&&data2==0) //输入的是符号,接着显示data1 ledplay(data1);
else if(data1!=0&&futemp!=0&&data2!=0) //显示的是data2 ledplay(data2); TH0=(65536-100)/256; TR0=1; }
void denghou() {
data2=0; futemp=0; }
unsigned char KeyTemp,CheckValue,Key1=0x00,Key2=0x00; void Dispose() {
unsigned char Val; if(Line==0x01) Line=0;
else if(Line==0x02) Line=1;
else if(Line==0x04) Line=2;
else if(Line==0x08) Line=3; if(Row==0x10) Row=0;
else if(Row==0x20) Row=1;
else if(Row==0x40) Row=2;
else if(Row==0x80) Row=3; Val=Line*4+Row; if(Val<10){ //存数
if(futemp==0){ //存在data1中 if(data1==0) data1=Val; else
data1=data1*10+Val; }
else{ //存在data2中 if(data2==0) data2=Val; else
data2=data2*10+Val; } }
else if(Val>9&&Val<16){ if(Val!=13&&Val!=12)
futemp=Val; //存符号并进行响应处理 else if(Val==12){ data1=0; data2=0; futemp=0; }
else if(Val==13){ if(futemp==10){ data1=data1*data2; denghou(); }
else if(futemp==11){ data1=data1/data2; denghou(); }
else if(futemp==14){ data1=data1-data2; denghou(); }
else if(futemp==15){ data1=data1+data2; denghou(); } else{ while(1){ P2=0xff; P0=0x00; } } } } else while(1){ P2=0xff; P0=0x00;
} //显示错误 }
unsigned char GetKey1(void) {
P1=0x0F;
CheckValue=P1^0x0F; if(CheckValue==0x00) return 0x00;
else if(CheckValue==0x01) Line=CheckValue; else if(CheckValue==0x02) Line=CheckValue; else if(CheckValue==0x04) Line=CheckValue; else if(CheckValue==0x08) Line=CheckValue; Key1=0x0F; return Key1;