精確到千分之一秒的單片機(jī)秒表程序(帶仿真)
#include
#include
#define uint unsigned int
#define uchar unsigned char
sbit duxa=P2^6;
sbit wexa=P2^7;
sbit k1=P1^2;
sbit k2=P1^3;
sbit k3=P1^4;
uchar temp,qian,bai,shi,ge;
uint num,a,shu;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void init();
void delay(uint z);
void display(uchar num,uchar qian,uchar bai,uchar shi,uchar ge);
void main()
{
init();
while(1)
{
display(num,qian,bai,shi,ge);
if(k1==0)
{
delay(1);
TR0=0;
delay(1);
}
if(k2==0)
{
delay(1);
TR0=1;
delay(1);
}
if(k3==0)
{
delay(1);
a=0;
num=0;
TR0=1;
delay(1);
}
}
}
void init()
{
num=0;
temp=0xf0;
TMOD=0x01;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
EA=1;
ET0=1;
TR0=1;
bai=0;
shi=0;
ge=0;
a=0;
k1=1;
k2=1;
k3=1;
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void timer0() interrupt 1
{
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
a++;
if(a==9999)
{
a=0;
num++;
if(num==10)
num=0;
}
shu=a;
qian=shu/1000;
bai=shu%1000/100;
shi=shu%100/10;
ge=shu%10;
}
void display(uchar num,uchar qian,uchar bai,uchar shi,uchar ge)
{
wexa=1;
P0=0xfe;
wexa=0;
P0=0;
duxa=1;
P0=table[num];
duxa=0;
P0=0xff;
delay(1);
wexa=1;
P0=0xfd;
wexa=0;
P0=0;
duxa=1;
P0=table[qian];
duxa=0;
P0=0xff;
delay(1);
wexa=1;
P0=0xfb;
wexa=0;
P0=0;
duxa=1;
P0=table[bai];
duxa=0;
P0=0xff;
delay(1);
wexa=1;
P0=0xf7;
wexa=0;
P0=0;
duxa=1;
P0=table[shi];
duxa=0;
P0=0xff;
delay(1);
wexa=1;
P0=0xef;
wexa=0;
P0=0;
duxa=1;
P0=table[ge];
duxa=0;
P0=0xff;
delay(1);
}
評論