From 6f0d7b3a864d7f0e9a520a36af72fd31a0b7264f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=A1=E5=9D=82=E3=82=B9=E3=83=90=E3=83=AB?= Date: Tue, 6 May 2025 13:11:46 +0800 Subject: [PATCH] archieve: homework6 test1 --- homework6/src/homework6.h | 4 ++++ homework6/src/test1.c | 42 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 homework6/src/test1.c diff --git a/homework6/src/homework6.h b/homework6/src/homework6.h index 3f046c5..07af757 100644 --- a/homework6/src/homework6.h +++ b/homework6/src/homework6.h @@ -3,6 +3,7 @@ * this is the header file to * define the * variables: + * - 38 transistors address * - pins for the LEDs and the segs * - pins for the 38 transistors * - seg chars to display to display the number @@ -13,6 +14,9 @@ #include // variables: +// 38 transistors address +#define LEDS [6]{0, 1, 2, 3, 4, 5} +#define TO_LED 6 // pins for the LEDs and the segs sbit BUS1A = P2 ^ 0; // Red LED and A Seg sbit BUS2B = P2 ^ 1; // Orange LED and B Seg diff --git a/homework6/src/test1.c b/homework6/src/test1.c new file mode 100644 index 0000000..a4fd7e5 --- /dev/null +++ b/homework6/src/test1.c @@ -0,0 +1,42 @@ +// test1.c +// 定时器0中断实现流水灯,设定合适的初值实现1s定时,控制LED灯依次点亮,形成流水灯效果 +// Timer 0 interrupt to implement the LED light, set the appropriate initial value to implement 1s timing, control the LED light to turn on one by one, forming the light effect. + +#include "homework6.h" + +void test1() +{ + // Set the 38 transistors to the address. + set38(TO_LED); + // init the timer 0. + TMOD = 0x01; // set the timer 0 to mode 1. + TH0 = 0xFC; // set the timer 0 to 1s. + TL0 = 0x66; // set the timer 0 to 1s. + TR0 = 1; // start the timer 0. + ET0 = 1; // enable the timer 0 interrupt. + EA = 1; // enable the interrupt. + P2 = 0xFE; // init mode: the first LED light. + while (1) {} +} + +void Timer0_ISR() interrupt 1 +{ + static unsigned int counter = 0; // counter to count the number of interrupts. + static unsigned char led = 0xFE; // store the LED light. + + // reset the timer 0. + TH0 = 0xFC; // set the timer 0 to 1s. + TL0 = 0x66; // set the timer 0 to 1s. + + 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. + } +} + +void main() +{ + test1(); +} \ No newline at end of file