利用51单片机进行时钟模拟,利用T0中断函数进行时钟模拟
#include <reg51.h>
#define uc unsigned char
#define ui unsigned int
uc code led[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char count =0; //计数器赋值
unsigned char t;
bit ldelay=0;
timer0 () interrupt 1 //T0中断函数
{
if(++t==20){
t=0;
ldelay=1; //判断循环是否超限
}
// TH0=0x3c;TL0=0xb0; //重置T0初值
TH0=(25536-5000)/256;
TL0=(25536-5000)%256;
}
void main(){
TMOD=0x01; //设置T0计数方式1
// TH0=0x3c;
// TL0=0xb0; // 设置T0初值,溢出20次大约1S (12MHz)
TH0=(25536-5000)/256; //2的16次方,
TL0=(25536-5000)%256; //设置T0初值,溢出20次大约1S (12MHz)
TR0=1; //启动T0
ET0=EA=1; //开总中断
P0=P1=0x3f;
while(1){
if(ldelay){
ldelay=0;
if(++count==60) count=0;
P0=led[count/10]; //显示十位数
P1=led[count%10]; //显示个位数
}}
}