The idea of using timer interrupt in 51 single-chip microcomputer 1

1. Turn on global interrupt EA = 1 
2. Corresponding interrupt enable such as timer 1 ET1 = 1, timer 0 ET0 = 1
3. Timer working mode configuration: There are three ways, dual 8-bit timing, 16-bit timing and capture mode There are 4 types of timer 2. TMOD is a register configured by timer 0 and timer 1. TMOD = 0x01, timer 0 works in mode 1 (16-bit counting)
4. Turn on timer TR0 = 1; timing When device 1 is turned on, TR1 = 1;
5. Interrupt service routine : It is the transaction you want to process when the time is up.
Example: //50ms timer
void main(void)
{
TMOD = 0x01; //Configuration mode
TH0 = 0x3C; //Counting time
TL0 = 0xB0;
TR0 = 1; //Enable timer
EA = 1; //Enable global interrupt
ET0 = 1; //Turn on timer 0 interrupt
for(;;)
}

void Timer0_ISR(void) intterupt 1 // Interrupt service routine
{
TH0 = 0x3C; //Count time
TL0 = 0xB0;

P1 = 0x11; //The timing is up, the action to be performed
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325864075&siteId=291194637