瑞萨单片机william hill官网
直播中

jf_23522415

2年用户 65经验值
擅长:可编程逻辑 嵌入式技术
私信 关注
[经验]

【瑞萨RA4系列开发板体验】+ 外部中断模块的使用 + 点LED

本篇介绍RA4M2按键中断控制LED。主要使用了RA4M2的ICU外部中断模块,当然实现类似功能也可以直接通过读取按键引脚电平来做一个按键检测的功能来实现不同的亮灯逻辑。

实现功能:点按RA4M2试用板子的SW1,控制三个led灯的亮灯逻辑。默认上电后,三个灯一秒亮一秒灭,点按SW1后,切换成,三个灯轮流亮,间隔300ms。

新建工程

  1. 新建工程

image-20221115171944752

  1. image-20221115172124267
  2. 填写好项目名称后点击下一步
    image-20221115172208364
  3. 选择对应的芯片类型,下一步
    image-20221115172258470
  4. image-20221115172327678
  5. 点击完成
    image-20221115172352320

配置

  1. 打开configuration.xml,右边窗口选中Stacks卡片,添加一个r_icu外部中断控制
    image-20221115172631767
  2. 点击新建项,在属性窗口会更新可配置的内容,依次配置
    image-20221115172841491
  3. 绑定引脚
    image-20221115173344775
  4. image-20221115173412778
  5. 绑定好后,回到Pins选项卡
    image-20221115173536456
  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
}
​
​

回帖(2)

jf_23522415

2022-11-15 21:24:57
  • 视频补充,

举报

jf_23522415

2022-11-16 08:59:25
由于图床连接不稳定,有些图片显示不全,在附件的pdf中有完整示例
举报
附件: 您需要登录才可以下载或查看附件。没有帐号?注册

更多回帖

发帖
×
20
完善资料,
赚取积分