開發板原理圖
從原理圖與開發板實物圖,我們可以看到給用戶使用的LED有兩個,分別為LED_USER1與LED_USER2兩個,對開發板的LED0與LED1,原理圖如下:
開發板上還有三個用戶按鍵,原理圖如下:
安裝開發板SDK
打開RT-Thread Studio,點擊SDK管理器,下載開發板SDK:
等待安裝結束后退出SDK管理器
新建工程
打開RT-Thread Studio點擊菜單
選擇基于開發板創建工程:
輸入工程名稱后生成工程:
生成工程預覽如下:
配置GPIO
選擇RA Smart Configurator文件夾位置,只需要選到FSP文件夾就行了:
在RASC工程配置LED為輸出模式,按鍵為輸入模式,并設置為默認的中斷號。保存并生成工程:
編寫流水燈程序
hal_entry.c 文件內容如下:
#include
#include "hal_data.h"
#include
#define USER1_PIN BSP_IO_PORT_02_PIN_09 /* Onboard LED pins /
#define USER2_PIN BSP_IO_PORT_02_PIN_10 / Onboard LED pins */
void hal_entry(void)
{
rt_kprintf("nHello RT-Thread!n");
while (1)
{
rt_pin_write(USER1_PIN, PIN_HIGH);
rt_pin_write(USER2_PIN, PIN_LOW);
rt_thread_mdelay(500);
rt_pin_write(USER1_PIN, PIN_LOW);
rt_pin_write(USER2_PIN, PIN_HIGH);
rt_thread_mdelay(500);
}
}
運行效果,兩個LED交替閃爍。
GPIO 輸入模式
我們配置TN0-1為中斷輸入模式,由于TN2沒有中斷輸入模式,所以用讀取IO的方法來實現。具體代碼如下:
void key_init(void)
{
/* 按鍵0引腳為輸入模式 /
rt_pin_mode(TN0_PIN, PIN_MODE_INPUT_PULLUP);
/ 綁定中斷,下降沿模式,回調函數名為beep_on /
rt_pin_attach_irq(TN0_PIN, PIN_IRQ_MODE_FALLING, led_off, RT_NULL);
/ 使能中斷 /
rt_pin_irq_enable(TN0_PIN, PIN_IRQ_ENABLE);
/ 按鍵1引腳為輸入模式 /
rt_pin_mode(TN1_PIN, PIN_MODE_INPUT_PULLUP);
/ 綁定中斷,下降沿模式,回調函數名為beep_on /
rt_pin_attach_irq(TN1_PIN, PIN_IRQ_MODE_FALLING, led_on, RT_NULL);
/ 使能中斷 /
rt_pin_irq_enable(TN1_PIN, PIN_IRQ_ENABLE);
/ 按鍵2引腳為輸入模式 */
rt_pin_mode(TN2_PIN, PIN_MODE_INPUT_PULLUP);
}
TN0、TN1的中斷函如下:
void led_off(void *args)
{
rt_kprintf("bnt0 down!n");
led_flash_state= 0;
}
void led_on(void *args)
{
rt_kprintf("bnt1 down!n");
led_flash_state= 1;
}
TN2的數據讀取如下:
if(rt_pin_read(TN2_PIN) == 0)
{
rt_thread_delay(10);//消抖
if(rt_pin_read(TN2_PIN) == 0)
led_flash_state = 2;
rt_kprintf("nTN2 push down!n");
}
創建閃燈任務如下,主要實現的是如果TN0按下,兩個LED燈常亮,TN2按下,兩個燈交替閃爍,TN1按下,兩個燈熄滅。
static rt_thread_t tid1 = RT_NULL;
/* 線程 1 的入口函數 */
static void thread_led_flash_entry(void *parameter)
{
static uint32_t flash_cnt;
while (1)
{
if (led_flash_state == 0) {
rt_pin_write(USER1_PIN, PIN_LOW);
rt_pin_write(USER2_PIN, PIN_LOW);
}
else if (led_flash_state == 1) {
rt_pin_write(USER1_PIN, PIN_HIGH);
rt_pin_write(USER2_PIN, PIN_HIGH);
}
else if (led_flash_state == 2) {
if(flash_cnt<50)
{
rt_pin_write(USER1_PIN, PIN_HIGH);
rt_pin_write(USER2_PIN, PIN_LOW);
}
else if(flash_cnt<100)
{
rt_pin_write(USER1_PIN, PIN_LOW);
rt_pin_write(USER2_PIN, PIN_HIGH);
}
else {
flash_cnt = 0;
}
flash_cnt ++;
}
rt_thread_mdelay(10);
}
}
整個hal_entry.c如下:
/*
Copyright (c) 2006-2023, RT-Thread Development Team
SPDX-License-Identifier: Apache-2.0
Change Logs:
Date Author Notes
2021-10-10 Sherman first version
/
#include
#include "hal_data.h"
#include
#define USER1_PIN BSP_IO_PORT_02_PIN_09 / Onboard LED pins /
#define USER2_PIN BSP_IO_PORT_02_PIN_10 / Onboard LED pins /
#define TN0_PIN BSP_IO_PORT_00_PIN_05 / Onboard TN0 pins /
#define TN1_PIN BSP_IO_PORT_00_PIN_06 / Onboard TN1 pins /
#define TN2_PIN BSP_IO_PORT_00_PIN_07 / Onboard TN2 pins */
static uint8_t led_flash_state =0;
void led_off(void *args)
{
rt_kprintf("bnt0 down!n");
led_flash_state= 0;
}
void led_on(void *args)
{
rt_kprintf("bnt1 down!n");
led_flash_state= 1;
}
#define THREAD_PRIORITY 25
#define THREAD_STACK_SIZE 512
#define THREAD_TIMESLICE 5
static rt_thread_t tid1 = RT_NULL;
/* 線程 1 的入口函數 */
static void thread_led_flash_entry(void *parameter)
{
static uint32_t flash_cnt;
while (1)
{
if (led_flash_state == 0) {
rt_pin_write(USER1_PIN, PIN_LOW);
rt_pin_write(USER2_PIN, PIN_LOW);
}
else if (led_flash_state == 1) {
rt_pin_write(USER1_PIN, PIN_HIGH);
rt_pin_write(USER2_PIN, PIN_HIGH);
}
else if (led_flash_state == 2) {
if(flash_cnt<50)
{
rt_pin_write(USER1_PIN, PIN_HIGH);
rt_pin_write(USER2_PIN, PIN_LOW);
}
else if(flash_cnt<100)
{
rt_pin_write(USER1_PIN, PIN_LOW);
rt_pin_write(USER2_PIN, PIN_HIGH);
}
else {
flash_cnt = 0;
}
flash_cnt ++;
}
rt_thread_mdelay(10);
}
}
void key_init(void)
{
/* 按鍵0引腳為輸入模式 */
rt_pin_mode(TN0_PIN, PIN_MODE_INPUT_PULLUP);
/* 綁定中斷,下降沿模式,回調函數名為beep_on */
rt_pin_attach_irq(TN0_PIN, PIN_IRQ_MODE_FALLING, led_off, RT_NULL);
/* 使能中斷 */
rt_pin_irq_enable(TN0_PIN, PIN_IRQ_ENABLE);
/* 按鍵1引腳為輸入模式 */
rt_pin_mode(TN1_PIN, PIN_MODE_INPUT_PULLUP);
/* 綁定中斷,下降沿模式,回調函數名為beep_on */
rt_pin_attach_irq(TN1_PIN, PIN_IRQ_MODE_FALLING, led_on, RT_NULL);
/* 使能中斷 */
rt_pin_irq_enable(TN1_PIN, PIN_IRQ_ENABLE);
/* 按鍵2引腳為輸入模式 */
rt_pin_mode(TN2_PIN, PIN_MODE_INPUT_PULLUP);
}
void hal_entry(void)
{
rt_kprintf("nHello RT-Thread!n");
key_init();
/* 創建線程 1,名稱是 thread1,入口是 thread1_entry*/
tid1 = rt_thread_create("thread_led",
thread_led_flash_entry, RT_NULL,
THREAD_STACK_SIZE,
THREAD_PRIORITY, THREAD_TIMESLICE);
/* 如果獲得線程控制塊,啟動這個線程 */
if (tid1 != RT_NULL)
rt_thread_startup(tid1);
while (1)
{
if(rt_pin_read(TN2_PIN) == 0)
{
rt_thread_delay(10);
if(rt_pin_read(TN2_PIN) == 0)
led_flash_state = 2;
rt_kprintf("nTN2 push down!n");
}
rt_thread_delay(10);
}
}
實現的效果就是,我們按TN0,兩個LED亮,按下TN1,兩個LED燈滅,按下TN2,兩個LED燈交替閃爍。
評論
查看更多