// test2.c // 定时器1中断控制两个LED交替闪烁。设置定时器1的初值,使其每隔25ms产生一次中断。 // 在中断服务程序中,每次中断对两个LED的状态进行切换,一个LED亮时另一个LED灭, // 从而实现交替闪烁 // Timer 1 interrupt to control two LED lights to alternate. // Set the initial value of the timer 1 to make it produce an interrupt every 25ms. // In the interrupt service program, // switch the state of the two LED lights every time an interrupt occurs, // one LED light on at a time, and the other LED light off, // thus implementing alternating flashing. #include "homework6.h" void test2() { // Set the 38 transistors to the address. set38(TO_LED); // init the timer 1. TMOD = 0x9E; // set the timer 1 to mode 1. TH1 = 0x58; // set the timer 1 to 25ms. TL1 = 0x66; // set the timer 1 to 25ms. TR1 = 1; // start the timer 1. ET1 = 1; // enable the timer 1 interrupt. EA = 1; // enable the interrupt. P2 = 0xFC; // init mode: the first LED light. while (1) {} } void Timer1_ISR() interrupt 3 { TH1 = 0x9E; // reset the timer 1. TH1 = 0x58; // reset the timer 1. P2 ^= 0x03; // switch the state of the two LED lights. } void main() { test2(); }