[新版中科蓝讯AB32VG1 RISC-V开发板] - 5:增强版的 Blink
本帖最后由 MianQi 于 2021-8-13 07:52 编辑<p>对前一个帖子中的“Blink”做了一些升级:</p>
<p>此前的RGB Blink用的是同一个线程,现在给红绿蓝黄白五个LED分别建一个线程,并且,五个LED的闪烁频率不一样,周期分别是200ms,400ms,600ms,800ms和1s。</p>
<p> </p>
<p>具体的改动是:</p>
<p>1、“LED.h” 和 “main.c”都不变。</p>
<p>2、“LED.c” 中,</p>
<p>由一个线程“rgb”变为五个线程“rgbyw_red”、“rgbyw_green”、“rgbyw_blue”、“rgbyw_yellow”、“rgbyw_white”;这五个线程的时间片和入口函数参数都一样,区别在于不同的名称、入口函数名和优先级(红-绿-蓝-黄-白分别对应1-2-3-4-5)。</p>
<p>3、五个线程的入口(主体)函数对应着五个线程启动(初始化)函数。</p>
<p>4、线程的启动函数“INIT_APP_EXPORT()”调用五次。</p>
<p> </p>
<pre>
<code class="language-cpp">#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);
</code></pre>
<p> </p>
<p>实验中还遇到两个问题:</p>
<p>1、板子的USB接口似乎接触不良,刚开始的时候LED不亮,待拿着USB接口处观察时,突然亮了,被小小地惊到了。</p>
<p>2、尽管已经用过了“Downloader”的“Erase”,但默认的RGB LED还是以红色闪烁,不知何故。</p>
本帖最后由 MianQi 于 2021-8-13 07:51 编辑
<p>接线图:</p>
<p></p>
<p> </p>
<p>效果演示:</p>
<p><iframe allowfullscreen="true" frameborder="0" height="450" src="//player.bilibili.com/player.html?bvid=1LL411J7TJ&page=1" style="background:#eee;margin-bottom:10px;" width="700"></iframe><br />
</p>
<p> </p>
<p>实验中还遇到两个问题:</p>
<p>1、板子的USB接口似乎接触不良,刚开始的时候LED不亮,待拿着USB接口处观察时,突然亮了,被小小地惊到了。</p>
<p>2、尽管已经用过了“Downloader”的“Erase”,但默认的RGB LED还是以红色闪烁,不知何故。</p>
<p>会玩:)</p>
<p>这是用的什么rtos?</p>
freebsder 发表于 2021-8-13 23:11
这是用的什么rtos?
<p>默认的预置的"RT-Thread"。</p>
页:
[1]