k230读取上升沿

Viewed 39

问题描述


使能成功后,读不到上升沿,以下是代码
参数定义
static drv_gpio_inst_t* g_gpio_insts[64] = {NULL};
static volatile uint32_t g_count_io5 = 0;
static volatile uint32_t g_count_io6 = 0;
static drv_gpio_inst_t* g_inst5 = NULL;
static drv_gpio_inst_t* g_inst6 = NULL;

// 2. 回调函数
void handler_5(void* args) { g_count_io5++; }
void handler_6(void* args) { g_count_io6++; }

//使能
int setup_gpio_pulse_counter(int pin, drv_gpio_inst_t** inst, gpio_irq_callback cb) {
// A. 映射引脚
drv_fpioa_set_pin_func(pin, (fpioa_func_t)(GPIO0 + pin));

// B. 创建实例
if (0 != drv_gpio_inst_create(pin, inst)) {
    printf("[Error] Pin %d 实例创建失败\n", pin);
    return -1;
}

// C. 【关键】强制注销之前可能残留的中断,解决 "Already attached"
drv_gpio_unregister_irq(*inst);

// D. 设置为输入下拉模式
// 编码器 A 相上升沿需要从 0 跳变到 1,下拉能保证没信号时电平稳定在 0
drv_gpio_mode_set(*inst, GPIO_DM_INPUT);

// E. 注册中断:10ms 是驱动要求的最小去抖时间
if (0 != drv_gpio_register_irq(*inst, GPIO_PE_RISING, 10, cb, NULL)) {
    printf("[Error] Pin %d 中断注册失败\n", pin);
    return -1;
}
return 0;

}

主函数调用
if (setup_gpio_pulse_counter(5, &g_inst5, handler_5) != 0) return -1;

// 初始化 IO 6
if (setup_gpio_pulse_counter(6, &g_inst6, handler_6) != 0) return -1;
 printf("IO5 Count: %u | IO6 Count: %u\n", g_count_io5, g_count_io6);

硬件板卡


k230

软件版本


rtsmart

其他信息


image.png这个Count一直是0,我用引脚先接3.3再接gnd也没有进行计数

1 Answers

你好,可以尝试把引脚模式修改为GPIO_DM_INPUT_PULLDOWN

找到问题了,中断没使能

好的。