用Keil uVision4写一个程序

2025-01-31 16:07:28
推荐回答(2个)
回答1:

#include 
#define LED P2
unsigned char time_flag;
void InitTimer0(void)
{
TMOD = 0x01;
TH0 = 0x3C;
TL0 = 0x0B0;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void main(void)
{
InitTimer0();
while(1)
{
if(time_flag==100) time_flag =0;
switch(time_flag/10)
{
case 0: LED = 0xfc;break;
case 1:
case 2: LED = 0xf3;break;
case 3:
case 4:
case 5: LED = 0xcf;break;
case 6:
case 7:
case 8:
case 9: LED = 0x3f;break;
default:LED = 0xff;
}
}
}
void Timer0Interrupt(void) interrupt 1
{
TH0 = 0x3C;
TL0 = 0x0B0;
//add your code here!
time_flag++;
}

晶振12M。定时器是50ms中断一次。

回答2:

用中断吧