#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
#define I2C_SLAVE_ADDRESS 0x40
#define I2C_SDA_PIN 2
#define I2C_SCL_PIN 3
#define GPIO_BASE_PATH "/sys/class/gpio/gpio"
#define GPIO_DIRECTION_FILE "direction"
#define GPIO_VALUE_FILE "value"
static int sda_fd = -1;
static int scl_fd = -1;
static void gpio_write(int fd, int value)
{
char buffer[2];
snprintf(buffer, sizeof(buffer), "%d", value);
write(fd, buffer, 2);
}
static int gpio_read(int fd)
{
char buffer[2];
lseek(fd, 0, SEEK_SET);
read(fd, buffer, 2);
return atoi(buffer);
}
static void gpio_set_direction(int fd, char *direction)
{
write(fd, direction, strlen(direction));
}
static int gpio_export(int pin)
{
char buffer[10];
int fd = open("
GPIO模拟I2C的C语言代码
最新推荐文章于 2023-10-17 16:38:23 发布