RT-Thread 学习记录(1)-LED,按键,电机,蜂鸣器

本文介绍了在RT-threadStudio环境中使用STM32L475VET6开发板实现LEDRGB闪烁、按键控制LED及蜂鸣器/电机控制的实例,包括了代码解读和基本中断处理。
摘要由CSDN通过智能技术生成

今天开始学习RT-thread,努力把自己的学习过程中遇到的bug和问题记录下来.

开发板:正点原子STM32L475VET6潘多拉开发板

平台:RT thread stduio

因为开发板的例程是基于MDK keil5平台写的,所以我选择在RT thread stduio中自己按着例程的介绍自己敲一遍,在这其中会加入我个人的一些要求.

例程1 LED RGB闪烁

这里利用了一个二维数组来循环RGB灯组的状态,利用取余%的操作循环0-7,可以学习.

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>

/* */
#define LEDR    GET_PIN(E, 7)
#define LEDG    GET_PIN(E, 8)
#define LEDB    GET_PIN(E, 9)

rt_uint8_t blink[][3]={
        {PIN_HIGH,PIN_HIGH,PIN_HIGH},
        {PIN_LOW,PIN_HIGH,PIN_HIGH},
        {PIN_HIGH,PIN_LOW,PIN_HIGH},
        {PIN_HIGH,PIN_HIGH,PIN_LOW},
        {PIN_LOW,PIN_LOW,PIN_HIGH},
        {PIN_HIGH,PIN_LOW,PIN_LOW},
        {PIN_LOW,PIN_HIGH,PIN_LOW},
        {PIN_LOW,PIN_LOW,PIN_LOW}

};


int main(void)
{
    /* set LED0 pin mode to output */
    static uint16_t count=0;
    uint16_t num;
    rt_pin_mode(LEDR, PIN_MODE_OUTPUT);
    rt_pin_mode(LEDG, PIN_MODE_OUTPUT);
    rt_pin_mode(LEDB, PIN_MODE_OUTPUT);
    //rt_pin_write(LEDR, PIN_HIGH);
    //rt_pin_write(LEDG, PIN_HIGH);
    //rt_pin_write(LEDB, PIN_HIGH);
    while (1)
    {
        num=count%8;
        rt_pin_write(LEDR, blink[num][0]);
        rt_pin_write(LEDG, blink[num][1]);
        rt_pin_write(LEDB, blink[num][2]);
        rt_thread_mdelay(400);
        count++;
    }
}

例程2 按键控制LED

这里主要习惯下rtthread的引脚初始化.

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>

/* 定义引脚 */
#define LEDR    GET_PIN(E, 7)
#define LEDG    GET_PIN(E, 8)
#define LEDB    GET_PIN(E, 9)
#define key0    GET_PIN(D, 10)
#define key1    56
#define key2    55

int main(void)
{
    /* 设置引脚输入输出 */
    rt_pin_mode(LEDR, PIN_MODE_OUTPUT);
    rt_pin_mode(LEDG, PIN_MODE_OUTPUT);
    rt_pin_mode(LEDB, PIN_MODE_OUTPUT);
    rt_pin_mode(key0, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(key1, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(key2, PIN_MODE_INPUT_PULLUP);

    while (1)
    {
        if(rt_pin_read(key0)==PIN_LOW){
            rt_thread_mdelay(50);
            if(rt_pin_read(key0)==PIN_LOW){
                printf("key has been down");
                rt_pin_write(LEDB, PIN_LOW);

            }

        }else {
            printf("key is high");
            rt_pin_write(LEDB, PIN_HIGH);
        }

        rt_thread_mdelay(100);
    }
}

例程3 按键控制蜂鸣器,电机正反转和停止

这里了解了RTthread的按键绑定中断,从pin设备文档中了解到绑定函数和使能函数:

所以初始化流程就是:配置输入输出模式,绑定中断函数,使能中断.

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>

/* 电机和蜂鸣器引脚*/
#define IA    GET_PIN(A, 1)
#define IB    GET_PIN(A, 0)
#define BEEP    GET_PIN(B, 2)
//按键引脚
#define KEY0    GET_PIN(D, 10)
#define KEY1    GET_PIN(D, 9)
#define KEY2    GET_PIN(D, 8)

int16_t ll=0,rr=0,ss=0;
//中断函数
void control(void *type){
    uint32_t type1=type;
    switch (type1) {
        case KEY0:
                if(ll==0){
                    rt_pin_write(IA, PIN_LOW);
                    rt_pin_write(IB, PIN_HIGH);
                    ll=1;
                }else {
                    rt_pin_write(IA, PIN_LOW);
                    rt_pin_write(IB, PIN_LOW);
                    ll=0;
                }
                HAL_Delay(50);
            break;
        case KEY1:
            if(rr==0){
               rt_pin_write(IA, PIN_HIGH);
               rt_pin_write(IB, PIN_LOW);
               rr=1;
            }else {
                rt_pin_write(IA, PIN_LOW);
                rt_pin_write(IB, PIN_LOW);
                rr=0;
            }
            HAL_Delay(50);
            break;
        case KEY2:
            if (ss==0) {
                rt_pin_write(BEEP, PIN_HIGH);
                ss=1;
            }else {
                rt_pin_write(BEEP, PIN_LOW);
                ss=0;
            }
            HAL_Delay(50);
            break;
        default:
            break;
    }

}

int main(void)
{
    /* 设置模式 */
    rt_pin_mode(IA, PIN_MODE_OUTPUT);
    rt_pin_mode(IB, PIN_MODE_OUTPUT);
    rt_pin_mode(BEEP, PIN_MODE_OUTPUT);

    rt_pin_mode(KEY0, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(KEY1, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(KEY2, PIN_MODE_INPUT_PULLUP);
    //绑定中断函数
    rt_pin_attach_irq(KEY0, PIN_IRQ_MODE_FALLING, control, KEY0);
    rt_pin_attach_irq(KEY1, PIN_IRQ_MODE_FALLING, control, KEY1);
    rt_pin_attach_irq(KEY2, PIN_IRQ_MODE_FALLING, control, KEY2);
    //使能中断
    rt_pin_irq_enable(KEY0, PIN_IRQ_ENABLE);
    rt_pin_irq_enable(KEY1, PIN_IRQ_ENABLE);
    rt_pin_irq_enable(KEY2, PIN_IRQ_ENABLE);

    while (1)
    {

        rt_thread_mdelay(500);

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值