定时器实现时间显示
#include <REGX52.H>
#include "Delay.h"
#include "LCD1602.h"
#include "Timer0.h"
unsigned char h,m,s;
void main(){
LCD_Init();
Timer0Init();
LCD_ShowString(1,1,"time:");
LCD_ShowString(2,3,":");
LCD_ShowString(2,6,":");
while(1){
LCD_ShowNum(2,1,h,2);
LCD_ShowNum(2,4,m,2);
LCD_ShowNum(2,7,s,2);
}
}
//定时器里面不可写复杂逻辑。
void Timer0_Routine() interrupt 1
{
static unsigned int T0Count; //静态全局变量(子程序结束不销毁)
TL0 = 0x18;
TH0 = 0xFC;
T0Count++; //执行一次中断就++
if(T0Count >= 1000){
T0Count = 0;
s++;
if(s>=60){
s = 0;
m++;
if(m>=60){
m = 0;
h++;
if(h>=60){
s=0;
h=0;
m=0;
}
}
}
}
}