diff --git a/homework5/.eide/eide.json b/homework5/.eide/eide.json index a837da6..ff0053b 100644 --- a/homework5/.eide/eide.json +++ b/homework5/.eide/eide.json @@ -20,7 +20,8 @@ "Debug": { "excludeList": [ "src/test.c", - "src/test1.c" + "src/test1.c", + "src/test2.c" ], "toolchain": "Keil_C51", "compileConfig": { diff --git a/homework5/src/test3.c b/homework5/src/test3.c new file mode 100644 index 0000000..a02d0e1 --- /dev/null +++ b/homework5/src/test3.c @@ -0,0 +1,83 @@ +// test3.c +// 让数码管动态显示秒表,但只显示有效位,高位的0不显示 +// display the stopwatch on the Seg. but only display the valid digits. + +/* + * how it work? + * 1. based on test2.c. + * 2. add check to the buffer. + * 3. only this unit is not 0 OR the previous unit is not 0, log the number in the buffer. + * 4. so this is my niubility code. + * 5. I'm so proud of myself. + * 6. msksbr NB!!! + * 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. + +void test3() +{ + unsigned char second = 0; // to count the seconds. + unsigned char minute = 0; // to count the minutes. + unsigned char hour = 0; // to count the hours. + unsigned char i = 0; // to count the times. + unsigned long time_counter = 0; + + while (1) { + for (i = 0; i < 6; i++) { + // display number + set38Transistors(i); + P2 = LedBuff[i]; + // display dot + if (i == 2 || i == 4) { + // 2nd dot between the minutes and seconds. + // 4th dot between the hours and minutes. + BUS8DP = 0; // display the dot. + } else { + BUS8DP = 1; // hide the dot on other places. + } + + timer(1); // delay 1ms. + time_counter++; + + // update time when 1000ms passed. + if (time_counter >= 1000) { + time_counter = 0; // reset the counter. + second++; // count the seconds. + // solve unit overflow. + // second overflow + if (second >= 60) { + second = 0; // reset the second. + minute++; // count the minutes. + } + // minute overflow. + if (minute >= 60) { + minute = 0; // reset the minute. + hour++; // count the hours. + } + // everything overflow. + if (hour >= 99 && second >= 59 && minute >= 59) { + hour = 0; // reset the hour. + second = 0; // reset the second. + minute = 0; // reset the minute. + } + } + // log the number in the buffer. + // only log the valid digits. + 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. + LedBuff[2] = minute > 0 ? LedChar[minute % 10] : 0XFF; // log the minute units. + LedBuff[1] = (second >= 10 || minute > 0) ? LedChar[second / 10] : 0XFF; // log the second tens. + LedBuff[0] = LedChar[second % 10]; // log the second units. + } + } +} + +void main() +{ + test3(); +} \ No newline at end of file