RPi-002 从GPIO控制到替Pi加上0.96" OLED

此篇大概包含以下几个部份

  • 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的方法除了上面几个网页,还可以交叉比对这几个网页的内容

Adafruit I2C Library Problem?

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接口说明

Using the I2C Interface

读写

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屏


硬件接法


NameRemarkPin#RPi Detail
VCCPower, 3.3V13V3
GNDGround6Ground
SCLI2C Clock5GPIO3_SCL
SDAI2C Data3GPIO2_SDA


注意, 

刚接上OLED屏是不会亮的,OLED屏需要经过适当的初始化才有输出 ......... 请不要在这边就放弃了


SSD1306 datasheet


软件部份

请参考此网页 (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)

会得到以下的输出




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值