接口/总线/驱动
开发板原理图
从原理图与开发板实物图,我们可以看到给用户使用的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灯交替闪烁。
全部0条评论
快来发表一下你的评论吧 !