一.C程序通过sysfs控制GPIO
通过 GPIO 输出应用程序控制 GPIO 口输出高低电平,以此来控制LED灯的亮灭。
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int fd; // 文件描述符
int ret; // 返回值
char gpio_path[100]; // GPIO路径
int len; // 字符串长度
// 导出GPIO引脚
int gpio_export(char *argv)
{
fd = open("/sys/class/gpio/export", O_WRONLY); // 打开export文件
if (fd < 0)
{
printf("open /sys/class/gpio/export error \n"); // 打开文件失败
return -1;
}
len = strlen(argv); // 获取参数字符串的长度
ret = write(fd, argv, len); // 将参数字符串写入文件,导出GPIO引脚
if (ret < 0)