STC8H8K64U 库函数学习笔记 —— 流水灯

发布于:2024-04-14 ⋅ 阅读:(215) ⋅ 点赞:(0)

STC8H8K64U 库函数学习笔记 —— 流水灯

环境说明:
芯片:STC8H8K64U
软件:

  1. KeilC51 μVersion V5.38.00
  2. STCAI-ISP (V6.94)

库文件说明:我将依赖的库文件统一放置到工程下的 lib 目录中,所以,代码中的包含指令会有 lib/ 的前缀

从左往右依次亮起,然后全灭,然后从右往左依次亮起,再次全灭,不断循环,代码如下:

#include "lib/Config.h"
#include "lib/STC8G_H_GPIO.h"
#include "lib/STC8G_H_Delay.h"

static int index = 0;
static const int INTERVAL = 200;
static const int SIZE = 8;

void GPIO_config() {
    P2M1 &= ~GPIO_Pin_7, P2M0 |=  GPIO_Pin_7; // 设置 P27 为 推挽
    P2M1 &= ~GPIO_Pin_6, P2M0 |=  GPIO_Pin_6; // 设置 P26 为 推挽
    P1M1 &= ~GPIO_Pin_5, P2M0 |=  GPIO_Pin_5; // 设置 P15 为 推挽
    P1M1 &= ~GPIO_Pin_4, P2M0 |=  GPIO_Pin_4; // 设置 P14 为 推挽
    P2M1 &= ~GPIO_Pin_3, P2M0 |=  GPIO_Pin_3; // 设置 P23 为 推挽
    P2M1 &= ~GPIO_Pin_2, P2M0 |=  GPIO_Pin_2; // 设置 P22 为 推挽
    P2M1 &= ~GPIO_Pin_1, P2M0 |=  GPIO_Pin_1; // 设置 P21 为 推挽
    P2M1 &= ~GPIO_Pin_0, P2M0 |=  GPIO_Pin_0; // 设置 P20 为 推挽
    P4M1 &= ~GPIO_Pin_5, P4M0 |=  GPIO_Pin_5; // 设置 P45 为 推挽
}

void turnOffAllLed() {
    P45 = 0;
    P27 = 1, P26 = 1, P15 = 1, P14 = 1;
    P23 = 1, P22 = 1, P21 = 1, P20 = 1;
}

void marquee(int start, int incr) {
    turnOffAllLed();
    delay_ms(INTERVAL);
    index = start;
    while(index < SIZE && index > -1) {
        switch(index) {
            case 0: P27 = 0; break; case 1: P26 = 0; break;
            case 2: P15 = 0; break; case 3: P14 = 0; break;
            case 4: P23 = 0; break; case 5: P22 = 0; break;
            case 6: P21 = 0; break; case 7: P20 = 0; break;
        }
        index += incr;
        delay_ms(INTERVAL);
    }
    turnOffAllLed();
}

void main() {
    GPIO_config();
    
    while(1) {
        marquee(0, 1);
        marquee(7, -1);
    }
}