作者: 来源:互联网
c51源程序如下:
//说明:利用定时/计数器0方式1,实现数码管显示(时,分,秒)
//作者:Zou Mulin
#include"reg51.h"
#define COUNT 1000
unsigned char LED_seg[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char LED_bit[6]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned LED_buf[8]={0};
unsigned int i=0,aa=00,bb=33,cc=16;
void delay(unsigned int n)
{
for(;n>0;n--);
}
void convert()
{
LED_buf[0]=aa%10;
LED_buf[1]=aa/10;
LED_buf[2]=bb%10;
LED_buf[3]=bb/10;
LED_buf[4]=cc%10;
LED_buf[5]=cc/10;
}
/**********T/C0中断服务程序**********/
void TIMER0_ISR (void) interrupt 1
{
TH0= (65536-COUNT)/256;
TL0= (65536-COUNT)%256; //重新赋计数值
i++;
if(i==1000){aa+=1; i=0;}
if(a==60){aa=0;bb+=1;}
if(b==60){bb=0;cc+=1;}
if(c==24){aa=0;bb=0;cc=0;}
}
/**********主函数*******************/
void main(void)
{
unsigned char j;
TMOD=0x01;
TH0= (65536-COUNT)/256;
TL0= (65536-COUNT)%256;
ET0=1;
EA=1;
TR0=1;
while(1)
{
convert();
for(j=0;j<8;j++)
{
P0=LED_seg[LED_buf[j]];
P1=LED_bit[j];
delay(100);
}
}
}