程序如下
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uint sec,counter;
main()
{
TMOD=0x01 //T0定時器 工作方式1
TH0=(65536-50000)/256
TL0=(65536-50000)%256
ET0=1 //T0開中斷
EA=1 //CPU開中斷
TR0=1 //啟動T0
While(1)
{
P2=table[sec/10];
P3=table[sec%10];
}
}
void int1 interrupt 1
{
counter++
if(counter==20)
{
counter=0;
sec++;
if(sec==8) //數字達到8則溢出,重新定義
sec=0;
}
TH0=(65536-50000)/256 //重新賦初值
TL0=(65536-50000)%256
}
|