You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

BeagleBone Black多按钮GPIO读取异常:按下一按钮三通道电压均变化

GPIO多按钮读取异常问题

我编写了用于初始化GPIO并从文件描述符读取按钮状态的代码,共连接3个按钮。代码逻辑为:先将3个GPIO引脚配置为输入模式,随后在无限循环中为每个按钮打开对应文件描述符并读取状态值。

目前遇到的问题:按下任意一个按钮时,三个GPIO通道的电压均发生变化;仅连接单个按钮时功能正常。

附上DSView逻辑分析仪截图及代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>

#define GPIO_BUTTON_IN 66
#define GPIO_BUTTON_OUT 67
#define GPIO_BUTTON_NEW 68

int GPIO_init(int pinNumber, const char *direction);
int GPIO_read(int pinNumber);
int GPIO_open(int pinNumber);
void GPIO_close(int gpio_fd);

int GPIO_init(int pinNumber, const char *direction)
{
    char buffer[50];
    char direction_path[50];
    snprintf(buffer, sizeof(buffer), "/sys/class/gpio/gpio%d", pinNumber);

    // Check if the GPIO folder already exists
    if (access(buffer, F_OK) != 0)
    {
        // If it doesn't exist, create the GPIO folder
        snprintf(buffer, sizeof(buffer), "echo %d > /sys/class/gpio/export", pinNumber);
        system(buffer);
    }

    snprintf(direction_path, sizeof(direction_path), "/sys/class/gpio/gpio%d/direction", pinNumber);
    printf("direction_path = %s\n\r", direction_path);
    int gpio_fd = open(direction_path, O_WRONLY);
    if (gpio_fd == -1)
    {
        perror("Error opening GPIO direction");
        return 0;
    }

    // Set the GPIO direction
    write(gpio_fd, direction, strlen(direction));
    close(gpio_fd);

    printf("GPIO direction set to %s for pin %d\n", direction, pinNumber);

    return 1;
}

int GPIO_read(int gpio_fd)
{
    char value;
    int bytesRead = read(gpio_fd, &value, 1);
    if (bytesRead == -1)
    {
        perror("Error reading GPIO value");
        return -1;
    }
    return (value == '1') ? 1 : 0;
}
int GPIO_open(int pinNumber)
{
    char buffer[50];
    snprintf(buffer, sizeof(buffer), "/sys/class/gpio/gpio%d/value", pinNumber);
    int fd = open(buffer, O_RDONLY);
    if (fd == -1)
    {
        perror("Error opening GPIO value file");
        exit(EXIT_FAILURE);
    }
    return fd;
}

void GPIO_close(int gpio_fd)
{
    close(gpio_fd);
}
int main()
{
    if (GPIO_init(GPIO_BUTTON_IN, "in") != 1 || GPIO_init(GPIO_BUTTON_OUT, "in") != 1 || GPIO_init(GPIO_BUTTON_NEW, "in") != 1)
    {
        perror("GPIO initialization failed!\n");
        return 1;
    }
    printf("GPIO initialization successful!\n");

    while (1)
    {
        int button_fd_in = GPIO_open(GPIO_BUTTON_IN);
        int button_fd_out = GPIO_open(GPIO_BUTTON_OUT);
        int button_fd_new = GPIO_open(GPIO_BUTTON_NEW);
        if (button_fd_in == -1 || button_fd_out == -1 || button_fd_new == -1)
        {
            perror("Error opening GPIO value file");
            break;
        }
        int button_state_in=GPIO_read(button_fd_in);
        int button_state_out=GPIO_read(button_fd_out);
        int button_state_new=GPIO_read(button_fd_new);

        if (!button_state_in)
        {
            printf("Button IN press\n");
        }
        if (!button_state_out) 
        {
            printf("Button OUT press\n");
        }
        if (!button_state_new) 
        {
            printf("Button NEW press\n");
        }
        GPIO_close(button_fd_in);
        GPIO_close(button_fd_out);
        GPIO_close(button_fd_new);
        sleep(1);
    }
}

DSView逻辑分析仪截图

内容的提问来源于stack exchange,提问作者Yael

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 07:28:17