本帖最后由 lulugl 于 2023-3-12 11:08 编辑
按键控制,是单片最常用的功能。SDK在\APM32F10x_SDK_V1.8\Examples\EINT\EINT_Config示例中做出了按键的DEMO,我这里向大家介绍用RT-Thread的PIN来实现key的按键控制LED灯。
1、在的rtthred-master-bsp中打开keil工程:
2、在工程的apllcations下面新建key.c,并把key.c加入工程中:
3、key.c内容如下:#include "key.h"
#include <rtthread.h>
#include <rtdevice.h>
#define KEY2_PIN_NUM 1 //PA01
#define LED3_PIN_NUM 69 //PE05
static void led_toggle(void *args)
{
uint8_t start_tick = 0;
start_tick = rt_tick_get()+20;
while(rt_tick_get()<start_tick);
if(rt_pin_read(KEY2_PIN_NUM) == PIN_LOW)
{
rt_pin_write(LED3_PIN_NUM, !rt_pin_read(LED3_PIN_NUM));
}
}
static void key_test(void)
{
rt_pin_mode(LED3_PIN_NUM,PIN_MODE_OUTPUT);
rt_pin_write(LED3_PIN_NUM,PIN_LOW);
rt_pin_mode(KEY2_PIN_NUM, PIN_IRQ_MODE_FALLING);
rt_pin_attach_irq(KEY2_PIN_NUM, PIN_IRQ_MODE_FALLING, led_toggle, RT_NULL);
rt_pin_irq_enable(KEY2_PIN_NUM,PIN_IRQ_ENABLE);
}
MSH_CMD_EXPORT(key_test,key test);
4、骗译下载,打开终端控制台,执行key_test
\ | /
- RT - Thread Operating System
/ | \ 5.0.0 build Mar 12 2023 10:21:19
2006 - 2022 Copyright by RT-Thread team
System Clock: 72000000
msh >ek
msh >ke
key_test
msh >key_test
msh >
5、按下KEY1就可以实现控制LED3的翻转。
|