本帖最后由 巴豆定心 于 2012-10-30 11:14 编辑
程序说明: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;}
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
&与&&有什么不一样吗?
一周热门 更多>