1816 views|5 replies

305

Posts

0

Resources
The OP
 

[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.

This post is from Domestic Chip Exchange

Latest reply

What rtos is this using?   Details Published on 2021-8-13 23:11
Personal signature

“Everyone wants the project to be good, fast, and cheap... pick two.”

- Unknown

 
 

305

Posts

0

Resources
2
 
This post was last edited by MianQi on 2021-8-13 07:51

Wiring Diagram:

Effect demonstration:


This post is from Domestic Chip Exchange
Personal signature

“Everyone wants the project to be good, fast, and cheap... pick two.”

- Unknown

 
 
 

305

Posts

0

Resources
3
 

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.

This post is from Domestic Chip Exchange
Personal signature

“Everyone wants the project to be good, fast, and cheap... pick two.”

- Unknown

 
 
 

2w

Posts

74

Resources
4
 

Can play :)

This post is from Domestic Chip Exchange
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle
Personal signature

加油!在电子行业默默贡献自己的力量!:)

 
 
 

7452

Posts

2

Resources
5
 

What rtos is this using?

This post is from Domestic Chip Exchange

Comments

The default preset is "RT-Thread".  Details Published on 2021-8-14 09:11
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 
 

305

Posts

0

Resources
6
 
freebsder posted on 2021-8-13 23:11 What rtos is this using?

The default preset is "RT-Thread".

This post is from Domestic Chip Exchange
Personal signature

“Everyone wants the project to be good, fast, and cheap... pick two.”

- Unknown

 
 
 

Find a datasheet?

EEWorld Datasheet Technical Support

Related articles more>>

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list