问题是按键时第一次和第二次按下时正常,按下第三个时一下子出3~4个相同的数求
单片机源程序如下:
- #include<reg51.h>
- typedef unsigned int u16;
- typedef unsigned char u8;
- unsigned char code xianshi[16]={'*','0','#','=','7','8','9','/','4','5','6','-','1','2','3','+'};
- unsigned char code shu[16]={0,0,0,0,7,8,9,0,4,5,6,0,1,2,3,0};
- unsigned char code table[]={'0','1','2','3','4','5','6','7','8','9'};
- #define anjian P1
- #define LCD_D P2
- sbit LCD_RS=P3^0;
- sbit LCD_RW=P3^1;
- sbit LCD_E=P3^2;
- u8 jianwei,i=0,jieguo;
- u8 zancun[4];
- void delay(u16 i)
- {
- while(i--);
- }
- void delayms(u16 c) //延时函数ms 误差 0us
- {
- u8 a,b;
- for (; c>0; c--)
- {
- for (b=199;b>0;b--)
- {
- for(a=1;a>0;a--);
- }
- }
- }
- void dat(u8 d) //写入数据
- {
- LCD_E=0; //关闭使能端
- LCD_RW=0; //进入 写
- LCD_RS=1; //选择 数据
- LCD_D=d; //输入数据
- delayms(1); //等待数据稳定
- LCD_E=1; //打开使能端
- delayms(5); //延时1ms
- LCD_E=0; //关闭使能端
- }
- void junzhenanjian()
- {
- u8 a;
- anjian=0x0f;
- if(anjian!=0x0f)
- {
- delay(1000); //消抖
- if(anjian!=0x0f)
- {
- anjian=0x0f;
- switch(anjian)
- {
- case(0x07): jianwei=0;break;
- case(0x0b): jianwei=1;break;
- case(0x0d): jianwei=2;break;
- case(0x0e): jianwei=3;break;
- }
- anjian=0xf0;
- switch(anjian)
- {
- case(0x70): jianwei=jianwei; break;
- case(0xb0): jianwei=jianwei+4; break;
- case(0xd0): jianwei=jianwei+8; break;
- case(0xe0): jianwei=jianwei+12;break;
- }
- while((a<50)&&(anjian!=0xf0))
- {
- delay(1000);
- a++;
- }
- dat(xianshi[jianwei]);
- zancun[i]=jianwei;
- i++;
- }
- }
- }
- void com(u8 c) //写入命令
- {
- LCD_E=0; //关闭使能端
- LCD_RW=0; //进入 写
- LCD_RS=0; //选择 命令
- LCD_D=c; //输入命令
- delayms(1); //等待数据稳定
- LCD_E=1; //打开使能端
- delayms(5); //延时1ms
- LCD_E=0; //关闭使能端
- }
- void qingping() //清屏
- {
-
- com(0x06); //光标右移
- com(0x0c); //显示光标
- com(0x38); //8线 1行 5×10
- com(0x01); //清屏
- com(0x80); //设置起点
- }
- void main()
- {
- u8 shi,ge;
- qingping();
- while(1)
- {
- junzhenanjian();
- if(i==4)
- {
- switch(zancun[1])
- {
- case(0):jieguo=(shu[zancun[0]]*shu[zancun[2]]);break;
- case(7):jieguo=(shu[zancun[0]]/shu[zancun[2]]);break;
- case(11):jieguo=(shu[zancun[0]]-shu[zancun[2]]);break;
- case(15):jieguo=(shu[zancun[0]]+shu[zancun[2]]);break;
- }
- if(jieguo>=10)
- {
- shi=jieguo/10;
- ge=jieguo-shi*10;
- dat(table[shi]);
- dat(table[ge]);
- }
- else
- dat(table[jieguo]);
- i=0;
- }
- }
- }
复制代码
|