From 32de81b3f5d8381a08169649fe27839a2e43dbf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=A1=E5=9D=82=E6=98=B4?= Date: Thu, 24 Apr 2025 18:44:28 +0800 Subject: [PATCH] archieve: homework5 --- homework5/.eide/eide.json | 5 +-- homework5/src/test1.c | 17 ++++++---- homework5/src/test2.c | 2 ++ homework5/src/test3.c | 5 +-- homework5/src/test4.c | 70 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 11 deletions(-) create mode 100644 homework5/src/test4.c diff --git a/homework5/.eide/eide.json b/homework5/.eide/eide.json index ff0053b..4d435c9 100644 --- a/homework5/.eide/eide.json +++ b/homework5/.eide/eide.json @@ -20,8 +20,9 @@ "Debug": { "excludeList": [ "src/test.c", - "src/test1.c", - "src/test2.c" + "src/test2.c", + "src/test3.c", + "src/test4.c" ], "toolchain": "Keil_C51", "compileConfig": { diff --git a/homework5/src/test1.c b/homework5/src/test1.c index 2a2f797..7874232 100644 --- a/homework5/src/test1.c +++ b/homework5/src/test1.c @@ -5,17 +5,20 @@ #include "homework5.h" unsigned char LedBuff[6] = { 0xFF, 0XFF, 0XFF, 0XFF, 0XFF, 0XFF}; -unsigned char i = 0; +unsigned char i = 0; +unsigned char display_id[6] = {1, 3, 0, 0, 7, 1}; void test1() { // display the number on the Seg. - LedBuff[5] = LedChar[1]; - LedBuff[4] = LedChar[3]; - LedBuff[3] = LedChar[0]; - LedBuff[2] = LedChar[0]; - LedBuff[1] = LedChar[7]; - LedBuff[0] = LedChar[1]; + // 5 is high digit. + // 0 is low digit. + LedBuff[5] = LedChar[display_id[0]]; + LedBuff[4] = LedChar[display_id[1]]; + LedBuff[3] = LedChar[display_id[2]]; + LedBuff[2] = LedChar[display_id[3]]; + LedBuff[1] = LedChar[display_id[4]]; + LedBuff[0] = LedChar[display_id[5]]; while (1) { /* * seg id on 38 transistors: diff --git a/homework5/src/test2.c b/homework5/src/test2.c index 10c81bd..413bf6f 100644 --- a/homework5/src/test2.c +++ b/homework5/src/test2.c @@ -69,6 +69,8 @@ void test2() } } // log the number in the buffer. + // 5 is high digit. + // 0 is low digit. LedBuff[5] = LedChar[hour / 10]; // log the hour tens. LedBuff[4] = LedChar[hour % 10]; // log the hour units. LedBuff[3] = LedChar[minute / 10]; // log the minute tens. diff --git a/homework5/src/test3.c b/homework5/src/test3.c index a02d0e1..4bd16f0 100644 --- a/homework5/src/test3.c +++ b/homework5/src/test3.c @@ -13,10 +13,9 @@ * 7. msksbr NB!!! * 8. msksbr NB!!! */ -*/ #include "homework5.h" - unsigned char LedBuff[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // buffer to store the seg chars. +unsigned char LedBuff[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // buffer to store the seg chars. void test3() { @@ -67,6 +66,8 @@ void test3() } // log the number in the buffer. // only log the valid digits. + // 5 is high digit. + // 0 is low digit. LedBuff[5] = hour >= 10 ? LedChar[hour / 10] : 0XFF; // log the hour tens. LedBuff[4] = hour > 0 ? LedChar[hour % 10] : 0XFF; // log the hour units. LedBuff[3] = (minute >= 10 || hour > 0) ? LedChar[minute / 10] : 0XFF; // log the minute tens. diff --git a/homework5/src/test4.c b/homework5/src/test4.c new file mode 100644 index 0000000..c563020 --- /dev/null +++ b/homework5/src/test4.c @@ -0,0 +1,70 @@ +// test4.c +// 写一个从999999开始倒计时的秒表。并且改用定时器T1的中断来完成 +// make a stopwatch that counts down from 999999. and use interrupt to finish it. + +#include "homework5.h" +unsigned char LedBuff[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // buffer to store the seg chars. +volatile unsigned char i = 0; // to current digit. +volatile unsigned long display_time = 999999; // to store the time to display. +unsigned int cnt = 0; // to count interrupt. + +// init timer +void initTimer(void) +{ + TMOD &= 0x0F; // clear TMOD. + TMOD |= 0x10; // set TMOD to mode 1. + TH1 = 0xFC; // 1ms timer. + TL1 = 0x18; + ET1 = 1; // enable timer 1 interrupt. + EA = 1; // enable global interrupt. + TR1 = 1; // start timer 1. +} + +// timer1 interrupt service routine. +void timer1ISR(void) interrupt 3 +{ + TH1 = 0xFC; // reset timer. + TL1 = 0x18; + + // display the number. + set38Transistors(i); + P2 = LedBuff[i]; + i = (i + 1) % 6; // to switch to the next digit. + + // 1s timer. + if (++cnt >= 1000) { // if reached 1s. + cnt = 0; + if (display_time > 0) { + display_time--; // decrease the time. + } + } +} + +void test4() +{ + initTimer(); + while (1) { + // split the time into 6 digits. + unsigned char first_digit = display_time / 100000; // to get the first digit. + unsigned char second_digit = (display_time % 100000) / 10000; // to get the second digit. + unsigned char third_digit = (display_time % 10000) / 1000; // to get the third digit. + unsigned char fourth_digit = (display_time % 1000) / 100; // to get the fourth digit. + unsigned char fifth_digit = (display_time % 100) / 10; // to get the fifth digit. + unsigned char sixth_digit = display_time % 10; // to get the sixth digit. + + // load the number in the buffer. + // 5 is high digit. + // 0 is low digit. + LedBuff[5] = LedChar[first_digit]; // to display the first digit. + LedBuff[4] = LedChar[second_digit]; // to display the second digit. + LedBuff[3] = LedChar[third_digit]; // to display the third digit. + LedBuff[2] = LedChar[fourth_digit]; // to display the fourth digit. + LedBuff[1] = LedChar[fifth_digit]; // to display the fifth digit. + LedBuff[0] = LedChar[sixth_digit]; // to display the sixth digit. + } +} + +void main() +{ + test4(); +} \ No newline at end of file