当前位置:网站首页>[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

RT-Thread  It's a collection
Real-time operating system (RTOS) kernel 、 Middleware components and developer communities are integrated
Technology platform , from
Mr. Xiong Puxiang
Lead and gather the strength of the open source community to develop ,RT-Thread  Also a
Complete and rich components 、 Highly scalable 、 Simple development 、 Ultra-low power consumption 、 High security
Of
Internet of things operating system
.

2. prospects

RT-Thread  Have a  IoT OS  All the key components required for the platform , for example GUI、 Network protocol stack 、 Secure transport 、 Low power components and so on . after 11 Years of cumulative development ,RT-Thread  Already have a
The largest embedded open source community in China
, At the same time, it is widely used in energy 、 vehicle 、 Medical care 、 Consumer electronics and many other industries , The cumulative installed capacity exceeds  14 Billion   platform , Become a Chinese
Independent development
、 The most mature and stable in China with the largest installed capacity
Open source  RTOS
.

3. Software ecology

RT-Thread  Have
Good software ecology
, Support all the mainstream compiler tools on the market, such as  GCC、Keil、IAR  etc. , Perfect tool chain 、 friendly , Support all kinds of standard interfaces , Such as  POSIX、CMSIS、C++ Application environment 、Javascript  Execution environment, etc , It's convenient for developers to transplant all kinds of applications . Business supports all mainstream MCU framework , Such as  ARM Cortex-M/R/A, MIPS, X86, Xtensa, C-Sky, RISC-V, Almost all the mainstream  MCU  and  Wi-Fi  chip .

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

2.RT-Thread Studio Interface is introduced

null

3. Code writing

null

4. burn

null

5. Serial port monitoring

null

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(&quot;err parameter ! Please enter 0-2.&quot;);
 }
}

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(&quot;KEY0 interrupt. motor turn left.&quot;);
 break;
 case PIN_KEY1:
 motor_ctrl(MOTOR_RIGHT);
 rt_kprintf(&quot;KEY1 interrupt. motor turn right.&quot;);
 break;
 case PIN_KEY2:
 motor_ctrl(MOTOR_STOP);
 rt_kprintf(&quot;KEY2 interrupt. motor stop.&quot;);
 break;
 default:
 rt_kprintf(&quot;error sign= %d !&quot;, 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(&quot;WK_UP pressed. beep on.&quot;);
 beep_ctrl(1);
 }
 }
 else
 {
 beep_ctrl(0);
 }
 rt_thread_mdelay(10);
 count++;
 }

 return 0;
}

6、 ... and 、 Principle explanation

Through the key pin 、 Input and output modes of motor and buzzer , And set the interrupt for the key, and write the interrupt callback function , After the enable interrupt .

1. Motor control : When there is an external event, trigger the pin status ( Press the button ) when , The interrupt callback function judges the specific trigger pin , And carry out the corresponding operation

2. Buzzer control : Loop in the main function to determine whether WK_UP Press the key , Press to trigger the buzzer to sound , Release and stop the sound .



原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071007170143.html

随机推荐