您好,欢迎访问三七文档
#includereg52.h#includedelay.h#includeLCD1602.h#includeKEY.h#defineucharunsignedchar#defineuintunsignedintucharnum;//存放按键标识uchardenghao=0,flag=0,fuhao,key;//denghao存放是否按下等号键,flag存放是否按下符号键,fuhao存放按下是什么符号,key存放按键ASCII码值longa,b,c;//加数a、b结果为cucharcodetable[]={//键盘显示编码,加上0x30,为对应的ASCII码0x2f-0x307,8,9,0x2f-0x30,//1、2、3、+;0x2a-0x304,5,6,0x2a-0x30,//4、5、6、-;0x2d-0x301,2,3,0x2d-0x30,//7、8、9、*;0x2b-0x300x2f-0x30,0x2a-0x30,0,0x2b-0x30};//0、=、CE、/;0x01-0x30,0,0x3d-0x30,0x2f-0x30};//0、=、CE、/;voidkeyscan(){uchartemp;KEY_8=0xfe;//11111110temp=KEY_8;temp=temp&0xf0;while(temp!=0xf0){delay_nms(20);temp=KEY_8;temp=temp&0xf0;while(temp!=0xf0){temp=KEY_8;switch(temp){case0xee:num=0;break;//11101110case0xde:num=1;break;//11011110case0xbe:num=2;break;//10111110case0x7e:num=3;break;//01111110}while(temp!=0xf0)//等待按键释放{temp=KEY_8;temp=temp&0xf0;}}if(num==0||num==1||num==2)//判断是按下1,2,3{if(flag==0)//如果没有按符号键,符号前的数值为aa=a*10+table[num];elseif(flag==1)//如果按了符号键,符号后的数值为bb=b*10+table[num];if(denghao==1)//如果之前按了'='号,再按键时清屏,进行下一次计算{denghao=0;LCD_init();LCD_write_str(0,0,JIsuanqitest);write_com(0x80+0x40);//清屏}}elseif(num==3)//判断是否按下+{flag=1;fuhao=4;}key=table[num];//显示按下的键write_dat(0x30+key);}KEY_8=0xfd;//11111101temp=KEY_8;temp=temp&0xf0;while(temp!=0xf0){delay_nms(20);;temp=KEY_8;temp=temp&0xf0;while(temp!=0xf0){temp=KEY_8;switch(temp){case0xed:num=4;break;//11101101case0xdd:num=5;break;//11011101case0xbd:num=6;break;//10111101case0x7d:num=7;break;//01111101}while(temp!=0xf0)//等待按键释放{temp=KEY_8;temp=temp&0xf0;}}if(num==4||num==5||num==6)//判断是按下4,5,6{if(denghao==1)//如果之前按了'='号,再按键时清屏,进行下一次计算{denghao=0;LCD_init();LCD_write_str(0,0,JIsuanqitest);write_com(0x80+0x40);//清屏}if(flag==0)//如果没有按符号键,符号前的数值为aa=a*10+table[num];elseif(flag==1)//如果按了符号键,符号后的数值为bb=b*10+table[num];}elseif(num==7)//判断是否按下-{flag=1;fuhao=3;}key=table[num];//显示按下的键write_dat(0x30+key);}KEY_8=0xfb;//11111011temp=KEY_8;temp=temp&0xf0;while(temp!=0xf0){delay_nms(20);temp=KEY_8;temp=temp&0xf0;while(temp!=0xf0){temp=KEY_8;switch(temp){case0xeb:num=8;break;//11101011case0xdb:num=9;break;//11011011case0xbb:num=10;break;//10111011case0x7b:num=11;break;//01111011}while(temp!=0xf0){temp=KEY_8;temp=temp&0xf0;}}if(num==8||num==9||num==10)//判断是按下7,8,9{if(denghao==1)//如果之前按了'='号,再按键时清屏,进行下一次计算{denghao=0;LCD_init();LCD_write_str(0,0,JIsuanqitest);write_com(0x80+0x40);//清屏}if(flag==0)a=a*10+table[num];//如果没有按符号键,符号前的数值为aelseif(flag==1)b=b*10+table[num];//如果按了符号键,符号后的数值为b}elseif(num==11)//判断是否按下*{flag=1;fuhao=2;}key=table[num];write_dat(0x30+key);}KEY_8=0xf7;//11110111temp=KEY_8;temp=temp&0xf0;while(temp!=0xf0){delay_nms(20);temp=KEY_8;temp=temp&0xf0;while(temp!=0xf0){temp=KEY_8;switch(temp){case0xe7:num=12;break;//11100111case0xd7:num=13;break;//11010111case0xb7:num=14;break;//10110111case0x77:num=15;break;//01110111}while(temp!=0xf0){temp=KEY_8;temp=temp&0xf0;}}switch(num){case13://按0键{if(denghao==1)//如果之前按了'='号,再按键时清屏,进行下一次计算{denghao=0;LCD_init();LCD_write_str(0,0,JIsuanqitest);write_com(0x80+0x40);//清屏}if(flag==0)a=a*10;elseif(flag==1)b=b*10;write_dat(0x30);}break;case14://按=键(+、-、*、/、){denghao=1;if(fuhao==1)//如果符号键是+,执行+运算{write_com(0x80+0x4f);//使结果从屏幕最右下角显示出来write_com(0x04);//从右到左显示c=a+b;while(c!=0){write_dat(0x30+c%10);c=c/10;}write_dat(0x3d);//显示等号=fuhao=0;a=0;b=0;flag=0;}if(fuhao==2)//如果符号键是-,执行-运算{write_com(0x80+0x4f);//使结果从屏幕最右下角显示出来write_com(0x04);if(a=b){c=a-b;while(c!=0){write_dat(0x30+c%10);c=c/10;}}elseif(ab){c=b-a;while(c!=0){write_dat(0x30+c%10);c=c/10;}write_dat(0x2d);//显示负号}write_dat(0x3d);//显示等号=a=0;b=0;flag=0;fuhao=0;}if(fuhao==3)//如果符号键是*,执行*运算{write_com(0x80+0x4f);//使结果从屏幕右下角显示出来write_com(0x04);c=a*b;while(c!=0){write_dat(0x30+c%10);c=c/10;}write_dat(0x3d);//显示等号=a=0;b=0;flag=0;fuhao=0;}if(fuhao==4)//如果符号键是/,执行/运算{key=0;//小数位数write_com(0x80+0x4f);write_com(0x04);if(b!=0)c=(long)(((float)a/b)*10000);//结果保留4位小数elseLCD_write_str(1,15,rorre);//显示错位while(c!=0){write_dat(0x30+c%10);c=c/10;key++;if(key==4)//显示完六位小数后,显示小数点write_dat(0x2e);//显示小数点}if(a/b=0){write_dat(0x30);//显示0}write_dat(0x3d);//显示等号a=0;b=0;flag=0;fuhao=0;}}break;case12:{LCD_init();LCD_write_str(0,0,JIsuanqitest);write_com(0x80+0x40);//清屏a=0;b=0;flag=0;fuhao=0;}break;case15:{flag=1;fuhao=1;write_dat(0x30+table[num]);}break;}}}voidjisuanqi_main(){keyscan();}voidint1()interrupt2{EX1=0;//关外部中断1//music_delay1(1);//去抖EX1=1;//开外部中断1}
本文标题:键盘
链接地址:https://www.777doc.com/doc-5559075 .html