// 导出GPIOroot@EasyARM-iMX283 /sys/class/gpio# echo 68 >export这样子很难在代码中使用,经过不端尝试,又问了无数遍的度娘,终于找到了导出GPIO的方法:
// 导出GPIO char buffer[10000000]; int len; int fd; fd = open("/sys/class/gpio/export", O_WRONLY); if (fd < 0) { PRintf("Failed!/n"); return(-1); } len = snprintf(buffer, 10000000, "%d", 68); if (write(fd, buffer, len) < 0) { printf("Fail to export gpio!"); return -1; } close(fd); 以上代码就导出了 P2.4.导出之后就是设置输入还是输出了
// 开发指南代码root@EasyARM-iMX283 /sys/devices/virtual/gpio/gpio68# echo out >direction #设置为输出当然也是可以通过IO 操作。char path[10000000]; int fd; snprintf(path, 10000000, "/sys/class/gpio/gpio%d/direction", 68); fd = open(path, O_WRONLY); if (fd < 0) { printf("failed !/n"); return -1; } if (write(fd, "out", 3) < 0) { // 设置为输出 printf( "failed to set direction!/n"); return -1; } close(fd); 最后设置GPIO的值,还是先写一下 开发指南代码root@EasyARM-iMX283 /sys/devices/virtual/gpio/gpio68# echo 0 >value #输出低电平root@EasyARM-iMX283 /sys/devices/virtual/gpio/gpio68# echo 1 >value #输出高电平以下是IO操作方法:char path[10000000]; int fd; snprintf(path, 10000000, "/sys/class/gpio/gpio%d/value", 68); fd = open(path, O_WRONLY); if (fd < 0) { printf("failed!/n"); return -1; } if (write(fd, 1, 1) < 0) { printf("failed to write value!/n"); return -1; } close(fd);如此,就能愉悦的 控制 连接在 GPIO 上的led 灯 打开和熄灭了
新闻热点
疑难解答