/* 定时器0中断初始化函数 */
void Timer0Init() //1毫秒@12.000MHz
{
//AUXR &= 0x7F; //定时器时钟12T模式
TMOD = 0x01; //设置计数模式
TL0 = (65535-50000) % 256; //设置定时初始值
TH0 = (65535-50000) / 256; //设置定时初始值
ET0 = 1;
EA = 1;
TR0 = 1;
}
//定时器0使用函数
unsigned char fen = 0;
unsigned char miao = 0;
unsigned char haomiao = 0;
void ServiceTimer0() interrupt 1
{
TL0 = (65535-50000) % 256;
TH0 = (65535-50000) / 256;
haomiao++;
if(haomiao==20){
miao++;
haomiao=0;
if(miao==60){
fen++;
miao=0;
}
if(fen==99){
fen=0;
}
}
}
void showing(){ // 秒表计时
ShowSMG(fen/10,1);
Delay100ms(2);
ShowSMG(fen%10,2);
Delay100ms(2);
ShowSMG(16,3);
Delay100ms(2);
ShowSMG(miao/10,4);
Delay100ms(2);
ShowSMG(miao%10,5);
Delay100ms(2);
ShowSMG(16,6);
Delay100ms(2);
ShowSMG(haomiao/10,7);
Delay100ms(2);
ShowSMG(haomiao%10,8);
Delay100ms(2);
}
void main(){
Timer0Init();
L1 = 1;
while(1){
showing();
}
}
【蓝桥杯单片机06】一个简单的定时器应用(秒表计时)
于 2025-04-04 14:07:20 首次发布