[New version of Zhongke Bluexun AB32VG1 RISC-V development board] - 5: Enhanced version of Blink
[Copy link]
This post was last edited by MianQi on 2021-8-13 07:52
A few updates to the "Blink" from the previous post:
Previously, RGB Blink used the same thread. Now, a thread is created for each of the five LEDs: red, green, blue, yellow, and white. In addition, the flashing frequencies of the five LEDs are different, with periods of 200ms, 400ms, 600ms, 800ms, and 1s respectively.
The specific changes are:
1. Both “LED.h” and “main.c” remain unchanged.
2. In “LED.c”,
From one thread "rgb" it becomes five threads "rgbyw_red", "rgbyw_green", "rgbyw_blue", "rgbyw_yellow", "rgbyw_white"; the time slices and entry function parameters of these five threads are the same, the difference lies in the different names, entry function names and priorities (red-green-blue-yellow-white correspond to 1-2-3-4-5 respectively).
3. The entry (main) functions of the five threads correspond to the five thread start (initialization) functions.
4. The thread startup function "INIT_APP_EXPORT()" is called five times.
#include "LED.h"
#include <rtthread.h>
#include "board.h"
//q定义一个结构体类型
struct LED_RGBYW
{
uint8_t LED_R;
uint8_t LED_G;
uint8_t LED_B;
uint8_t LED_Y;
uint8_t LED_W;
};
//q实例化一个结构体对象
struct LED_RGBYW led_rgbyw;
//q结构体对象的初始化
void led_rgbyw_init(void){
//q将结构体对象的成员对应到管脚
led_rgbyw.LED_R = rt_pin_get("PB.0");
led_rgbyw.LED_G = rt_pin_get("PA.5");
led_rgbyw.LED_B = rt_pin_get("PE.1");
led_rgbyw.LED_Y = rt_pin_get("PE.0");
led_rgbyw.LED_W = rt_pin_get("PF.0");
//q设置结构体对象的成员管脚的输出方式
rt_pin_mode(led_rgbyw.LED_R, PIN_MODE_OUTPUT);
rt_pin_mode(led_rgbyw.LED_G, PIN_MODE_OUTPUT);
rt_pin_mode(led_rgbyw.LED_B, PIN_MODE_OUTPUT);
rt_pin_mode(led_rgbyw.LED_Y, PIN_MODE_OUTPUT);
rt_pin_mode(led_rgbyw.LED_W, PIN_MODE_OUTPUT);
}
//RGBYW五色LED各自的驱动程序
//if(toggle==1) LED亮
//if(toggle==0) LED灭
void led_rgbyw_Red(rt_bool_t toggle){
if(toggle){
rt_pin_write(led_rgbyw.LED_R, PIN_LOW);
} else{
rt_pin_write(led_rgbyw.LED_R, PIN_HIGH);
}
}
void led_rgbyw_Green(rt_bool_t toggle){
if(toggle){
rt_pin_write(led_rgbyw.LED_G, PIN_LOW);
} else{
rt_pin_write(led_rgbyw.LED_G, PIN_HIGH);
}
}
void led_rgbyw_Blue(rt_bool_t toggle){
if(toggle){
rt_pin_write(led_rgbyw.LED_B, PIN_LOW);
} else{
rt_pin_write(led_rgbyw.LED_B, PIN_HIGH);
}
}
void led_rgbyw_Yellow(rt_bool_t toggle){
if(toggle){
rt_pin_write(led_rgbyw.LED_Y, PIN_LOW);
} else{
rt_pin_write(led_rgbyw.LED_Y, PIN_HIGH);
}
}
void led_rgbyw_White(rt_bool_t toggle){
if(toggle){
rt_pin_write(led_rgbyw.LED_W, PIN_LOW);
} else{
rt_pin_write(led_rgbyw.LED_W, PIN_HIGH);
}
}
//q编写线程入口函数(主程序文件)
static void led_rgbyw_red_thread_entry(void* p){
led_rgbyw_init();
while(1){
led_rgbyw_Red(1);
rt_thread_mdelay(100);
led_rgbyw_Red(0);
rt_thread_mdelay(100);
}
}
static void led_rgbyw_green_thread_entry(void* p){
led_rgbyw_init();
while(1){
led_rgbyw_Green(1);
rt_thread_mdelay(200);
led_rgbyw_Green(0);
rt_thread_mdelay(200);
}
}
static void led_rgbyw_blue_thread_entry(void* p){
led_rgbyw_init();
while(1){
led_rgbyw_Blue(1);
rt_thread_mdelay(300);
led_rgbyw_Blue(0);
rt_thread_mdelay(300);
}
}
static void led_rgbyw_yellow_thread_entry(void* p){
led_rgbyw_init();
while(1){
led_rgbyw_Yellow(1);
rt_thread_mdelay(300);
led_rgbyw_Yellow(0);
rt_thread_mdelay(300);
}
}
static void led_rgbyw_white_thread_entry(void* p){
led_rgbyw_init();
while(1){
led_rgbyw_White(1);
rt_thread_mdelay(300);
led_rgbyw_White(0);
rt_thread_mdelay(300);
}
}
//q创建线程启动函数,用于启动此前创建的线程主体
static int Thread_led_rgbyw_red(void){
rt_thread_t thread = RT_NULL;
thread = rt_thread_create("led_rgbyw_red", led_rgbyw_red_thread_entry, RT_NULL, 512, 1, 10);
if(thread == RT_NULL){
rt_kprintf("Thread_led_rgbyw_red initiate ERROR");
return RT_ERROR;
}
rt_thread_startup(thread);
}
static int Thread_led_rgbyw_green(void){
rt_thread_t thread = RT_NULL;
thread = rt_thread_create("led_rgbyw_green", led_rgbyw_green_thread_entry, RT_NULL, 512, 2, 10);
if(thread == RT_NULL){
rt_kprintf("Thread_led_rgbyw_green initiate ERROR");
return RT_ERROR;
}
rt_thread_startup(thread);
}
static int Thread_led_rgbyw_blue(void){
rt_thread_t thread = RT_NULL;
thread = rt_thread_create("led_rgbyw_blue", led_rgbyw_blue_thread_entry, RT_NULL, 512, 3, 10);
if(thread == RT_NULL){
rt_kprintf("Thread_led_rgbyw_blue initiate ERROR");
return RT_ERROR;
}
rt_thread_startup(thread);
}
static int Thread_led_rgbyw_yellow(void){
rt_thread_t thread = RT_NULL;
thread = rt_thread_create("led_rgbyw_yellow", led_rgbyw_yellow_thread_entry, RT_NULL, 512, 4, 10);
if(thread == RT_NULL){
rt_kprintf("Thread_led_rgbyw_yellow initiate ERROR");
return RT_ERROR;
}
rt_thread_startup(thread);
}
static int Thread_led_rgbyw_white(void){
rt_thread_t thread = RT_NULL;
thread = rt_thread_create("led_rgbyw_white", led_rgbyw_white_thread_entry, RT_NULL, 512, 5, 10);
if(thread == RT_NULL){
rt_kprintf("Thread_led_rgbyw_white initiate ERROR");
return RT_ERROR;
}
rt_thread_startup(thread);
}
//q将线程的初始化加入系统初始化,启动线程
INIT_APP_EXPORT(Thread_led_rgbyw_red);
INIT_APP_EXPORT(Thread_led_rgbyw_green);
INIT_APP_EXPORT(Thread_led_rgbyw_blue);
INIT_APP_EXPORT(Thread_led_rgbyw_yellow);
INIT_APP_EXPORT(Thread_led_rgbyw_white);
There are two problems encountered in the experiment:
1. The USB interface of the board seemed to have poor contact. The LED was not on at first. But when I held the USB interface to observe it, it suddenly lit up, which surprised me a little.
2. Although I have used "Erase" of "Downloader", the default RGB LED is still flashing red for some reason.
|