/* =============== How to use the timer interrupt ---- tf0 (TF1) value hardware reset method ========
1. Total disconnection
Second, enable the timer to interrupt
Third: Set the special function register tmod and configure the working mode.
Fourth, set the initial values of the count registers th0 and tl0.
Fifth: Set tcon and set the timer to start counting through tr0 to 1.*/
// LED is controlled by a transistor and 73hc138
// Use the timer 0 to implement the 1 second delay between the light and off
# Include <reg52.h>
Sbit led2 = P0 ^ 0;
Sbit addr0 = p1 ^ 0;
Sbit addr1 = p1 ^ 1;
Sbit addr2 = p1 ^ 2;
Sbit addr3 = p1 ^ 3;
Sbit enled1 = p1 ^ 4;
Unsigned char time0;
Int main ()
{
Addr0 = 0, addr1 = 1, addr2 = 1, addr3 = 1, enled1 = 0; // 38 I/O status when the decoder controls led2 (schematic)
Time0 = 0;
Led2 = 1; // keep led2 in the non-interrupted state (omitted)
Ea = 1; // total disconnection
Et0 = 1; // enable the timer to interrupt
Tr0 = 1; // start the timer 0
Tmod = 0x01; // timer 0, working mode 1
Th0 = 0xb8; tl0 = 0x00; // set the initial value for loading when the timer is 20 ms
While (1) // large loop of the main program
{
If (time0 = 50) // judge time0. If the value of time0 is 50, it indicates that 50 interruptions have been performed (from the scheduled time of 1 second)
{
Time0 = 0; // reset time0 and re-timer
Led2 = ~ Led2; // led2 is reversed once every 1 second to enable 1 second of flashing.
}
}
Return 0;
}
Void et0 () interrupt 1
{
Th0 = 0xb8; tl0 = 0x00; // if it can be interrupted, it indicates that the time has reached 20 ms, so the initial value of the reload
Time0 ++; // every time an interruption occurs, time0 plus 1
}
Daddy's amateur MCU: How to Use the timer interrupt -- enter the tf0 (TF1) value for automatic hardware reset