1回答

0收藏

[原创] 【Tigerboard】之GPIO的输入输出控制

#拆解/开源硬件 #拆解/开源硬件 3175 人阅读 | 1 人回复 | 2016-03-19

  要利用TigerBoard实现外围设备的控制,首先的学会如果去控制TigerBoard的GPIO口,由于板子Gobian系统中已经封装了RPi.GPIO库,并且兼容Raspberry Pi的管脚,所以我们可以参考树莓派GPIO的控制方法,首先看下J3这两排管脚。




一.GPIO的输出控制

  了解了GPIO的原理图后,我们可以参考官方提供的技术文档和Demo选33PIN这一管脚进行测试,这里我们采用Python来实现这一功能,通过观察LED的状态来验证程序
  1. import RPi.GPIO as gpio  # Include the RPi.GPIO library as gpio.
  2. import time                    # Include the Python time functions.
  3. gpio.setmode(gpio.BOARD) # Set mod as BOARD, which means to use the numbering from the physical layout.
  4. while True:
  5.     gpio.setup(33, gpio.OUT)          # Set pin 33 on J3 to be an output.
  6.     gpio.output(33, gpio.HIGH)        # Set pin 33 on J3 as high.
  7.     time.sleep(1)                     # Wait for one second.
  8.     gpio.output(33, gpio.LOW)             # Set pin 33 on J3 as low.
  9.     time.sleep(1)                     # Wait for one second.
复制代码
说明:
  程序开始导入了要用到的库和包,然后设置了GPIO的模式--gpio.BOARD,最后通过一个while循环来实现了灯的亮灭控制,并且亮灭之间延时时间为1S

学会了如果控制GPIO的控制,我们可以轻松的操作继电器开关、人体红外检测等简单的模块


二.GPIO的输入控制

  这里只需要改变GPIO的配置,并进行检测即可基本同GPIO输入
  1. import RPi.GPIO as gpio  
  2. import time
  3. gpio.setmode(gpio.BOARD)
  4. while True:
  5.     gpio.setup(33, gpio.IN)             # Set up pin 33 on header J3 to be used as an input.
  6.     if gpio.input(33):                       # Read the value of pin 33; and execute the indented if it’s high.
  7.       print “Button pressed!”           # Print a message to the terminal.
  8.     time.sleep(3)                             # Wait for 3second.
复制代码


  这里gpio.input(33)输入你可以再33管脚通过接地和接3.3V电压来模拟验证程序,会了这个便知道如果操作按键和其他的一些输入识别的GPIO控制了。

分享到:
回复

使用道具 举报

回答|共 1 个

倒序浏览

沙发

annysky

发表于 2016-4-10 16:09:16 | 只看该作者

使用RPi.GPIO会出现以下错误:
RuntimeError: This module can only be run on a Raspberry Pi!
怎么解决??????????


你遇到这种问题了吗?这个问题怎么解决?
您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条