archieve: homework6

This commit is contained in:
2025-05-06 21:08:28 +08:00
parent 346478498d
commit 8a90337479
2 changed files with 44 additions and 1 deletions
+42
View File
@@ -3,10 +3,52 @@
// use timer T0 to count 10 seconds and display the stopwatch on the 7-segment display, and let the led light move from left to right after 10 seconds.
#include "homework6.h"
unsigned char digits[2] = {0, 0};
unsigned char displayPos = 0;
void test4()
{
digits[0] = 1; // set the first digit to 0.
digits[1] = 0; // set the second digit to 1.
TMOD = 0x01; // set the timer 0 to mode 1.
TH0 = 0xFC; // set the timer 0 to 1s.
TL0 = 0x18; // set the timer 0 to 1s.
TR0 = 1; // start the timer 0.
ET0 = 1; // enable the timer 0 interrupt.
EA = 1; // enable the interrupt.
while (1) {}
}
void Timer0_ISR() interrupt 1
{
static unsigned char led = 0xFE; // store the led light.
static unsigned int counter = 0; // counter to count the number of interrupts.
TH0 = 0xFC; // reset the timer 0.
TL0 = 0x18; // reset the timer 0.
if (digits[0] == 0 && digits[1] == 0) { // if the stopwatch is 00, let the led light move from left to right.
set38(TO_LED); // set the 38 transistors to the address.
if (++counter >= 1000) {
counter = 0; // reset the counter.
led = (led << 1) | 0x01; // shift the led light to the left.
if (led == 0xFF) led = 0xFE; // if the led light is 0xFF, reset the led light to 0xFE.
P2 = led; // set the led light.
}
} else {
set38(displayPos); // set the 38 transistors to the address.
P2 = LedChar[digits[1 - displayPos]]; // display the digit.
displayPos++; // switch to the next digit.
if (displayPos >= 2) displayPos = 0; // if the display position is 2, reset the display position to 0.
if (++counter >= 1000) { // if the counter is 1000, count 1 second.
counter = 0; // reset the counter.
if (digits[1]-- == 0) {
digits[1] = 9;
if (digits[0] > 0) {
digits[0]--;
}
}
}
}
}
void main()
{
test4();