实现数码管自加的代码:
#include
#define uchar unsigned char
#define uint unsigned int
uchar code table[16] = {0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,0x7f,
0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //显示数字表
uchar flag,bai,shi,ge;
uint temp;
void xunhuan();
void delay(uint z);
void init();
void main()
{
init();
while(1)
{
if(flag==20)
{
flag=0;
temp++;
if(temp==1000)
{
temp=0;
}
}
bai=temp/100;
shi=temp%100/10;
ge=temp%10;
xunhuan();
}
}
void xunhuan()
{
P2=0;
P0=table[bai];
delay(30);
P2=1;
P0=table[shi];
delay(30);
P2=2;
P0=table[ge];
delay(30);
}
void init()
{
TMOD=0x01; //定时器0
TH0=(65536-50000)/256; //定时50ms
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256; //定时50ms,1m=1000ms=20*50ms
TL0=(65536-50000)%256;
flag++;
}
void delay(uint z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}