本帖最后由 dirtwillfly 于 2025-6-22 13:46 编辑
4. hpm5361软件部分
在app_reed_switch.c文件内:
使用#include包含相关的头文件
- #include <rtthread.h>
- #include <rtdevice.h>
- #include "app_reed_switch.h"
- #include <drv_gpio.h>
- #include <hpm_soc.h>
使用宏定义获取PB11对应的pin:
- #define REED_SWITCH_PIN GET_PIN(B, 11)
初始化gpio,这里配置io为输入模式,配置了中断为下降沿触发,以及触发时的回调函数为reed_switch_irq() ,并使能了中断。
- //reed_switch GPIO初始化
- void reed_switch_init(void)
- {
- rt_pin_mode(REED_SWITCH_PIN, PIN_MODE_INPUT);
- rt_pin_attach_irq(REED_SWITCH_PIN, PIN_IRQ_MODE_FALLING, reed_switch_irq, RT_NULL);
- rt_pin_irq_enable(REED_SWITCH_PIN, PIN_IRQ_ENABLE);
- }
中断回调函数:
- //中断回调函数
- void reed_switch_irq(void *parameter)
- {
- rt_kprintf("reed switch is triggered\n");
- }
在在app_reed_switch.h文件内:
- #ifndef APPLICATIONS_APP_REED_SWITCH_H_
- #define APPLICATIONS_APP_REED_SWITCH_H_
- void reed_switch_init(void);
- #endif /* APPLICATIONS_APP_REED_SWITCH_H_ */
最后,在main函数,先添加上包含app_reed_switch.h头文件:
- #include "app_reed_switch.h"
然后在main函数中调用reed_switch_init()
|