编写代码,LED1不停的亮灭间隔500ms, LED2等待按键S1按下亮,再按下灭,同时串口打印LED2的状态
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Iden
tifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-02-25 DYC first version
*/
#include 《rtthread.h》
#include “hal_data.h”
#include 《rtdevice.h》
#define LED1_PIN “P502” /* Onboard LED1 pins */
#define LED2_PIN “P501” /* Onboard LED2 pins */
#define USER_INPUT “P004”
rt_uint32_t led2_pin = 0;
void hal_entry(void)
{
rt_kprintf(“nHello RT-Thread!n”);
rt_uint32_t led1_pin = rt_pin_get(LED1_PIN);
while (1)
{
rt_pin_write(led1_pin, PIN_HIGH);
rt_thread_mdelay(500);
rt_pin_write(led1_pin, PIN_LOW);
rt_thread_mdelay(500);
}
}
void irq_callback_test(void *args)
{
rt_kprintf(“n IRQ03 triggered n”);
static int out ;
led2_pin = rt_pin_get(LED2_PIN);
out = rt_pin_read(led2_pin) ? PIN_LOW:PIN_HIGH;
if (PIN_LOW == out)
rt_kprintf(“n LED2 offn”);
else {
rt_kprintf(“n LED2 onn”);
}
rt_pin_write(led2_pin,out);
}
void icu_sample(void)
{
/* init */
rt_uint32_t pin = rt_pin_get(USER_INPUT);
rt_kprintf(“n pin number : 0x%04X n”, pin);
rt_err_t err = rt_pin_attach_irq(pin, PIN_IRQ_MODE_RISING, irq_callback_test, RT_NULL);
if (RT_EOK != err)
{
rt_kprintf(“n attach irq failed. n”);
}
err = rt_pin_irq_enable(pin, PIN_IRQ_ENABLE);
if (RT_EOK != err)
{
rt_kprintf(“n enable irq failed. n”);
}
}
MSH_CMD_EXPORT(icu_sample, icu sample);
编译下载到开发板,按S1是没有反应的,先在RT-Thread Studio 的终端打开串口
输入和代码中写的对应的入口 icu_sample,打开中断监听按键,这时候按一下LED2亮,再按一下灭同时终端上打印了LED2的状态
到这里就完成了GIOP的点灯操作和输入检测完毕。
原作者:快乐小鸟