当前位置:网站首页>[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
2.RT-Thread Studio Interface is introduced
3. Code writing
4. burn
5. Serial port monitoring
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
边栏推荐
- [shortest circuit] acwing 1127 Sweet butter (heap optimized dijsktra or SPFA)
- MATLAB實現Huffman編碼譯碼含GUI界面
- SwiftUI 4 新功能之掌握 WeatherKit 和 Swift Charts
- 牛客网刷题网址
- What are the technical differences in source code anti disclosure
- @Bean与@Component用在同一个类上,会怎么样?
- Let digital manage inventory
- 人大金仓受邀参加《航天七〇六“我与航天电脑有约”全国合作伙伴大会》
- Ask about the version of flinkcdc2.2.0, which supports concurrency. Does this concurrency mean Multiple Parallelism? Now I find that mysqlcdc is full
- SwiftUI Swift 内功之 Swift 中使用不透明类型的 5 个技巧
猜你喜欢
110. Network security penetration test - [privilege promotion 8] - [windows sqlserver xp_cmdshell stored procedure authorization]
ES底层原理之倒排索引
Review and arrangement of HCIA
消息队列消息丢失和消息重复发送的处理策略
Visual studio 2019 (localdb) \mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports version 782 and earlier
《通信软件开发与应用》课程结业报告
Up meta - Web3.0 world innovative meta universe financial agreement
[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]
Mise en œuvre du codage Huffman et du décodage avec interface graphique par MATLAB
千人规模互联网公司研发效能成功之路
随机推荐
Epp+dis learning road (2) -- blink! twinkle!
Rationaldmis2022 array workpiece measurement
112. Network security penetration test - [privilege promotion article 10] - [Windows 2003 lpk.ddl hijacking rights lifting & MSF local rights lifting]
Sign up now | oar hacker marathon phase III midsummer debut, waiting for you to challenge
【纹理特征提取】基于matlab局部二值模式LBP图像纹理特征提取【含Matlab源码 1931期】
问题:先后键入字符串和字符,结果发生冲突
关于 Web Content-Security-Policy Directive 通过 meta 元素指定的一些测试用例
[filter tracking] comparison between EKF and UKF based on MATLAB extended Kalman filter [including Matlab source code 1933]
Xiaohongshu microservice framework and governance and other cloud native business architecture evolution cases
源代码防泄密中的技术区别再哪里
(待会删)yyds,付费搞来的学术资源,请低调使用!
When OSPF specifies that the connection type is P2P, it enables devices on both ends that are not in the same subnet to Ping each other
【紋理特征提取】基於matlab局部二值模式LBP圖像紋理特征提取【含Matlab源碼 1931期】
【滤波跟踪】捷联惯导纯惯导解算matlab实现
从工具升级为解决方案,有赞的新站位指向新价值
18 basic introduction to divider separator component of fleet tutorial (tutorial includes source code)
超标量处理器设计 姚永斌 第9章 指令执行 摘录
如何理解服装产业链及供应链
Explore cloud database of cloud services together
powershell cs-UTF-16LE编码上线