当前位置:网站首页>【玩转 RT-Thread】 RT-Thread Studio —— 按键控制电机正反转、蜂鸣器
【玩转 RT-Thread】 RT-Thread Studio —— 按键控制电机正反转、蜂鸣器
2022-07-07 10:07:00 【InfoQ】
一、初识RT-Thread
做世界级的 OS,让万物互联,信息畅通无阻。
成为未来 AIoT 领域最为主流的操作系统平台。
1.简介
实时操作系统(RTOS)内核、中间件组件和开发者社区于一体
熊谱翔先生
组件完整丰富、高度可伸缩、简易开发、超低功耗、高安全性
物联网操作系统
2.前景
国内最大的嵌入式开源社区
自主开发
开源 RTOS
3.软件生态
良好的软件生态
二、实验准备
- 编程工具:
RT-Thread studio
- 开发板:
潘多拉STM32L475
三、实验需求
- 1.使用按键控制蜂鸣器和电机,当按下KEY0 后电机左转,当按下KEY1 后电机
- 右转,当按下KEY2 后电机停止,当按住WK_UP 时蜂鸣器鸣叫,松开WK_UP 后蜂鸣器关闭。
- 2.其中KEY0 KEY1 KEY2 三个按键会触发中断,通过pin 设备的中断回调函数控制电机,WK_UP 按键通过轮询的方式控制蜂鸣器鸣叫。
四、操作流程
1.新建RT-Thread工程
2.RT-Thread Studio界面介绍
3.代码编写
4.烧录
5.串口监视
五、代码演示
1.头文件
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
2.宏定义
//按键初始化
#define PIN_KEY0 GET_PIN(D, 10) // PD10: KEY0 --> KEY
#define PIN_KEY1 GET_PIN(D, 9) // PD9: KEY1 --> KEY
#define PIN_KEY2 GET_PIN(D, 8) // PD8: KEY2 --> KEY
#define PIN_WK_UP GET_PIN(C,13)//PC13:WK_UP
//电机初始化
#define PIN_MOTOR_A GET_PIN(A,1)//PA1:MOTOR_A
#define PIN_MOTOR_B GET_PIN(A,0)//PA0:MOTOR_B
//蜂鸣器初始化
#define PIN_BEEP GET_PIN(B,2)//PB2:BEEP
enum
{
MOTOR_STOP,
MOTOR_LEFT,
MOTOR_RIGHT
};
3.void motor_ctrl(rt_uint8_t turn) //电机控制函数
void motor_ctrl(rt_uint8_t turn)
{
if (turn == MOTOR_STOP)
{
rt_pin_write(PIN_MOTOR_A, PIN_LOW);
rt_pin_write(PIN_MOTOR_B, PIN_LOW);
}
else if (turn == MOTOR_LEFT)
{
rt_pin_write(PIN_MOTOR_A, PIN_LOW);
rt_pin_write(PIN_MOTOR_B, PIN_HIGH);
}
else if (turn == MOTOR_RIGHT)
{
rt_pin_write(PIN_MOTOR_A, PIN_HIGH);
rt_pin_write(PIN_MOTOR_B, PIN_LOW);
}
else
{
rt_kprintf("err parameter ! Please enter 0-2.");
}
}
4.void beep_ctrl(rt_uint8_t on) //蜂鸣器控制函数
void beep_ctrl(rt_uint8_t on)
{
if (on)
{
rt_pin_write(PIN_BEEP, PIN_HIGH);
}
else
{
rt_pin_write(PIN_BEEP, PIN_LOW);
}
}
5.void irq_callback(void *args) // 中断回调函数
void irq_callback(void *args)
{
rt_uint32_t sign = (rt_uint32_t)args;
switch (sign)
{
case PIN_KEY0:
motor_ctrl(MOTOR_LEFT);
rt_kprintf("KEY0 interrupt. motor turn left.");
break;
case PIN_KEY1:
motor_ctrl(MOTOR_RIGHT);
rt_kprintf("KEY1 interrupt. motor turn right.");
break;
case PIN_KEY2:
motor_ctrl(MOTOR_STOP);
rt_kprintf("KEY2 interrupt. motor stop.");
break;
default:
rt_kprintf("error sign= %d !", sign);
break;
}
}
6.主函数
int main(void)
{
unsigned int count = 1;
/* 设置按键引脚为输入模式*/
rt_pin_mode(PIN_KEY0, PIN_MODE_INPUT_PULLUP);
rt_pin_mode(PIN_KEY1, PIN_MODE_INPUT_PULLUP);
rt_pin_mode(PIN_KEY2, PIN_MODE_INPUT_PULLUP);
rt_pin_mode(PIN_WK_UP, PIN_MODE_INPUT_PULLDOWN);
/* 设置电机控制引脚为输入模式*/
rt_pin_mode(PIN_MOTOR_A, PIN_MODE_OUTPUT);
rt_pin_mode(PIN_MOTOR_B, PIN_MODE_OUTPUT);
/* 设置蜂鸣器引脚为输出模式*/
rt_pin_mode(PIN_BEEP, PIN_MODE_OUTPUT);
/* 设置按键中断模式与中断回调函数*/
rt_pin_attach_irq(PIN_KEY0, PIN_IRQ_MODE_FALLING , irq_callback , (void *)PIN_KEY0
);
rt_pin_attach_irq(PIN_KEY1, PIN_IRQ_MODE_FALLING , irq_callback , (void *)PIN_KEY1
);
rt_pin_attach_irq(PIN_KEY2, PIN_IRQ_MODE_FALLING , irq_callback , (void *)PIN_KEY2
);
/* 使能中断*/
rt_pin_irq_enable(PIN_KEY0, PIN_IRQ_ENABLE);
rt_pin_irq_enable(PIN_KEY1, PIN_IRQ_ENABLE);
rt_pin_irq_enable(PIN_KEY2, PIN_IRQ_ENABLE);
while (count > 0)
{
if (rt_pin_read(PIN_WK_UP) == PIN_HIGH)
{
rt_thread_mdelay(50);
if (rt_pin_read(PIN_WK_UP) == PIN_HIGH)
{
rt_kprintf("WK_UP pressed. beep on.");
beep_ctrl(1);
}
}
else
{
beep_ctrl(0);
}
rt_thread_mdelay(10);
count++;
}
return 0;
}
六、原理讲解
边栏推荐
- How to connect 5V serial port to 3.3V MCU serial port?
- Le Cluster kubernets en cours d'exécution veut ajuster l'adresse du segment réseau du pod
- 超标量处理器设计 姚永斌 第9章 指令执行 摘录
- 相机标定(1): 单目相机标定及张正友标定基本原理
- [shortest circuit] acwing1128 Messenger: Floyd shortest circuit
- Onedns helps college industry network security
- Automated testing framework
- What are the top-level domain names? How is it classified?
- In my limited software testing experience, a full-time summary of automation testing experience
- Summed up 200 Classic machine learning interview questions (with reference answers)
猜你喜欢
Complete collection of common error handling in MySQL installation
111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]
【纹理特征提取】基于matlab局部二值模式LBP图像纹理特征提取【含Matlab源码 1931期】
Unity 贴图自动匹配材质工具 贴图自动添加到材质球工具 材质球匹配贴图工具 Substance Painter制作的贴图自动匹配材质球工具
Enclosed please find. Net Maui's latest learning resources
110.网络安全渗透测试—[权限提升篇8]—[Windows SqlServer xp_cmdshell存储过程提权]
[data clustering] realize data clustering analysis based on multiverse optimization DBSCAN with matlab code
@Bean与@Component用在同一个类上,会怎么样?
HCIA复习整理
Solve the problem that vscode can only open two tabs
随机推荐
Blog moved to Zhihu
Enclosed please find. Net Maui's latest learning resources
Suggestions on one-stop development of testing life
问题:先后键入字符串和字符,结果发生冲突
Steps of redis installation and self startup configuration under CentOS system
What are the top-level domain names? How is it classified?
Mastering the new functions of swiftui 4 weatherkit and swift charts
When sink is consumed in mysql, the self incrementing primary key has been set in the database table. How to operate in Flink?
[extraction des caractéristiques de texture] extraction des caractéristiques de texture de l'image LBP basée sur le mode binaire local de Matlab [y compris le code source de Matlab 1931]
About how to install mysql8.0 on the cloud server (Tencent cloud here) and enable local remote connection
SwiftUI Swift 内功之如何在 Swift 中进行自动三角函数计算
Superscalar processor design yaoyongbin Chapter 10 instruction submission excerpt
<No. 8> 1816. Truncate sentences (simple)
zero-shot, one-shot和few-shot
What are the technical differences in source code anti disclosure
Introduction to three methods of anti red domain name generation
[neural network] convolutional neural network CNN [including Matlab source code 1932]
108.网络安全渗透测试—[权限提升篇6]—[Windows内核溢出提权]
一起探索云服务之云数据库
Visual Studio 2019 (LocalDB)\MSSQLLocalDB SQL Server 2014 数据库版本为852无法打开,此服务器支持782版及更低版本