当前位置:网站首页>[play RT thread] RT thread Studio - key control motor forward and reverse rotation, buzzer
[play RT thread] RT thread Studio - key control motor forward and reverse rotation, buzzer
2022-07-07 12:22:00 【InfoQ】
One 、 First time to know RT-Thread
Be a world-class OS, Let everything be connected , Information flows freely .
Become the future AIoT The most mainstream operating system platform in the field .
1. brief introduction
Real-time operating system (RTOS) kernel 、 Middleware components and developer communities are integrated
Mr. Xiong Puxiang
Complete and rich components 、 Highly scalable 、 Simple development 、 Ultra-low power consumption 、 High security
Internet of things operating system
2. prospects
The largest embedded open source community in China
Independent development
Open source RTOS
3. Software ecology
Good software ecology
Two 、 Experiment preparation
- Programming tools :
RT-Thread studio
- Development board :
Pandora STM32L475
3、 ... and 、 The experimental requirements
- 1. Use the keys to control the buzzer and motor , When pressed KEY0 The rear motor turns left , When pressed KEY1 Rear motor
- Turn right , When pressed KEY2 The rear motor stops , When pressing WK_UP The buzzer sounds when , Release WK_UP The rear buzzer is off .
- 2. among KEY0 KEY1 KEY2 Three key interrupts are triggered , adopt pin The interrupt callback function of the device controls the motor ,WK_UP Press the key to control the buzzer by polling .
Four 、 Operation process
1. newly build RT-Thread engineering
![null](/img/c2/dc48eecd12564af03db63f52b51994.png)
2.RT-Thread Studio Interface is introduced
![null](/img/9c/00f977f38557734ec2b2302ba1412b.png)
3. Code writing
![null](/img/da/05ffce25060b9b77eb7c7ce3a667e6.png)
4. burn
![null](/img/5f/75549fc328d7ac51f8b97eef2c059d.png)
5. Serial port monitoring
![null](/img/15/ea7e4621f14613153ce340557ba26e.png)
5、 ... and 、 Code demonstration
1. The header file
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
2. Macro definition
// Key initialization
#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
// Motor initialization
#define PIN_MOTOR_A GET_PIN(A,1)//PA1:MOTOR_A
#define PIN_MOTOR_B GET_PIN(A,0)//PA0:MOTOR_B
// Buzzer initialization
#define PIN_BEEP GET_PIN(B,2)//PB2:BEEP
enum
{
MOTOR_STOP,
MOTOR_LEFT,
MOTOR_RIGHT
};
3.void motor_ctrl(rt_uint8_t turn) // Motor control function
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) // Buzzer control function
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) // Interrupt callback function
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. The main function
int main(void)
{
unsigned int count = 1;
/* Set the key pin to the input mode */
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);
/* Set the motor control pin to the input mode */
rt_pin_mode(PIN_MOTOR_A, PIN_MODE_OUTPUT);
rt_pin_mode(PIN_MOTOR_B, PIN_MODE_OUTPUT);
/* Set buzzer pin to output mode */
rt_pin_mode(PIN_BEEP, PIN_MODE_OUTPUT);
/* Set the key interrupt mode and interrupt callback function */
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
);
/* To interrupt */
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;
}
6、 ... and 、 Principle explanation
边栏推荐
- 人大金仓受邀参加《航天七〇六“我与航天电脑有约”全国合作伙伴大会》
- <No. 9> 1805. 字符串中不同整数的数目 (简单)
- Fleet tutorial 15 introduction to GridView Basics (tutorial includes source code)
- How to connect 5V serial port to 3.3V MCU serial port?
- 111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]
- 110. Network security penetration test - [privilege promotion 8] - [windows sqlserver xp_cmdshell stored procedure authorization]
- 盘点JS判断空对象的几大方法
- Flet tutorial 17 basic introduction to card components (tutorial includes source code)
- <No. 9> 1805. Number of different integers in the string (simple)
- [filter tracking] comparison between EKF and UKF based on MATLAB extended Kalman filter [including Matlab source code 1933]
猜你喜欢
SwiftUI 4 新功能之掌握 WeatherKit 和 Swift Charts
Swiftui swift internal skill how to perform automatic trigonometric function calculation in swift
Tutorial on principles and applications of database system (009) -- conceptual model and data model
Flet教程之 19 VerticalDivider 分隔符组件 基础入门(教程含源码)
Common locking table processing methods in Oracle
超标量处理器设计 姚永斌 第8章 指令发射 摘录
Unity 贴图自动匹配材质工具 贴图自动添加到材质球工具 材质球匹配贴图工具 Substance Painter制作的贴图自动匹配材质球工具
2022 年第八届“认证杯”中国高校风险管理与控制能力挑战赛
《通信软件开发与应用》课程结业报告
Matlab implementation of Huffman coding and decoding with GUI interface
随机推荐
An error occurred when vscade tried to create a file in the target directory: access denied [resolved]
Flet教程之 14 ListTile 基础入门(教程含源码)
Basic introduction to the 16 tabs tab control in the fleet tutorial (the tutorial includes source code)
30. Few-shot Named Entity Recognition with Self-describing Networks 阅读笔记
Flet教程之 18 Divider 分隔符组件 基础入门(教程含源码)
The road to success in R & D efficiency of 1000 person Internet companies
Flet tutorial 17 basic introduction to card components (tutorial includes source code)
111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]
数据库系统原理与应用教程(007)—— 数据库相关概念
【玩转 RT-Thread】 RT-Thread Studio —— 按键控制电机正反转、蜂鸣器
Will the filing free server affect the ranking and weight of the website?
What is a LAN domain name? How to parse?
[shortest circuit] acwing 1127 Sweet butter (heap optimized dijsktra or SPFA)
Swiftui swift internal skill how to perform automatic trigonometric function calculation in swift
Let digital manage inventory
SwiftUI 教程之如何在 2 秒内实现自动滚动功能
[filter tracking] strapdown inertial navigation simulation based on MATLAB [including Matlab source code 1935]
Completion report of communication software development and Application
Superscalar processor design yaoyongbin Chapter 8 instruction emission excerpt
如何理解服装产业链及供应链