MianQi 发表于 2021-8-13 07:54

[新版中科蓝讯AB32VG1 RISC-V开发板] - 5:增强版的 Blink

本帖最后由 MianQi 于 2021-8-13 07:52 编辑

<p>对前一个帖子中的&ldquo;Blink&rdquo;做了一些升级:</p>

<p>此前的RGB Blink用的是同一个线程,现在给红绿蓝黄白五个LED分别建一个线程,并且,五个LED的闪烁频率不一样,周期分别是200ms,400ms,600ms,800ms和1s。</p>

<p>&nbsp;</p>

<p>具体的改动是:</p>

<p>1、&ldquo;LED.h&rdquo; 和 &ldquo;main.c&rdquo;都不变。</p>

<p>2、&ldquo;LED.c&rdquo; 中,</p>

<p>由一个线程&ldquo;rgb&rdquo;变为五个线程&ldquo;rgbyw_red&rdquo;、&ldquo;rgbyw_green&rdquo;、&ldquo;rgbyw_blue&rdquo;、&ldquo;rgbyw_yellow&rdquo;、&ldquo;rgbyw_white&rdquo;;这五个线程的时间片和入口函数参数都一样,区别在于不同的名称、入口函数名和优先级(红-绿-蓝-黄-白分别对应1-2-3-4-5)。</p>

<p>3、五个线程的入口(主体)函数对应着五个线程启动(初始化)函数。</p>

<p>4、线程的启动函数&ldquo;INIT_APP_EXPORT()&rdquo;调用五次。</p>

<p>&nbsp;</p>

<pre>
<code class="language-cpp">#include "LED.h"
#include &lt;rtthread.h&gt;
#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>&nbsp;</p>

<p>实验中还遇到两个问题:</p>

<p>1、板子的USB接口似乎接触不良,刚开始的时候LED不亮,待拿着USB接口处观察时,突然亮了,被小小地惊到了。</p>

<p>2、尽管已经用过了&ldquo;Downloader&rdquo;的&ldquo;Erase&rdquo;,但默认的RGB LED还是以红色闪烁,不知何故。</p>

MianQi 发表于 2021-8-13 07:54

本帖最后由 MianQi 于 2021-8-13 07:51 编辑

<p>接线图:</p>

<p></p>

<p>&nbsp;</p>

<p>效果演示:</p>

<p><iframe allowfullscreen="true" frameborder="0" height="450" src="//player.bilibili.com/player.html?bvid=1LL411J7TJ&amp;page=1" style="background:#eee;margin-bottom:10px;" width="700"></iframe><br />
&nbsp;</p>

<p>&nbsp;</p>

MianQi 发表于 2021-8-13 07:54

<p>实验中还遇到两个问题:</p>

<p>1、板子的USB接口似乎接触不良,刚开始的时候LED不亮,待拿着USB接口处观察时,突然亮了,被小小地惊到了。</p>

<p>2、尽管已经用过了&ldquo;Downloader&rdquo;的&ldquo;Erase&rdquo;,但默认的RGB LED还是以红色闪烁,不知何故。</p>

soso 发表于 2021-8-13 09:55

<p>会玩:)</p>

freebsder 发表于 2021-8-13 23:11

<p>这是用的什么rtos?</p>

MianQi 发表于 2021-8-14 09:11

freebsder 发表于 2021-8-13 23:11
这是用的什么rtos?

<p>默认的预置的&quot;RT-Thread&quot;。</p>
页: [1]
查看完整版本: [新版中科蓝讯AB32VG1 RISC-V开发板] - 5:增强版的 Blink