潘多拉 RT-Thread 三色 LED
实验概述
本实验主要功能是运行 RT-Thread 操作系统,驱动板载 RGB 三色 LED 灯,使其周期性地变换颜色。
硬件连接
潘多拉 IoT Board 板载的 RGB-LED 电路如下图所示。可以看到,RGB-LED 属于共阳 LED,阴极分别与单片机的引脚连接。也就是说,当单片机引脚输出为低电平时点亮 LED,高电平时熄灭 LED。
查看原理图,可知 LED_R 引脚连接 STM32 的 38 号引脚,即 PE7;LED_G 引脚连接 STM32 的 39 号引脚,即 PE8;LED_B 引脚连接 STM32 的 40 号引脚,即 PE9。
示例代码
参考《潘多拉 IoT Board 开发环境》创建工程,在 applications/main.c 中输入如下代码。
applications/main.c
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
/* defined the LED pin */
#define PIN_LED_R GET_PIN(E, 7)
#define PIN_LED_G GET_PIN(E, 8)
#define PIN_LED_B GET_PIN(E, 9)
/* 定义 LED 亮灭电平 */
#define LED_ON (0)
#define LED_OFF (1)
/* 定义 8 组 LED 闪灯表,其顺序为 R G B */
static const rt_uint8_t _blink_tab[][3] =
{
{LED_ON, LED_ON, LED_ON},
{LED_OFF, LED_ON, LED_ON},
{LED_ON, LED_OFF, LED_ON},
{LED_ON, LED_ON, LED_OFF},
{LED_OFF, LED_OFF, LED_ON},
{LED_ON, LED_OFF, LED_OFF},
{LED_OFF, LED_ON, LED_OFF},
{LED_OFF, LED_OFF, LED_OFF},
};
int main(void)
{
unsigned int count = 1;
unsigned int group_num = sizeof(_blink_tab)/sizeof(_blink_tab[0]);
unsigned int group_current;
/* 设置 RGB 灯引脚为输出模式 */
rt_pin_mode(PIN_LED_R, PIN_MODE_OUTPUT);
rt_pin_mode(PIN_LED_G, PIN_MODE_OUTPUT);
rt_pin_mode(PIN_LED_B, PIN_MODE_OUTPUT);
while (count > 0)
{
/* 获得组编号 */
group_current = count % group_num;
/* 控制 RGB 灯 */
rt_pin_write(PIN_LED_R, _blink_tab[group_current][0]);
rt_pin_write(PIN_LED_G, _blink_tab[group_current][1]);
rt_pin_write(PIN_LED_B, _blink_tab[group_current][2]);
/* 输出 LOG 信息 */
LOG_D("group: %d | red led [%-3.3s] | green led [%-3.3s] | blue led [%-3.3s]",
group_current,
_blink_tab[group_current][0] == LED_ON ? "ON" : "OFF",
_blink_tab[group_current][1] == LED_ON ? "ON" : "OFF",
_blink_tab[group_current][2] == LED_ON ? "ON" : "OFF");
/* 延时一段时间 */
rt_thread_mdelay(500);
count++;
}
return 0;
}
完整代码:02_basic_rgb_led