電子時(shí)鐘的配套c程序
/*------------------------------------------------------------------------------
聲明:
①使用共陽數(shù)碼管,選位碼12、9、8、6接P3^0 ~ P3^3,如0xaa=1010 1010,左至右為0101
②中間兩小數(shù)點(diǎn),點(diǎn)亮選3、8
③a、b、c、d、e、f、g、h 分別接P1口個(gè)管腳
④任意8位二進(jìn)制,如1100 0000,在共陽上顯示0
⑤P2^2口接一個(gè)發(fā)光二極管
⑥P0^0和P0^4分別接了兩個(gè)按鍵
-------------------------------------------------------------------------------*/
#includereg52.h>
sbit KEY1=P0^0;
sbit KEY2=P0^4;
sbit LED=P2^2;
code unsigned char tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char StrTab[4];
unsigned char minute=30,hour=20,second; //初始化為20:30
void delay(unsigned int cnt)
{
while(--cnt);
}
void Displaypro(void) //顯示處理函數(shù)
{
StrTab[0]=tab[hour/10]; //顯示小時(shí)
StrTab[1]=tab[hour%10];
StrTab[2]=tab[minute/10]; //顯示分鐘
StrTab[3]=tab[minute%10];
}
main()
{
TMOD|=0x01; //定時(shí)器0 10ms in 12M crystal 用于計(jì)時(shí)
TH0=0xd8; //(65536-10000)/256
TL0=0xf0; //(65536-10000)%256
ET0=1;
TR0=1;
TMOD|=0x10; //定時(shí)器1 1.8ms in 12M crytal 用于動(dòng)態(tài)掃描
TH1=0xF8; //(65536-1808)/256
TL1=0xF0; //(65536-1808)%256
ET1=1;
TR1=1;
EA=1;
Displaypro(); //調(diào)用顯示處理函數(shù)
while(1)
{
if(!KEY1)
{
delay(10000); //延時(shí)一段時(shí)間(去抖動(dòng))
if(!KEY1)
{
hour++;
if(hour==24)
hour=0;
Displaypro();
}
}
if(!KEY2)
{
delay(10000);
if(!KEY2)
{
minute++;
if(minute==60)
minute=0;
Displaypro();
}
}
}
}
void time1_isr(void) interrupt 3 using 0 //定時(shí)器1用來動(dòng)態(tài)掃描
{
static unsigned char num;
//TH1=0xf8;
//TL1=0xf0; //中斷源每1.8ms向主程序發(fā)一個(gè)脈沖,執(zhí)行中斷程序
TH1=(65536-3600)/256;
TL1=(65536-3600)%256; //不是因?yàn)檫@里出錯(cuò),掃描速度可以
switch(num)
{
case 0: P3=0x01; P1=StrTab[num];break;
case 1: P3=0x02; P1=StrTab[num];break;
case 2: P3=0x04; P1=StrTab[num];break;
case 3: P3=0x08; P1=StrTab[num];break;
default: break;
}
num++;
if(num==4)
num=0;
}
void time(void) interrupt 1 using 1
{
static unsigned char count; //定義內(nèi)部靜態(tài)變量
TH0=0xd8;
TL0=0xf0;
count++;
switch(count)
{
case 0:
case 20:
case 40:
case 60:
case 80: Displaypro(); break; //隔一段時(shí)間調(diào)用顯示處理函數(shù)
case 50: P2=~P2;break; //每1s亮一下
case 100: P2=~P2;break;
default: break;
}
if(count==100)
{
count=0;
second++;
if(second==60)
{
second=0;
minute++;
if(minute==60)
{
minute=0;
hour++;
if(hour==24)
hour=0;
}
}
}
}
評(píng)論