您的位置:首页 > 其它

iMX283 学习笔记----1. GPIO2_4(P2.4) 操作

2017-03-05 22:20 330 查看
在数据手册上一直找不到可以控制 P2.4 的寄存器的内存地址,只能在linux开发只能上看到如下的代码
// 导出GPIO
root@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 灯 打开和熄灭了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: