此程序模仿一般的电子表的功用,完成了时,分,秒的显现,并有调表功用。程序简略,很多当地并没有留意细节,仅仅完成了部分功用。
//数码管为8位七段共阳数码管,数据端接在P0口,挑选端接P2口
//外部中止0,挑选调时,分或秒,并闪耀,外部中止1,闪耀位加1,完成调表
//注:些程序仅仅模仿,并无准确校准
#include
unsigned char LED7Code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0x0c6,0xa1,0x86,0x8e,
0xbf, //‘-’
}; //七段共阳数码管的编码0-f
unsigned char LED7Addr[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //数码管的地址
unsigned char flag=0;
unsigned char loop=0;
unsigned char second=0;
unsigned char minute=0;
unsigned char hour=0;
void delay(unsigned int n) //n=10000,延时90.05毫秒
{
while(n–);
}
void display(unsigned char addr,unsigned char LEDCode) //通用显现函数
{
P2=LED7Addr[addr]; //P2送数码管的地址
P0=LED7Code[LEDCode]; //P0送要显现的数据
delay(500); //延时
}
void INT_EX0() interrupt 0
{
delay(2000);
if (!P3_2)
{
if(flag++》=3) flag=0;
}
}
void INT_EX1() interrupt 2
{
delay(2000);
if (!P3_3)
{
if(flag==1) second++;
if(flag==2) minute++;
if(flag==3) hour++;
}
}
void INT_T0() interrupt 1 //守时器0
{
TL0=0xb0; //装入初值,15536,守时50000us
TH0=0x3c;
loop++;
}
void main()
{
TMOD=0x01; //守时器0,工作方法2,16位守时器
TL0=0xb0; //装入初值,15536,守时50000us
TH0=0x3c;
ET0=1;
TR0=1;
EX0=1;
EX1=1;
IT0=1; //跳变方法
IT1=1;
EA=1;
while (1)
{
if(loop》=20)
{
second++;
loop=0;
}
if (second》=60)
{
second=0;
minute++;
}
if (minute==60)
{
minute=0;
hour++;
}
if (!(flag==3&&loop《10))
{
display(0,hour/10);
display(1,hour%10);
}
display(2,16);
if (!(flag==2&&loop《10))
{
display(3,minute/10);
display(4,minute%10);
}
display(5,16);
if (!(flag==1&&loop《10))
{
display(6,second/10);
display(7,second%10);
}
}
}