#include <stdio.h>
#include <stdlib.h>
int main() {
// 设置要打开的GPIO引脚编号
int gpioPin = 17;
// 构建GPIO设备文件路径
char gpioPath[128];
snprintf(gpioPath, sizeof(gpioPath), "/sys/class/gpio/gpio%d", gpioPin);
// 检查GPIO设备文件是否已经存在
if (access(gpioPath, F_OK) == -1) {
// 构建导出GPIO引脚的命令
char exportCmd[128];
snprintf(exportCmd, sizeof(exportCmd), "sudo su -c 'echo %d > /sys/class/gpio/export'", gpioPin);
// 执行导出GPIO引脚的命令
if (system(exportCmd) == -1) {
perror("system");
return 1;
}
}
// 构建设置GPIO引脚方向的命令
char directionCmd[128];
snprintf(directionCmd, sizeof(directionCmd), "sudo su -c 'echo out > /sys/class/gpio/gpio%d/direction'", gpioPin);
// 执行设置GPIO引脚方向的命令
if (system(directionCmd) == -1) {
perror("system");
return 1;
}
// 在此处执行其他GPIO操作
return 0;
}
linux下操作gpio端口
于 2023-09-20 09:57:34 首次发布