本篇介绍RA4M2按键中断控制LED。主要使用了RA4M2的ICU外部中断模块,当然实现类似功能也可以直接通过读取按键引脚电平来做一个按键检测的功能来实现不同的亮灯逻辑。
实现功能:点按RA4M2试用板子的SW1,控制三个led灯的亮灯逻辑。默认上电后,三个灯一秒亮一秒灭,点按SW1后,切换成,三个灯轮流亮,间隔300ms。
新建工程
- 新建工程
- 填写好项目名称后点击下一步
- 选择对应的芯片类型,下一步
- 点击完成
配置
- 打开configuration.xml,右边窗口选中Stacks卡片,添加一个r_icu外部中断控制
- 点击新建项,在属性窗口会更新可配置的内容,依次配置
- 绑定引脚
- 绑定好后,回到Pins选项卡
- 分别配置p404 405 415三个io口的Mode模式为:Output mode (initial low)
- ctrl + s保存,到这一步基本配置好了,接下来写调用逻辑。点击右上角generate project content。
编写逻辑
- 打开项目资源管理器中src文件夹下hal_entery.c文件,准备添加内容。
- 添加中断回调的内容
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);
- 添加IRQ初始化函数
void IRQ_INIT(void)
{
R_ICU_ExternalIrqOpen(button1.p_ctrl, button1.p_cfg);
R_ICU_ExternalIrqEnable(button1.p_ctrl);
}
- 主代码
void hal_entry(void)
{
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
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);
R_ICU_ExternalIrqEnable(button1.p_ctrl);
}
void hal_entry(void)
{
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
R_BSP_NonSecureEnter();
#endif
}
|