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