// test3.c // 通过定时器T1中断,实现三位数秒表 // make a three-digit stopwatch by timer T1 interrupt. #include "homework6.h" unsigned char digits[3] = {0, 0, 0}; // store the three digits of the stopwatch. unsigned char displayPos = 0; // store the position of the display. void test3() { TMOD = 0x10; // set the timer 1 to mode 1. TH1 = 0xFC; // set the timer 1 to 1s. TL1 = 0x18; // set the timer 1 to 1s. TR1 = 1; // start the timer 1. ET1 = 1; // enable the timer 1 interrupt. EA = 1; // enable the interrupt. while (1); } void Timer1_ISR() interrupt 3 { static unsigned int counter = 0; // counter to count the number of interrupts. TH1 = 0xFC; // reset the timer 1. TL1 = 0x18; // reset the timer 1. set38(displayPos); // set the 38 transistors to the address. P2 = LedChar[digits[2 - displayPos]]; // display the digit. displayPos++; // switch to the next digit. if (displayPos >= 3) displayPos = 0; // if the display position is 3, reset the display position to 0. if (++counter >= 1000) { counter = 0; // reset the counter. if (++digits[2] >= 10) { digits[2] = 0; // reset the third digit. if (++digits[1] >= 10) { digits[1] = 0; // reset the second digit. if (++digits[0] >= 10) { digits[0] = 0; // reset the first digit. } } } } // reset the stopwatch when the stopwatch is 999. if (digits[0] == 9 && digits[1] == 9 && digits[2] == 9) { digits[0] = 0; digits[1] = 0; digits[2] = 0; } } void main() { test3(); }