[Review of the RTT of the Bluesight AB32VG1 RISC-V board] + LED and button control
[Copy link]
This post was last edited by xiyue521 on 2021-5-1 02:30
From the schematic diagram, we can know that the LED is on at low level, and the button is at low level when pressed. We can configure the button to pull up the input to detect:
rt_pin_mode(S3, PIN_MODE_INPUT_PULLUP );
1. First encapsulate the underlying function, rt_pin_get to obtain the pin id value
rt_pin_mode, rt_pin_write, and rt_pin_read are the key mode, read, and write functions. You can modify the corresponding pin by directly passing the obtained ID value into them.
void RGB_Init(void){
LED_R = rt_pin_get("PE.1");
LED_G = rt_pin_get("PE.4");
LED_B = rt_pin_get("PA.2");
rt_pin_mode(LED_R, PIN_MODE_OUTPUT);
rt_pin_mode(LED_G, PIN_MODE_OUTPUT);
rt_pin_mode(LED_B, PIN_MODE_OUTPUT);
}
void RGB_Red(rt_bool_t on){
if (on) {
rt_pin_write(LED_R, PIN_LOW);
}else {
rt_pin_write(LED_R, PIN_HIGH);
}
rt_pin_write(LED_G, PIN_HIGH);
rt_pin_write(LED_B, PIN_HIGH);
}
void RGB_Blue(rt_bool_t on){
if (on) {
rt_pin_write(LED_B, PIN_LOW);
}else {
rt_pin_write(LED_B, PIN_HIGH);
}
rt_pin_write(LED_G, PIN_HIGH);
rt_pin_write(LED_R, PIN_HIGH);
}
void RGB_Green(rt_bool_t on){
if (on) {
rt_pin_write(LED_G, PIN_LOW);
}else {
rt_pin_write(LED_G, PIN_HIGH);
}
rt_pin_write(LED_R, PIN_HIGH);
rt_pin_write(LED_B, PIN_HIGH);
}
void RGB_ALL(rt_bool_t on){
if (on) {
rt_pin_write(LED_G, PIN_LOW);
rt_pin_write(LED_R, PIN_LOW);
rt_pin_write(LED_B, PIN_LOW);
}
else
{
rt_pin_write(LED_G, PIN_HIGH);
rt_pin_write(LED_R, PIN_HIGH);
rt_pin_write(LED_B, PIN_HIGH);
}
}
void Key_Init(void){
S3= rt_pin_get("PF.0");
S2 = rt_pin_get("PF.1");
rt_pin_mode(S2, PIN_MODE_INPUT_PULLUP );
rt_pin_mode(S3, PIN_MODE_INPUT_PULLUP );
}
2. Next, create a LED task, during which the global variable mode is used, and the value can also be transferred through the mailbox. In addition, the INIT_APP_EXPORT() function can be used to directly initialize the function without calling it in main.
static void rgb_thread_entry(void* pdata)
{
RGB_Init();
while(1)
{
switch(mode)
{
case 0:
RGB_ALL(0);
rt_thread_mdelay(1000);
RGB_ALL(1);
rt_thread_mdelay(1000);
break;
case 1:
rt_thread_mdelay(1000);
RGB_Blue(1);
rt_thread_mdelay(1000);
RGB_Green(1);
rt_thread_mdelay(1000);
RGB_Red(1);
break;
}
}
}
static int Thread_RGB(void){
rt_thread_t thread = RT_NULL;
thread = rt_thread_create("rgb", rgb_thread_entry, RT_NULL, 512, 9, 10);
if(thread == RT_NULL)
{
rt_kprintf("Thread_GRB Init ERROR");
return RT_ERROR;
}
rt_thread_startup(thread);
}
INIT_APP_EXPORT(Thread_RGB);
3. Create a key task. Here, the polling method is used to detect the key, which is detected once every 100ms. When the corresponding key is detected, the mode is modified to switch different LED flashing modes.
Both tasks are created dynamically and the formats are similar, so using RTOS can actually simplify a lot of things. In particular, RTTHREAD also has console and LOG functions, which are convenient for debugging. I highly recommend it!
static void key_thread_entry(void *parameter){
Key_Init();
RGB_Init();
RGB_Red(1);
while(1){
if (rt_pin_read(S2)==PIN_LOW) mode=0;
if (rt_pin_read(S3)==PIN_LOW) mode=1;
rt_thread_mdelay(100);
}
}
static int Key_Thread_Init(void){
rt_thread_t thread = RT_NULL;
thread = rt_thread_create("key_thread", key_thread_entry, RT_NULL, 512, 10, 10);
if(thread == RT_NULL)
{
rt_kprintf("Thread_GRB Init ERROR");
return RT_ERROR;
}
rt_thread_startup(thread);
}
INIT_APP_EXPORT(Key_Thread_Init);
4. The main function prints the mode once every 1s, nothing else.
#include <rtthread.h>
#include "board.h"
uint8_t LED_R;
uint8_t LED_B;
uint8_t LED_G;
uint8_t S2;
uint8_t S3;
uint8_t mode ;
int main(void)
{
rt_kprintf("Hello, world\n");
while (1)
{
rt_kprintf("mode:%d\r\n",mode);
rt_thread_mdelay(1000); //空延时
};
return 0;
}
5. Enter ps to view the thread information, and you can see that there are more button threads and LED threads.
This article ends here. The following is an introduction to timers.
|