本篇介绍RA4M2按键中断控制LED。主要使用了RA4M2的ICU外部中断模块,当然实现类似功能也可以直接通过读取按键引脚电平来做一个按键检测的功能来实现不同的亮灯逻辑。
实现功能:点按RA4M2试用板子的SW1,控制三个led灯的亮灯逻辑。默认上电后,三个灯一秒亮一秒灭,点按SW1后,切换成,三个灯轮流亮,间隔300ms。
bsp_io_level_t button1_status = BSP_IO_LEVEL_LOW; //全局
void button1_callback(external_irq_callback_args_t *p_args)
{
button1_status = ~button1_status;
}
#define LED_3 \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_04, BSP_IO_LEVEL_LOW); \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_05, BSP_IO_LEVEL_HIGH); \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_15, BSP_IO_LEVEL_LOW);
#define LED_2 \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_04, BSP_IO_LEVEL_HIGH); \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_05, BSP_IO_LEVEL_LOW); \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_15, BSP_IO_LEVEL_LOW);
#define LED_1 \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_04, BSP_IO_LEVEL_LOW); \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_05, BSP_IO_LEVEL_LOW); \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_15, BSP_IO_LEVEL_HIGH);
#define LED_HIGH \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_04, BSP_IO_LEVEL_HIGH); \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_05, BSP_IO_LEVEL_HIGH); \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_15, BSP_IO_LEVEL_HIGH);
#define LED_LOW \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_04, BSP_IO_LEVEL_LOW); \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_05, BSP_IO_LEVEL_LOW); \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_15, BSP_IO_LEVEL_LOW);
void IRQ_INIT(void)
{
R_ICU_ExternalIrqOpen(button1.p_ctrl, button1.p_cfg); //配置一个IRQ输入引脚与外部中断接口一起使用
R_ICU_ExternalIrqEnable(button1.p_ctrl); //在NVIC为指定通道启用外部中断
}
void hal_entry(void)
{
/* TODO: add your own code here */
IRQ_INIT(); //按键中断初始化
while (1)
{
if (button1_status)
{
LED_1;
R_BSP_SoftwareDelay(300, BSP_DELAY_UNITS_MILLISECONDS);
LED_2;
R_BSP_SoftwareDelay(300, BSP_DELAY_UNITS_MILLISECONDS);
LED_3;
R_BSP_SoftwareDelay(300, BSP_DELAY_UNITS_MILLISECONDS);
LED_2;
R_BSP_SoftwareDelay(300, BSP_DELAY_UNITS_MILLISECONDS);
}
else
{
LED_HIGH;
R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_SECONDS);
LED_LOW;
R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_SECONDS);
}
}
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
}
#include "hal_data.h"
FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER
bsp_io_level_t button1_status = BSP_IO_LEVEL_LOW;
#define LED_3 \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_04, BSP_IO_LEVEL_LOW); \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_05, BSP_IO_LEVEL_HIGH); \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_15, BSP_IO_LEVEL_LOW);
#define LED_2 \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_04, BSP_IO_LEVEL_HIGH); \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_05, BSP_IO_LEVEL_LOW); \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_15, BSP_IO_LEVEL_LOW);
#define LED_1 \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_04, BSP_IO_LEVEL_LOW); \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_05, BSP_IO_LEVEL_LOW); \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_15, BSP_IO_LEVEL_HIGH);
#define LED_HIGH \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_04, BSP_IO_LEVEL_HIGH); \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_05, BSP_IO_LEVEL_HIGH); \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_15, BSP_IO_LEVEL_HIGH);
#define LED_LOW \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_04, BSP_IO_LEVEL_LOW); \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_05, BSP_IO_LEVEL_LOW); \
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_15, BSP_IO_LEVEL_LOW);
void button1_callback(external_irq_callback_args_t *p_args)
{
(void) p_args;
button1_status = ~button1_status;
}
void IRQ_INIT(void)
{
R_ICU_ExternalIrqOpen(button1.p_ctrl, button1.p_cfg); //配置一个IRQ输入引脚与外部中断接口一起使用
R_ICU_ExternalIrqEnable(button1.p_ctrl); //在NVIC为指定通道启用外部中断
}
void hal_entry(void)
{
/* TODO: add your own code here */
IRQ_INIT(); //按键中断初始化
while (1)
{
if (button1_status)
{
LED_1;
R_BSP_SoftwareDelay(300, BSP_DELAY_UNITS_MILLISECONDS);
LED_2;
R_BSP_SoftwareDelay(300, BSP_DELAY_UNITS_MILLISECONDS);
LED_3;
R_BSP_SoftwareDelay(300, BSP_DELAY_UNITS_MILLISECONDS);
LED_2;
R_BSP_SoftwareDelay(300, BSP_DELAY_UNITS_MILLISECONDS);
}
else
{
LED_HIGH;
R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_SECONDS);
LED_LOW;
R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_SECONDS);
}
}
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
}
更多回帖