2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
As shown in the figure: Interrupts also need to configure registers. There are two types of registers, the first is the interrupt control register (IE and XICON), and the second is the priority control register. Here we only study an interrupt of timer T0.
To use the timer T0 interrupt:
ET0 = 1;ET0为定时器T0的中断开关,为1时打开中断
EA = 1; EA为中断源的总开关。
When the set time is up, the program in the interrupt function is executed. So how do you know which function is the interrupt function? — Query the interrupt number (different interrupt sources generate different interrupt numbers)
#include <REGX52.H>
sbit LED1 = P3^7;
int cnt = 0;
void Timer0_Init_10ms(void) //10毫秒@11.0592MHz
{
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x01; //设置定时器模式
TL0 = 0x00; //设置定时初值
TH0 = 0xDC; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
}
void Timer0_interrupt_Init(void)//定时器T0中断初始化
{
ET0 = 1;
EA = 1;
}
void main(void)
{
LED1 = 1;//先让灯熄灭的状态
Timer0_Init_10ms();//打开定时器T0
Timer0_interrupt_Init();//打开定时器T0中断
while(1)
{
}
}
/*定义中断函数*/
void Timer0Hander() interrupt 1
{
TF0 = 0;//软件清零
TL0 = 0x00; //重新给初值
TH0 = 0xDC;
cnt++;
if(cnt == 100)//数100次,相当于1s
{
cnt = 0;
LED1 = !LED1;
}
}
This achieves a 1s interval on and off.