archieve: homework5 test2

This commit is contained in:
2025-04-24 17:31:02 +08:00
parent acd7bb9ec9
commit cd667a286e
6 changed files with 106 additions and 21 deletions
Binary file not shown.
BIN
View File
Binary file not shown.
+2 -1
View File
@@ -19,7 +19,8 @@
"targets": {
"Debug": {
"excludeList": [
"src/test.c"
"src/test.c",
"src/test1.c"
],
"toolchain": "Keil_C51",
"compileConfig": {
+20 -1
View File
@@ -3,7 +3,7 @@
* this header defines:
* variables:
* 1. pin numbers to control the LED and the Seg.
* 2. sdf for timer
* 2. seg chars to display the number on the Seg.
* functions:
* 1. control the LED and the Seg.
* 2. to display the number on the Seg.
@@ -30,6 +30,25 @@ sbit ADDR1 = P1 ^ 1; // to B(A1)
sbit ADDR2 = P1 ^ 2; // to C(A2)
sbit ADDR3 = P1 ^ 3; // to E1
sbit ENLED = P1 ^ 4; // to E2 & E3
// seg chars:
unsigned char code LedChar[] = {
0xC0,
0xF9,
0XA4,
0XB0,
0X99,
0X92,
0X82,
0XF8,
0X80,
0X90,
0X88,
0X83,
0XC6,
0XA1,
0X86,
0X8E,
};
// functions:
// 38 transistors controller:
-19
View File
@@ -3,25 +3,6 @@
// display the last 6 digits of your student ID on the Seg.
#include "homework5.h"
unsigned char code LedChar[] = {
0xC0,
0xF9,
0XA4,
0XB0,
0X99,
0X92,
0X82,
0XF8,
0X80,
0X90,
0X88,
0X83,
0XC6,
0XA1,
0X86,
0X8E,
};
unsigned char LedBuff[6] = {
0xFF, 0XFF, 0XFF, 0XFF, 0XFF, 0XFF};
unsigned char i = 0;
+84
View File
@@ -0,0 +1,84 @@
// test2.c
// 不使用中断,让数码管动态显示秒表
// display the stopwatch on the Seg. without using interrupt.
/*
* seg display format:
* 00.00.00
* HH.MM.SS
* HH: hour, MM: minute, SS: second
* dot . is dp
*/
/*
* how it work?
* 1. sleep 1s
* 2. count the seconds, minutes, hours.
* 3. reset the seconds, minutes when it reaches 60.
* 4. reset all when everything overflow.
* 5. log the number in the buffer.
* 6. display the number on the Seg.
*/
#include "homework5.h"
unsigned char LedBuff[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // buffer to store the seg chars.
void test2()
{
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.
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.
LedBuff[2] = LedChar[minute % 10]; // log the minute units.
LedBuff[1] = LedChar[second / 10]; // log the second tens.
LedBuff[0] = LedChar[second % 10]; // log the second units.
}
}
}
void main()
{
test2();
}