当前位置:网站首页>[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 RTOS3. 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
边栏推荐
- Problem: the string and characters are typed successively, and the results conflict
- SwiftUI 4 新功能之掌握 WeatherKit 和 Swift Charts
- 2022年在启牛开华泰的账户安全吗?
- 问题:先后键入字符串和字符,结果发生冲突
- Swiftui swift internal skill how to perform automatic trigonometric function calculation in swift
- Baidu digital person Du Xiaoxiao responded to netizens' shouts online to meet the Shanghai college entrance examination English composition
- 111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]
- [texture feature extraction] LBP image texture feature extraction based on MATLAB local binary mode [including Matlab source code 1931]
- NPC Jincang was invited to participate in the "aerospace 706" I have an appointment with aerospace computer "national Partner Conference
- 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 中进行自动三角函数计算

数据库系统原理与应用教程(009)—— 概念模型与数据模型

powershell cs-UTF-16LE编码上线

Cmu15445 (fall 2019) project 2 - hash table details

Sort out the garbage collection of JVM, and don't involve high-quality things such as performance tuning for the time being

Completion report of communication software development and Application

VSCode的学习使用

【玩转 RT-Thread】 RT-Thread Studio —— 按键控制电机正反转、蜂鸣器

wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6

超标量处理器设计 姚永斌 第8章 指令发射 摘录
随机推荐
超标量处理器设计 姚永斌 第9章 指令执行 摘录
2022年在启牛开华泰的账户安全吗?
NPC Jincang was invited to participate in the "aerospace 706" I have an appointment with aerospace computer "national Partner Conference
Swiftui swift internal skill: five skills of using opaque type in swift
Tutorial on principles and applications of database system (009) -- conceptual model and data model
ENSP MPLS layer 3 dedicated line
<No. 9> 1805. 字符串中不同整数的数目 (简单)
UP Meta—Web3.0世界创新型元宇宙金融协议
Visual Studio 2019 (LocalDB)\MSSQLLocalDB SQL Server 2014 数据库版本为852无法打开,此服务器支持782版及更低版本
如何理解服装产业链及供应链
百度数字人度晓晓在线回应网友喊话 应战上海高考英语作文
powershell cs-UTF-16LE编码上线
[shortest circuit] acwing 1127 Sweet butter (heap optimized dijsktra or SPFA)
An error occurred when vscade tried to create a file in the target directory: access denied [resolved]
2022 8th "certification Cup" China University risk management and control ability challenge
108. Network security penetration test - [privilege escalation 6] - [windows kernel overflow privilege escalation]
NGUI-UILabel
Typescript interface inheritance
Upgrade from a tool to a solution, and the new site with praise points to new value
VSCode的学习使用