55 lines
2.3 KiB
C
55 lines
2.3 KiB
C
// test4.c
|
|
// 利用定时器T0定时10秒,并通过数码管显示秒表,到10秒后让led流水灯从左往右移动
|
|
// 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();
|
|
} |