Reputation: 153
I have code to initialize GPIO and read the button state value from a file descriptor. I only have 3 buttons.The code is very simple. first I initialize 3 gpio and configure them to output. then I open 3 file descriptors (1 for each button) and then read the value. and this happens in an endless loop. When you press one button, the voltage changes on all three. Everything works correctly for one button. You can see a screenshot of dsview logic analyzer. What could be the reason
#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);
}
}
Upvotes: 1
Views: 82
Reputation: 153
I added a resistor for each button and everything worked. If someone can explain to me why, I will be glad. thanks everyone
Upvotes: 1