目录
今天要介绍的是有关独立按键的知识,简单来说,非常简单,所以就不详细介绍了。
https://player.bilibili.com/player.html?aid=733631244
独立按键
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
int main(void)
{
unsigned int count = 1;
/* 设置 RGB 灯引脚的模式为输出模式 */
rt_pin_mode(PIN_LED_R, PIN_MODE_OUTPUT);
rt_pin_mode(PIN_LED_G, PIN_MODE_OUTPUT);
rt_pin_mode(PIN_LED_B, PIN_MODE_OUTPUT);
/* 设置 KEY 引脚的模式为输入模式 */
rt_pin_mode(PIN_KEY0, PIN_MODE_INPUT);
rt_pin_mode(PIN_KEY1, PIN_MODE_INPUT);
rt_pin_mode(PIN_KEY2, PIN_MODE_INPUT);
while (count > 0)
{
/* 读取按键 KEY 的引脚状态 */
if (rt_pin_read(PIN_KEY0) == PIN_LOW)
{
rt_thread_mdelay(50);
if (rt_pin_read(PIN_KEY0) == PIN_LOW)
{
/* 按键已被按下,输出 log,点亮 LED 灯 */
LOG_D("KEY0 pressed!");
rt_pin_write(PIN_LED_R, PIN_LOW);
}
}
else if (rt_pin_read(PIN_KEY1) == PIN_LOW)
{
rt_thread_mdelay(50);
if (rt_pin_read(PIN_KEY1) == PIN_LOW)
{
/* 按键已被按下,输出 log,点亮 LED 灯 */
LOG_D("KEY1 pressed!");
rt_pin_write(PIN_LED_G, PIN_LOW);
}
}
else if (rt_pin_read(PIN_KEY2) == PIN_LOW)
{
rt_thread_mdelay(50);
if (rt_pin_read(PIN_KEY2) == PIN_LOW)
{
/* 按键已被按下,输出 log,点亮 LED 灯 */
LOG_D("KEY2 pressed!");
rt_pin_write(PIN_LED_B, PIN_LOW);
}
}
else
{
/* 按键没被按下,熄灭 LED 灯 */
rt_pin_write(PIN_LED_R, PIN_HIGH);
rt_pin_write(PIN_LED_G, PIN_HIGH);
rt_pin_write(PIN_LED_B, PIN_HIGH);
}
rt_thread_mdelay(10);
count++;
}
return 0;
}
总结
下一个。