本帖最后由 badoudingxin 于 2012-10-30 12:19 编辑
程序说明:用key1与key2组合控制加减计数,按过key1后,再按key2加一计数;
按下key2后,再按下key1减一计数;key3清零。问题是:
仿真过程中出现计数乱跳现象,跪请大侠帮忙调试,谢谢啊!
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar code smg_du[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar code smg_we[]={0x01,0x02,0x04};
uchar buffer[]={0,0,0};
uint k,n;
int M=10;
void csh();
void jia();
void jian();
void zhiling();
sbit key1 = P3^0;
sbit key2 = P3^1;
sbit key3 = P3^2;
void csh()
{
TMOD= 0x01;
TH0=(65536-5000)/256;
TL0=(65536-5000)%256;
EA=1;
ET0=1;
TR0=1;
}
void delay(uint t)
{
uchar j;
while(t--);
for(j=0;j<119;j++);
}
void show()
{
uchar i;
buffer[0]=M%100%10;
buffer[1]=M%100/10;
buffer[2]=M/100;
for(i=0;i<3;i++)
{
P1=smg_du[buffer
];
P2=smg_we;
delay(3);
P2=0x00;
}
}
void main ()
{
csh();
key1=key2=key3=1;
k=1;n=0;
while(1)
{
//jia();
if(k==1&n==0)
{
jia();}else{
jian(); }
zhiling();
}
}
void timer0 () interrupt 1
{
TH0=(65536-5000)/256;
TL0=(65536-5000)%256;
show();
}
void jia()
{
if(key1==0)
{
delay(10);
if(key1==0){k=1;while(!key1);}
}
if(key2==0&k==1)
{
delay(10);
if(key2==0&k==1){k=0;M++;while(!key2);}
}
}
void jian()
{
if(key2==0)
{
delay(10);
if(key2==0){n=1;while(!key2);}
}
if(key1==0&n==1)
{
delay(10);
if(key1==0&n==1){n=0;M--;while(!key1);}
}
}
void zhiling()
{
if(M<0){M=0;}
if(key3==0)
{
delay(10);
if(key3==0){M=0;}
}
}
-
一周热门 更多>