此篇大概包含以下几个部份
- GPIO控制
- I2C控制
- 接上oled屏
GPIO控制
参考此页面 Raspberry Pi GPIO Tutorial: The Basics Explained
这是找到的网页中,具备最详述的GPIO说明
也包含如何打开GPIO、I2C、SPI的功能,不过在参考代码部份就只有GPIO的部份
参考线路与代码来自同一个 网页:
#import the GPIO and time package
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)
# loop through 50 times, on/off for 1 second
for i in range(50):
GPIO.output(7,True)
time.sleep(1)
GPIO.output(7,False)
time.sleep(1)
GPIO.cleanup()
I2C控制
打开I2C的方法除了上面几个网页,还可以交叉比对这几个网页的内容
IT Robotics Lab: Python 使用 I2C bus on Raspberry Pi (blogspot)
Raspberry Pi 筆記(二):GPIO接腳與I2C及SPI安裝 - 天花板隨記 (blogspot)
虽然大同小异,不过每个人遇到的问题也多少有点不一样
几个比较重要的地方
1.安装I2C工具 (detect, set, get, dump)
sudo apt-get install -y i2c-tools
2.写python前先用i2c detect扫一下
sudo i2cdetect -y 1
确认
- 硬件有接好
- slave出现的位置 (与预期是否一致)
3.安装smbus
sudo apt-get install python-smbus
4.API接口说明
读写
write_byte_data(addr, reg, val)
read_byte_data(addr, reg)
多个byte读写*
write_i2c_block_data(addr, reg, vals)
read_i2c_block_data(addr, reg)
其中vals的格式为
vals = [0x01, 0x03, 0xde, 0xbc]
*.是否支持多byte读写需要查阅元件的datasheet
写个小程序练习一下 (来自某个网页,后续补上)
import smbus
i2c = smbus.SMBus(1)
addr = 0x3c
reg = 0x03
val = 0x64
# write in
i2c.write_byte_data(addr, reg, val)
# print out readback
print i2c.read_byte_data(addr, reg)
接上OLED屏
硬件接法
Name | Remark | Pin# | RPi Detail |
---|---|---|---|
VCC | Power, 3.3V | 1 | 3V3 |
GND | Ground | 6 | Ground |
SCL | I2C Clock | 5 | GPIO3_SCL |
SDA | I2C Data | 3 | GPIO2_SDA |
注意,
刚接上OLED屏是不会亮的,OLED屏需要经过适当的初始化才有输出 ......... 请不要在这边就放弃了
软件部份
请参考此网页 (OLED Display 128x64 python library)
文中提到两个git来源
https://github.com/BLavery/lib_oled96
https://github.com/rm-hull/ssd1306
我自己是用hull的那个
流程如下
1.把参考代码从git拉到本机
git clone https://github.com/rm-hull/ssd1306.git
2.在 examples/ 底下找到 demo.py
from: https://github.com/rm-hull/ssd1306/blob/master/examples/demo.py
#!/usr/bin/env python
# Ported from:
# https://github.com/adafruit/Adafruit_Python_SSD1306/blob/master/examples/shapes.py
from oled.device import ssd1306, sh1106
from oled.render import canvas
from PIL import ImageFont
font = ImageFont.load_default()
device = ssd1306(port=1, address=0x3C)
with canvas(device) as draw:
# Draw some shapes.
# First define some constants to allow easy resizing of shapes.
padding = 2
shape_width = 20
top = padding
bottom = device.height - padding - 1
# Draw a rectangle of the same size of screen
draw.rectangle((0, 0, device.width-1, device.height-1), outline=255, fill=0)
# Move left to right keeping track of the current x position for drawing shapes.
x = padding
# Draw an ellipse.
draw.ellipse((x, top, x+shape_width, bottom), outline=255, fill=0)
x += shape_width + padding
# Draw a rectangle.
draw.rectangle((x, top, x+shape_width, bottom), outline=255, fill=0)
x += shape_width + padding
# Draw a triangle.
draw.polygon([(x, bottom), (x+shape_width/2, top), (x+shape_width, bottom)], outline=255, fill=0)
x += shape_width+padding
# Draw an X.
draw.line((x, bottom, x+shape_width, top), fill=255)
draw.line((x, top, x+shape_width, bottom), fill=255)
x += shape_width+padding
# Load default font.
font = ImageFont.load_default()
# Alternatively load a TTF font.
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
# font = ImageFont.truetype('Minecraftia.ttf', 8)
# Write two lines of text.
draw.text((x, top), 'Hello', font=font, fill=255)
draw.text((x, top+20), 'World!', font=font, fill=255)
会得到以下的输出