自己用單片機(jī)數(shù)碼管做的秒表
/*
實(shí)現(xiàn)功能,數(shù)碼管前五位計(jì)數(shù),顯示時(shí)間的變化,誤差1/1000s
*/
#include
#include
#define uint unsigned int
#define uchar unsigned char
uchar code wetable[]={
0xfe,0xfd,0xfb,0xf7,
0xef,0xdf,0xbf,0x7f};
uchar code dutable[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};//段選
uint closedu,closewe;
uint ge,shi,bai,num;
uchar wenum,count;
uchar doucount,dounum,doufirst,dousecond;
uchar decimal;
void delay(uint z);
void init();
void display(uint bai,uint shi,uint ge,uint doufrist,uint dousecond);
void main()
{
init();
while(1)
{
display(bai,shi,ge,doufirst,dousecond);
}
}
void init()
{
num=0;
dounum=0;
doucount=0;
decimal=0x80;
count=0;
wenum=0;
closedu=0xff;
closewe=0xff;
P1=closewe;//關(guān)閉位選
P0=closedu;//關(guān)閉段選
TMOD=0x01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
EA=1;
ET0=1;
TR0=1;
}
void delay(uint z)//延時(shí)1ms
{
uint x,y;
for(x=z;x>0;x--)
{
for(y=0;y<11;y++)
{
}
}
}
void display(uint bai,uint shi,uint ge,uint doufrist,uint dousecond)
{
closedu=0xff;
closewe=0xff;
wenum=0;
P1=wetable[wenum];
P0=dutable[bai];
delay(1);
P1=closewe;
P0=closedu;
wenum++;
P1=wetable[wenum];
P0=dutable[shi];
delay(1);
P1=closewe;
P0=closedu;
wenum++;
P1=wetable[wenum];
P0=dutable[ge];
delay(1);
P1=closewe;
P0=closedu;
P1=wetable[wenum];
P0=decimal;
delay(1);
P1=closewe;
P0=closedu;
wenum++;
P1=wetable[wenum];
P0=dutable[doufirst];
delay(1);
wenum++;
P1=wetable[wenum];
P0=dutable[dousecond];
delay(1);
wenum=0;
}
void time0() interrupt 1
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
count++;
doucount++;
if(count==100)
{
count=0;
num++;
if(num==1000)
{
num=0;
}
bai=num/100;
shi=num%100/10;
ge=num%10;
}
if(doucount==1)
{
doucount=0;
dounum++;
if(dounum==100)
{
dounum=0;
}
doufirst=dounum/10;
dousecond=dounum%10;
}
}
評(píng)論