[5]投影片 futurewad樹莓派研習會 141218
TRANSCRIPT
Session 6: GPIO
http://elinux.org/RPi_Low-
level_peripherals
2
Introduction
• R-Pi offers lower-level interfaces intended to
connect more directly with chips and subsystem
modules
• Signals on the 2x13 header pins include SPI,
I2C, serial UART, 3V3 and 5V power
• Not "plug and play" and require care to avoid
miswiring
• The pins use a 3.3V logic level and are not
tolerant of 5V levels, such as you might find on a
5V powered Arduino
3
• Not yet software-enabled are the
flex cable connectors with CSI
(camera serial interface) and DSI
(display serial interface), and a
serial link inside the HDMI
connector called CEC
• They provide 8 GPIO pins plus
access to I²C, SPI, UART), as well
as +3.3 V, +5 V and GND supply
lines5
RPi Serial Connection
• The following parameters are needed to connect to the Pi console, and
apply on both Linux and Windows.
– Speed (baud rate): 115200
– Bits: 8
– Parity: None
– Stop Bits: 1
– Flow Control: None
6
http://elinux.org/RPi_Serial_Connection
Connection to a microcontroller
or other peripheral• The TxD and RxD signals can also be connected directly
to similar signals on a microcontroller board like the
Arduino provided the signals are all at 3V3 levels.
• It's still a good idea to put 2K2 series resistors in the
lines to prevent damage when two outputs are
connected together, which could also happen if a GPIO
input pin is accidentally programmed as output.
• If your microcontroller uses 5V logic levels, level
conversion is usually necessary - see 'Connecting to a
PC' for details.
7
控制GPIO腳位
http://makezine.com/projects/tutori
al-raspberry-pi-gpio-pins-and-
python/
8
安裝GPIO for python函式庫
• 安裝python函式庫
• $sudo apt-get install RPi.GPIO
• Python的.py檔匯入函式庫
• import RPi.GPIO as GPIO
9
GPIO example:LED blink
• 設定初始狀態,GPIO針腳
• import RPi.GPIO as GPIO #匯入函式庫
• import time #匯入函式庫
• GPIO.setwarnings(False) #關閉偵錯模式
• GPIO.setmode(GPIO.BCM) #設定腳位代號
• GPIO.setup(17,GPIO.OUT) #17號為輸出腳
12
GPIO example:LED blink
• LED燈閃爍設定
• while True: #無限迴圈
• GPIO.output(17,True) #17腳輸出電壓(3.3V)
• time.sleep(2) #停頓2秒
• GPIO.output(17,False) #17腳不輸出電壓(0V)
• time.sleep(2)
13
GPIO example:Button
• 設定初始狀態,GPIO針腳
• import RPi.GPIO as GPIO #匯入函式庫
• import time #匯入函式庫
• GPIO.setwarnings(False) #關閉偵錯模式
• GPIO.setmode(GPIO.BCM) #設定腳位代號
• GPIO.setup(18,GPIO.IN) #18號為輸入腳
16
GPIO example:Button
• 偵測Button是否被按下
• while True:
• input_value=GPIO.input(18) #讀取18號輸入腳,並存入變數中
• if input_value ==False: #如果讀取為0V
• print( "The button has been pressed" )
• while input_value == False: #讀取為0V時,進入迴圈
• input_value = GPIO.input(18)
17
GPIO example:Led_Button
• 設定初始狀態,GPIO針腳
• import RPi.GPIO as GPIO #匯入函式庫
• import time #匯入函式庫
• GPIO.setwarnings(False) #關閉偵錯模式
• GPIO.setmode(GPIO.BCM) #設定腳位代號
• GPIO.setup(18,GPIO.IN) #18號為輸入腳
• GPIO.setup(17,GPIO.OUT) #17號為輸出腳
• GPIO.output(17,False) #17號為0V(燈不亮)
20
GPIO example:Led_Button
• 設定初始狀態,GPIO針腳
• while True:
• input_value=GPIO.input(18) #儲存18號輸入腳(按鈕)於變數
• if input_value ==False: #如果按鈕被按下
• print( "The button has been pressed,Lighting LED." )
• GPIO.output(17,True) #17號輸出腳給電(LED燈亮)
• while input_value == False: #按鈕被按下時進入迴圈
• input_value = GPIO.input(18) #讀取按鈕的狀態,存於變數中
• print( "The button has been pressed,Extiguishing LED." )
• if input_value == True : #如果按鈕沒被按下
• GPIO.output(17,False) # 17號輸出腳為零(LED燈暗)
21
(1)pwm.py
• GPIO.setup(18,GPIO.OUT)
• #設定通道
• p = GPIO.PWM(18,100)
• #設定頻率
• p.start(b)
• #設定Dutycycle
25
Raspberry Pi - Installing a
RS232 Serial Port
http://www.savagehomeautomatio
n.com/projects/raspberry-pi-
installing-a-rs232-serial-port.html
35
Raspberry Pi - RS232 Serial
Interface Options (Revisit)
SOFTWARE USE OF THE
SERIAL PORT
36
make of backup of the two files
that we intend to modify• sudo cp /boot/cmdline.txt
/boot/cmdline.bak
• sudo cp /etc/inittab /etc/inittab.bak
37
Next
• we need to remove the "console=ttyAMA0,115200" and
"kgdboc=ttyAMA0,115200" configuration parameters from the
"/boot/cmdline.txt" configuration file.
• To edit the file use this command:
$ sudo nano /boot/cmdline.txt• The file probably contains this default configuration line:
– dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200
console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
• After removing the two configuration parameters, it will look similar
to this:
– dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4
elevator=deadline rootwait
38
Last
• edit the "/etc/inittab" file and comment out
the use of the "ttyAMA0" serial port.
• To edit the file use this command:
$ sudo nano /etc/inittab
39
Now towards the bottom of the file, look for a
configuration line that includes the "ttyAMA0" port
address
40
Place a pound sign ("#") in front of the line to comment it
out. With a pound sign ("#") at the beginning of the line,
Linux will ignore this configuration line.
• Save the "/etc/inittab" file and then issue
this command to reboot the Raspberry Pi:
$ sudo reboot
41
• Now you are ready to use the serial port
with a software application and the
operating system won't interfere with the
port.
• Use the device address "ttyAMA0" in your
application to access this serial port.
42
Project:Create an internet
controlled robot using
Livebotshttp://www.instructables.com/id/Cr
eate-an-internet-controlled-robot-
using-Livebots/step1/Have-Fun/
43
Advanced GPIO for the
Raspberry Pi
https://pypi.python.org/pypi/RPIO
Extends RPi.GPIO with PWM,
GPIO interrups, TCP socket
interrupts, command line tools and
more
44
RPIO is an advanced GPIO module
for the Raspberry Pi• PWM via DMA (up to 1µs resolution)
• GPIO input and output (drop-in replacement for RPi.GPIO)
• GPIO interrupts (callbacks when events occur on input gpios)
• TCP socket interrupts (callbacks when tcp socket clients send data)
• Command-line tools rpio and rpio-curses
• Well documented, fast source code with minimal CPU usage
• Open source (LGPLv3+)
45
How to use GPIOs on
raspberry pi (Simple I/O, PWM
and UART)
https://sites.google.com/site/semill
eroadt/raspberry-pi-tutorials/gpio
53