当前位置:网站首页>[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
边栏推荐
- [filter tracking] comparison between EKF and UKF based on MATLAB extended Kalman filter [including Matlab source code 1933]
- Sonar:Cognitive Complexity认知复杂度
- Present pod information to the container through environment variables
- MATLAB实现Huffman编码译码含GUI界面
- Time bomb inside the software: 0-day log4shell is just the tip of the iceberg
- Flet教程之 14 ListTile 基础入门(教程含源码)
- Tutorial on principles and applications of database system (009) -- conceptual model and data model
- NPC Jincang was invited to participate in the "aerospace 706" I have an appointment with aerospace computer "national Partner Conference
- Fleet tutorial 14 basic introduction to listtile (tutorial includes source code)
- 超标量处理器设计 姚永斌 第10章 指令提交 摘录
猜你喜欢
Flet教程之 17 Card卡片组件 基础入门(教程含源码)
【全栈计划 —— 编程语言之C#】基础入门知识一文懂
Camera calibration (1): basic principles of monocular camera calibration and Zhang Zhengyou calibration
人大金仓受邀参加《航天七〇六“我与航天电脑有约”全国合作伙伴大会》
SwiftUI Swift 内功之如何在 Swift 中进行自动三角函数计算
UP Meta—Web3.0世界创新型元宇宙金融协议
powershell cs-UTF-16LE编码上线
软件内部的定时炸弹:0-Day Log4Shell只是冰山一角
zero-shot, one-shot和few-shot
2022 8th "certification Cup" China University risk management and control ability challenge
随机推荐
解决 Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
Sign up now | oar hacker marathon phase III midsummer debut, waiting for you to challenge
让数字管理好库存
免备案服务器会影响网站排名和权重吗?
H3C HCl MPLS layer 2 dedicated line experiment
TypeScript 接口继承
30. Few-shot Named Entity Recognition with Self-describing Networks 阅读笔记
NPC Jincang was invited to participate in the "aerospace 706" I have an appointment with aerospace computer "national Partner Conference
[texture feature extraction] LBP image texture feature extraction based on MATLAB local binary mode [including Matlab source code 1931]
Completion report of communication software development and Application
Rationaldmis2022 advanced programming macro program
ENSP MPLS layer 3 dedicated line
Fleet tutorial 15 introduction to GridView Basics (tutorial includes source code)
30. Feed shot named entity recognition with self describing networks reading notes
问题:先后键入字符串和字符,结果发生冲突
Epp+dis learning path (1) -- Hello world!
2022 8th "certification Cup" China University risk management and control ability challenge
<No. 8> 1816. 截断句子 (简单)
顶级域名有哪些?是如何分类的?
什么是局域网域名?如何解析?