完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
从Terminal直接控制IO口的方法如下:root@beaglebone:~# cd /sys/class/gpio
root@beaglebone:/sys/class/gpio# ls -l total 0 --w------- 1 root root 4096 Jan 1 00:00 export lrwxrwxrwx 1 root root 0 Jan 1 00:00 gpiochip0 -> ../../devices/virtual/gpio/gpiochip0 lrwxrwxrwx 1 root root 0 Jan 1 00:00 gpiochip32 -> ../../devices/virtual/gpio/gpiochip32 lrwxrwxrwx 1 root root 0 Jan 1 00:00 gpiochip64 -> ../../devices/virtual/gpio/gpiochip64 lrwxrwxrwx 1 root root 0 Jan 1 00:00 gpiochip96 -> ../../devices/virtual/gpio/gpiochip96 --w------- 1 root root 4096 Jan 1 00:00 unexport root@beaglebone:/sys/class/gpio# echo 44 > export root@beaglebone:/sys/class/gpio# ls -l total 0 --w------- 1 root root 4096 Jan 1 00:03 export lrwxrwxrwx 1 root root 0 Jan 1 00:03 gpio44 -> ../../devices/virtual/gpio/gpio44 lrwxrwxrwx 1 root root 0 Jan 1 00:00 gpiochip0 -> ../../devices/virtual/gpio/gpiochip0 lrwxrwxrwx 1 root root 0 Jan 1 00:00 gpiochip32 -> ../../devices/virtual/gpio/gpiochip32 lrwxrwxrwx 1 root root 0 Jan 1 00:00 gpiochip64 -> ../../devices/virtual/gpio/gpiochip64 lrwxrwxrwx 1 root root 0 Jan 1 00:00 gpiochip96 -> ../../devices/virtual/gpio/gpiochip96 --w------- 1 root root 4096 Jan 1 00:00 unexport root@beaglebone:/sys/class/gpio# cd gpio44 root@beaglebone:/sys/class/gpio/gpio44# ls -l total 0 -rw-r--r-- 1 root root 4096 Jan 1 00:03 active_low -rw-r--r-- 1 root root 4096 Jan 1 00:03 direction -rw-r--r-- 1 root root 4096 Jan 1 00:03 edge drwxr-xr-x 2 root root 0 Jan 1 00:03 power lrwxrwxrwx 1 root root 0 Jan 1 00:03 subsystem -> ../../../../class/gpio -rw-r--r-- 1 root root 4096 Jan 1 00:03 uevent -rw-r--r-- 1 root root 4096 Jan 1 00:03 value root@beaglebone:/sys/class/gpio/gpio44# cat direction in root@beaglebone:/sys/class/gpio/gpio44# echo out > direction root@beaglebone:/sys/class/gpio/gpio44# cat direction out root@beaglebone:/sys/class/gpio/gpio44# cat value 0 root@beaglebone:/sys/class/gpio/gpio44# echo 1 > value root@beaglebone:/sys/class/gpio/gpio44# cat value 1 解释一下,首先要把某个(本例中是第44个,即P8_12)gpio export一下,变成用户可用的状态,然后目录里就会多出来一个gpio44目录,进入它对相应文件进行读写就可以操作io口了。输入输出是一样的道理。读的话Linux会自动实时更新value文件里的数据,但更新速度有多快暂时还不清楚,高速io操作的话用这种方法感觉不靠谱。不过速度不敏感的话是没问题的。用c程序控制io口,我们当然可以完全照搬上面对文件操作的过程,只不过写成c语言的形式。具体如下: #include #include #include #include #define SYSFS_GPIO_DIR "/sys/class/gpio" #define MAX_BUF 64 void main() { int fd, len; char buf[MAX_BUF]; char ch; int i; //export gpio44 fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY); len = snprintf(buf,sizeof(buf),"44"); write(fd,buf,len); close(fd); //set direction snprintf(buf,sizeof(buf),SYSFS_GPIO_DIR"/gpio44/direction"); fd = open(buf, O_WRONLY); write(fd, "in", 3); close(fd); //read and print value 10 times for(i=0;i<10;i++) { snprintf(buf,sizeof(buf),SYSFS_GPIO_DIR"/gpio44/value"); fd = open(buf, O_RDONLY); read(fd,&ch,1); printf("%cn",ch); close(fd); usleep(1000000); } } |
|
|
|
只有小组成员才能发言,加入小组>>
164个成员聚集在这个小组
加入小组BBB通过sd卡启动ubuntu16.04后不能升级下载联网的问题
4391 浏览 2 评论
3159 浏览 0 评论
7467 浏览 5 评论
317浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-1 00:32 , Processed in 0.484598 second(s), Total 71, Slave 53 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号