archieve: homework6 test1

This commit is contained in:
2025-05-06 13:11:46 +08:00
parent c8709b51af
commit 6f0d7b3a86
2 changed files with 46 additions and 0 deletions
+4
View File
@@ -3,6 +3,7 @@
* this is the header file to * this is the header file to
* define the * define the
* variables: * variables:
* - 38 transistors address
* - pins for the LEDs and the segs * - pins for the LEDs and the segs
* - pins for the 38 transistors * - pins for the 38 transistors
* - seg chars to display to display the number * - seg chars to display to display the number
@@ -13,6 +14,9 @@
#include <REG52.H> #include <REG52.H>
// variables: // variables:
// 38 transistors address
#define LEDS [6]{0, 1, 2, 3, 4, 5}
#define TO_LED 6
// pins for the LEDs and the segs // pins for the LEDs and the segs
sbit BUS1A = P2 ^ 0; // Red LED and A Seg sbit BUS1A = P2 ^ 0; // Red LED and A Seg
sbit BUS2B = P2 ^ 1; // Orange LED and B Seg sbit BUS2B = P2 ^ 1; // Orange LED and B Seg
+42
View File
@@ -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();
}