/************************************************************* 功能: 实现电子时钟显示(C) 定义: P3口位选,P1口段选,P0.0,P0.1,P0.2为调时口 时间: 2010—11—26 说明: 仿真存在问题,这可能是仿真软件的问题,需要将段位扫描反调 实际运行的结果是正确的 *************************************************************/ #include <AT89X51.H> unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00}; unsigned char dispbitcode[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; unsigned char dispbuf[8]={0,0,16,0,0,16,0,0}; unsigned char dispbitcnt; unsigned char second; unsigned char minite; unsigned char hour; unsigned int tcnt; unsigned char mstcnt; unsigned char i,j; void main(void) { TMOD=0x02; TH0=0x06; TL0=0x06; TR0=1; ET0=1; EA=1; while(1) { if(P0_0==0) { for(i=5;i>0;i--) for(j=248;j>0;j--); if(P0_0==0) { second++; if(second==60) { second=0; } dispbuf[0]=second%10; dispbuf[1]=second/10; while(P0_0==0); } } if(P0_1==0) { for(i=5;i>0;i--) for(j=248;j>0;j--); if(P0_1==0) { minite++; if(minite==60) { minite=0; } dispbuf[3]=minite%10; dispbuf[4]=minite/10; while(P0_1==0); } } if(P0_2==0) { for(i=5;i>0;i--) for(j=248;j>0;j--); if(P0_2==0) { hour++; if(hour==24) { hour=0; } dispbuf[6]=hour%10; dispbuf[7]=hour/10; while(P0_2==0); } } } } void t0(void) interrupt 1 using 0 { mstcnt++; if(mstcnt==8) { mstcnt=0; P1=dispcode[dispbuf[dispbitcnt]]; P3=dispbitcode[dispbitcnt]; dispbitcnt++; if(dispbitcnt==8) { dispbitcnt=0; } } tcnt++; if(tcnt==4000) { tcnt=0; second++; if(second==60) { second=0; minite++; if(minite==60) { minite=0; hour++; if(hour==24) { hour=0; } } } dispbuf[0]=second%10; dispbuf[1]=second/10; dispbuf[3]=minite%10; dispbuf[4]=minite/10; dispbuf[6]=hour%10; dispbuf[7]=hour/10; } } /************************************************************* 功能: 实现电子时钟显示(C) 定义: P2口位选,P0口段选 时间: 2010—11—26 *************************************************************/ #include<reg52.h> code unsigned char tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; unsigned char StrTab[6]; unsigned char minute=30,hour=12,second=0; void Displaypro(void) { StrTab[0]=tab[second%10]; StrTab[1]=tab[second/10]; StrTab[2]=tab[minute%10]; StrTab[3]=tab[minute/10]; StrTab[4]=tab[hour%10]; StrTab[5]=tab[hour/10]; } main() { TMOD |=0x01; TH0=0xd8; TL0=0xf0; ET0=1; TR0=1; TMOD |=0x10; TH1=0xF8; TL1=0xf0; ET1=1; TR1=1; EA =1; while(1){;} } void time1_isr(void) interrupt 3 { static unsigned char num; TH1=0xf8; TL1=0xf0; switch (num) { case 0:P2=0x01;P0=StrTab[num];break; case 1:P2=0x02;P0=StrTab[num];break; case 2:P2=0x04;P0=StrTab[num];break; case 3:P2=0x08;P0=StrTab[num];break; case 4:P2=0x10;P0=StrTab[num];break; case 5:P2=0x20;P0=StrTab[num];break; default:break; } num++; if(num==6) num=0; } void tim(void) interrupt 1 { static unsigned char count; TH0=0xd8; TL0=0xf0; count++; switch (count) { case 0: case 20: case 40: case 60: case 80:Displaypro();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; } } } } //源代码下载 :http://www.51hei.com/f/szc511.rar