archive: homework4 test4
This commit is contained in:
@@ -20,7 +20,8 @@
|
||||
"Debug": {
|
||||
"excludeList": [
|
||||
"src/test1.c",
|
||||
"src/test2.c"
|
||||
"src/test2.c",
|
||||
"src/test3.c"
|
||||
],
|
||||
"toolchain": "Keil_C51",
|
||||
"compileConfig": {
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,40 @@
|
||||
// test4.c
|
||||
// 使用定时器来实现左右移动的流水灯(每过0.5秒,切换一个小灯);
|
||||
#include "homework4.h"
|
||||
|
||||
unsigned char cnt = 0; // 定时器溢出计数
|
||||
unsigned char led = 0; // 当前亮灯
|
||||
|
||||
void test4()
|
||||
{
|
||||
set_38_2LED();
|
||||
TMOD = 0x01; // timer T0 mode 1, 16-bit timer
|
||||
TH0 = 0XB8;
|
||||
TL0 = 0X00;
|
||||
TR0 = 1; // timer T0 run
|
||||
|
||||
while (1) {
|
||||
if (TF0 == 1) { // timer T0 overflow
|
||||
TF0 = 0; // clear timer T0 overflow flag
|
||||
TH0 = 0xB8; // reload timer T0 value
|
||||
TL0 = 0x00;
|
||||
cnt++; // log timer T0 overflow counts
|
||||
if (cnt >= 50) {
|
||||
cnt = 0; // clear log timer T0 overflow counts
|
||||
if (led < 8) {
|
||||
P2 = ~(0X01 << led);
|
||||
} else {
|
||||
P2 = ~(0X80 >> (led - 8));
|
||||
}
|
||||
led++;
|
||||
if (led >= 16) {
|
||||
led = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void main()
|
||||
{
|
||||
test4();
|
||||
}
|
||||
Reference in New Issue
Block a user