keitiaoshuzishizhong.c

来自「基于AT89S52的可调数字时钟」· C语言 代码 · 共 92 行

C
92
字号
#include <at89x52.h>
unsigned char code dispcode[]={0xc0,0xf9,0xa4,0xb0, 
                               0x99,0x92,0x82,0xf8, 
                               0x80,0x90,0x00}; 
unsigned char dispbitcode[]={0x01,0x02,0x04,0x08,0x10,0x20};
unsigned char dispbuf[]={0,0,0,0,0,0}  ;
unsigned char mstcnt;
unsigned int tcnt;
unsigned char second;
unsigned char minute;
unsigned char hour;
unsigned char dispbitcnt;
void delay(void)
{
 unsigned char i,j;
 for (i=0;i<20;i++)
 for (j=0;j<248;j++);
}
void main (void)
{
 TMOD=0X02;
 TH0=0X06;
 TL0=0X06;
 TR0=1;
 ET0=1;
 EA=1;
 while (1)
 {
 if (P2_0==0)
  {
   delay();
   if (P2_0==0)
   second++;
   while(P2_0==0);
  }
  if (P2_1==0)
  {
   delay();
   if (P2_1==0)
   minute++;
   while(P2_1==0);
  }
  if (P2_2==0)
  {
   delay();
   if (P2_2==0)
   hour++;
   while(P2_2==0);
  }
 
  dispbuf[0]=hour/10;
  dispbuf[1]=hour%10;
  dispbuf[2]=minute/10;
  dispbuf[3]=minute%10;
  dispbuf[4]=second/10;
  dispbuf[5]=second%10;
 }
}
void t0() interrupt 1
{
 mstcnt++;
 if (mstcnt==4)
 {
  mstcnt=0;	   
  P1=dispcode[dispbuf[dispbitcnt]];
  P2=dispbitcode[dispbitcnt];
  dispbitcnt++;
  if (dispbitcnt==6)
  dispbitcnt=0;
 }
 tcnt++;
 if (tcnt==4000)
 {
  tcnt=0;
  second++;
  if (second==60)
  {
   second=0;
   minute++;
   if (minute==60)
   {
    minute=0;
	hour++;
	if (hour==24)
	{
	 hour=0;
	}
   }
  }
 }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?