GPIO(General Purpose Input Output,通用型输入输出),对于输入,一定可以通过读取某个寄存器来确定引脚电位的高低;对于输出,一定可以通过写入某个寄存器来让这个引脚输出高电位或者低电位;对于其他特殊功能,则有另外的寄存器来控制它们。
我们先来看下JetsonNano主板的40针的引脚实物图
对应的扩展板的引脚图
我们先来看下GPIO有哪些属性和方法
import RPi.GPIO as GPIO
#使用Jetson.GPIO也可以调用到GPIO驱动
#import Jetson.GPIO as GPIO
import time
dir(GPIO)
#['BCM', 'BOARD', 'BOTH', 'CVM', 'FALLING', 'HARD_PWM', 'HIGH', 'IN', 'JETSON_INFO', 'LOW', 'OUT', 'PUD_DOWN', 'PUD_OFF', 'PUD_UP', 'PWM', 'RISING', 'RPI_INFO', 'TEGRA_SOC', 'UNKNOWN', 'VERSION', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'add_event_callback', 'add_event_detect', 'cleanup', 'event', 'event_detected', 'getmode', 'gpio', 'gpio_event', 'gpio_function', 'gpio_pin_data', 'input', 'model', 'os', 'output', 'remove_event_detect', 'setmode', 'setup', 'setwarnings', 'time', 'wait_for_edge', 'warnings']
这里的GPIO有两种引脚模式:BCM和BOARD,对应着逻辑引脚和物理引脚,下面是对针脚的一些对应说明图片,大家可以了解下
GPIO的其他的一些相关信息,我们来熟悉一下:
#RPi.GPIO模块的版本信息
GPIO.VERSION
#0.1.0
#RPI信息
GPIO.RPI_INFO
#{'P1_REVISION': 1, 'RAM': '4096M', 'REVISION': 'Unknown', 'TYPE': 'Jetson Nano', 'MANUFACTURER': 'NVIDIA', 'PROCESSOR': 'ARM A57'}
GPIO.JETSON_INFO跟上面一样的结果
#{'P1_REVISION': 1, 'RAM': '4096M', 'REVISION': 'Unknown', 'TYPE': 'Jetson Nano', 'MANUFACTURER': 'NVIDIA', 'PROCESSOR': 'ARM A57'}
#设置引脚的输出状态(设置23引脚为高电平)
GPIO.output(LED3BLUE_pin, GPIO.HIGH)
#读取 GPIO 引脚的值(参数:通道是基于您指定的编号系统BOARD或BCM的通道号):
GPIO.input(LED3BLUE_pin)
然后我们来对引脚做一些操作示例,当然在做下面这些操作之前,需要先获取外设的权限,比如gpio和i2c的使用权限
sudo usermod -a -G gpio jetson #jetson为当前用户名
sudo usermod -a -G i2c jetson #jetson为当前用户名
从RPi.GPIO导入GPIO对象,初始化引脚对象,将GPIO口模式设置为BCM模式,设置为输出模式,初始电平为高电平,然后每隔0.25进入函数翻转GPIO口电平使LED闪烁
import RPi.GPIO as GPIO
import time
# 引脚定义
LED3BLUE_pin = 23 # BOARD pin 12, BCM pin 18
def main():
# Pin Setup:Board pin-numbering scheme
GPIO.setmode(GPIO.BCM)
# 将引脚设置为输出引脚,初始状态可选为高
GPIO.setup(LED3BLUE_pin, GPIO.OUT, initial=GPIO.HIGH)
print("Starting demo now!")
curr_value = GPIO.HIGH
try:
while True:
# 每隔0.25秒钟翻转一次输出
time.sleep(0.25)
print("Outputting {} to pin {}".format(curr_value, LED3BLUE_pin))
GPIO.output(LED3BLUE_pin, curr_value)
curr_value ^= GPIO.HIGH
finally:
GPIO.cleanup()
if __name__ == '__main__':
main()
'''
Starting demo now!
/usr/local/lib/python3.6/dist-packages/Jetson/GPIO/gpio.py:352: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings
RuntimeWarning)
Outputting 1 to pin 23
Outputting 0 to pin 23
Outputting 1 to pin 23
Outputting 0 to pin 23
Outputting 1 to pin 23
Outputting 0 to pin 23
Outputting 1 to pin 23
Outputting 0 to pin 23
Outputting 1 to pin 23
Outputting 0 to pin 23
...
'''
禁用警告已有提示说明,可以使用GPIO.setwarnings(False)来忽略掉
import RPi.GPIO as GPIO
import time
# 引脚定义
LED2GREEN_pin = 24 # BOARD pin 12, BCM pin 18
def main():
# Pin Setup:
# Board pin-numbering scheme
GPIO.setmode(GPIO.BCM)
# 将引脚设置为输出引脚,初始状态可选为高
GPIO.setup(LED2GREEN_pin, GPIO.OUT, initial=GPIO.HIGH)
print("Starting demo now!")
curr_value = GPIO.HIGH
try:
while True:
# 每隔0.25秒钟翻转一次输出
time.sleep(0.25)
print("Outputting {} to pin {}".format(curr_value, LED2GREEN_pin))
GPIO.output(LED2GREEN_pin, curr_value)
curr_value ^= GPIO.HIGH
finally:
GPIO.cleanup()
if __name__ == '__main__':
main()
'''
Starting demo now!
Outputting 1 to pin 24
Outputting 0 to pin 24
Outputting 1 to pin 24
Outputting 0 to pin 24
Outputting 1 to pin 24
Outputting 0 to pin 24
Outputting 1 to pin 24
Outputting 0 to pin 24
Outputting 1 to pin 24
Outputting 0 to pin 24
...
'''
将按键GPIO口模式设置为BCM模式,设置为输入模式,当按键状态发生变化后会使绿色LED跟随状态变化,并打印当前按键GPIO口状态
import RPi.GPIO as GPIO
import time
# Pin Definitons:
led_pin = 24 # BOARD pin 24 绿色
but_pin = 8 # BOARD pin 8 key1
def main():
prev_value = None
# Pin Setup:
GPIO.setmode(GPIO.BCM) # BCM
GPIO.setup(led_pin, GPIO.OUT) # LED pin set as output
GPIO.setup(but_pin, GPIO.IN) # Button pin set as input
# Initial state for LEDs:
GPIO.output(led_pin, GPIO.LOW)
print("Starting demo now!")
try:
while True:
curr_value = GPIO.input(but_pin)
if curr_value != prev_value:
GPIO.output(led_pin, not curr_value)
prev_value = curr_value
print("Outputting {} to Pin {}".format(curr_value, led_pin))
# time.sleep(0.5)
finally:
GPIO.cleanup() # cleanup all GPIO
if __name__ == '__main__':
main()
'''
Starting demo now!
Outputting 1 to Pin 24
Outputting 0 to Pin 24
Outputting 1 to Pin 24
Outputting 0 to Pin 24
Outputting 1 to Pin 24
Outputting 0 to Pin 24
Outputting 1 to Pin 24
Outputting 0 to Pin 24
Outputting 1 to Pin 24
...
'''
这里的KEY按键,按下和松开,观察绿色LED灯的变化。
当然在这里会用到系统总线,需要安装python-smbus
sudo apt-get install python-smbus
import smbus
import time
bus = smbus.SMBus(1)
Buzzer_ADD = 0x1B
print("Starting demo now!")
curr_value = 1
while True:
time.sleep(1)
# Toggle the output every second
print("Outputting {}".format(curr_value))
bus.write_byte_data(Buzzer_ADD,0x06,curr_value)
curr_value ^= 1
蜂鸣器GPIO口翻转6次
import smbus
import time
bus = smbus.SMBus(1)
Buzzer_ADD = 0x1B
bus.write_byte_data(Buzzer_ADD,0x06,1)
time.sleep(0.1)
bus.write_byte_data(Buzzer_ADD,0x06,0)
time.sleep(0.2)
bus.write_byte_data(Buzzer_ADD,0x06,1)
time.sleep(0.1)
bus.write_byte_data(Buzzer_ADD,0x06,0)
time.sleep(0.2)
bus.write_byte_data(Buzzer_ADD,0x06,1)
time.sleep(0.1)
bus.write_byte_data(Buzzer_ADD,0x06,0)
time.sleep(0.2)
其中的smbus我们来看下有哪些属性和方法,SMBus (System Management Bus,系统管理总线) 是1995年由Intel提出的,应用于移动PC和桌面PC系统中的低速率通讯。希望通过一条廉价并且功能强大的总线(由两条线组成),来控制主板上的设备并收集相应的信息。
print(dir(smbus.SMBus))
#['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'block_process_call', 'close', 'open', 'pec', 'process_call', 'read_block_data', 'read_byte', 'read_byte_data', 'read_i2c_block_data', 'read_word_data', 'write_block_data', 'write_byte', 'write_byte_data', 'write_i2c_block_data', 'write_quick', 'write_word_data']