使用Linux命令控制树莓派GPIO接口

  • 示例
进入目录/sys/class/gpio中,下面是执行命令的过程: [codesyntax lang="bash"]
root@raspberrypi:/sys/class/gpio# echo 20 > export
root@raspberrypi:/sys/class/gpio# l
total 0
drwxrwx---  2 root gpio    0 Mar 18 17:00 .
drwxr-xr-x 46 root root    0 Mar 18 16:53 ..
-rwxrwx---  1 root gpio 4.0K Mar 18 17:00 export
lrwxrwxrwx  1 root gpio    0 Mar 18 17:00 gpio20 -> ../../devices/soc/3f200000.gpio/gpio/gpio20
lrwxrwxrwx  1 root gpio    0 Jan  1  1970 gpiochip0 -> ../../devices/soc/3f200000.gpio/gpio/gpiochip0
-rwxrwx---  1 root gpio 4.0K Jan  1  1970 unexport
root@raspberrypi:/sys/class/gpio# l gpio20
lrwxrwxrwx 1 root gpio 0 Mar 18 17:00 gpio20 -> ../../devices/soc/3f200000.gpio/gpio/gpio20
root@raspberrypi:/sys/class/gpio# cd gpio20
root@raspberrypi:/sys/class/gpio/gpio20# l
total 0
drwxrwx--- 3 root gpio    0 Mar 18 17:00 .
drwxrwx--- 4 root gpio    0 Mar 18 17:00 ..
-rwxrwx--- 1 root gpio 4.0K Mar 18 17:00 active_low
lrwxrwxrwx 1 root gpio    0 Mar 18 17:00 device -> ../../../3f200000.gpio
-rwxrwx--- 1 root gpio 4.0K Mar 18 17:00 direction
-rwxrwx--- 1 root gpio 4.0K Mar 18 17:00 edge
drwxrwx--- 2 root gpio    0 Mar 18 17:00 power
lrwxrwxrwx 1 root gpio    0 Mar 18 17:00 subsystem -> ../../../../../class/gpio
-rwxrwx--- 1 root gpio 4.0K Mar 18 17:00 uevent
-rwxrwx--- 1 root gpio 4.0K Mar 18 17:00 value
root@raspberrypi:/sys/class/gpio/gpio20# cd ..
root@raspberrypi:/sys/class/gpio# echo out > gpio20/direction 
root@raspberrypi:/sys/class/gpio# echo 1 > gpio20/value 
root@raspberrypi:/sys/class/gpio# echo 0 > gpio20/value 
root@raspberrypi:/sys/class/gpio# echo 20 > unexport 
root@raspberrypi:/sys/class/gpio# l
total 0
drwxrwx---  2 root gpio    0 Mar 18 17:03 .
drwxr-xr-x 46 root root    0 Mar 18 16:53 ..
-rwxrwx---  1 root gpio 4.0K Mar 18 17:00 export
lrwxrwxrwx  1 root gpio    0 Jan  1  1970 gpiochip0 -> ../../devices/soc/3f200000.gpio/gpio/gpiochip0
-rwxrwx---  1 root gpio 4.0K Mar 18 17:03 unexport
root@raspberrypi:/sys/class/gpio#
[/codesyntax] gpio20文件夹下有direction和value两个接口。direction接收in(输入)、out(输出)俩个值,默认为输出;value接口代表GPIO电平,非零值为高电平,零为低电平。
  • 参考
想知道你的树莓派到底有那些GPIO接口吗,请看这里。 https://github.com/concefly/openRPi_GPIO/wiki/%E7%AE%80%E6%98%8E-Linux-GPIO-%E9%A9%B1%E5%8A%A8%E6%8C%87%E5%AF%BC

转载于:https://my.oschina.net/surenpi/blog/481838

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值