diff --git a/homework6/.eide/eide.json b/homework6/.eide/eide.json index 8d94045..1a60965 100644 --- a/homework6/.eide/eide.json +++ b/homework6/.eide/eide.json @@ -19,7 +19,8 @@ "targets": { "Debug": { "excludeList": [ - "src/test1.c" + "src/test1.c", + "src/test2.c" ], "toolchain": "Keil_C51", "compileConfig": { diff --git a/homework6/homework6.code-workspace b/homework6/homework6.code-workspace index 8c192af..2a97a15 100644 --- a/homework6/homework6.code-workspace +++ b/homework6/homework6.code-workspace @@ -18,7 +18,8 @@ "*.c++": "cpp", "*.cpp": "cpp", "*.cxx": "cpp", - "*.cc": "cpp" + "*.cc": "cpp", + "REG52.H": "cpp" }, "[yaml]": { "editor.insertSpaces": true, diff --git a/homework6/src/homework6.h b/homework6/src/homework6.h index 07af757..1020ccd 100644 --- a/homework6/src/homework6.h +++ b/homework6/src/homework6.h @@ -69,4 +69,5 @@ void set38(unsigned char addr) ADDR0 = addr & 0x01; ADDR1 = (addr & 0x02) >> 1; ADDR2 = (addr & 0x04) >> 2; -} \ No newline at end of file +} +// low seg is the right side. \ No newline at end of file diff --git a/homework6/src/test3.c b/homework6/src/test3.c new file mode 100644 index 0000000..6bf1c74 --- /dev/null +++ b/homework6/src/test3.c @@ -0,0 +1,50 @@ +// 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(); +} \ No newline at end of file