一、硬件连接
树莓派自带的40个排针接口里面就有一组SPI接口:GPIO9(MISO) ,GPIO10(MOSI), GPIO11(SCL)。
然后看一下我使用的OLED的接口(注意,OLED的驱动芯片必须是SSD1306):
分别是GND VCC D0 D1 RST DC CS
各个口的功能与树莓派的IO口连线分别如下:
GND接树莓派的GND, VCC接树莓派的3v3电源口,不要接到5V,否则会烧坏OLED
CS是SPI的片选口,可以多组SPI同时使用,这里接树莓派的GPIO8(CE0)口,第24个管脚
DC口是数据与命令选择口,这里接到GPIO27,第13管脚
RST是复位口,这里接到GPIO17也就是11管脚
D1(MOSI)口,接到树莓派的GPIO10(MOSI)口,也就是19管脚
D0(SCLK)口,接到树莓派的GPIO11(SCLK)口,也就是23管脚
二、打开树莓派的SPI口
树莓派默认的SPI和I2C口都是被禁用的,使用之前必须先打开
1.sudo raspi-config
在高级选项中进入SPI配置项,使能SPI口,否则接下来操作将无效
2.sudo vim /etc/modprobe.d/raspi-blacklist.conf
使用井号注释掉这行:blacklist spi-bcm2708,即:#blacklist spi-bcm2708,退出保存(:wq)
3.sudo reboot,重启树莓派,这样就会打开树莓派的spi口,具体你可以在/dev目录下看到两个文件:spidev0.0spidev0.1,对应于GPIO口上的SPI口,0和1表示片选管脚CE0和CE1
三、使用python开始驱动SPI口的OLED
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential python-dev python-pip
sudo apt-get install python-imaging python-smbus
sudo apt-get install git
#clone 下国外友人提供的python库
git clone https://github.com/adafruit/Adafruit_Python_SSD1306.git
cd Adafruit_Python_SSD1306
sudo python setup.py install
下面就可以使用python来驱动这个OLED了:
新建个python文件:spioled-image.py
import time
import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
import Image
# Raspberry Pi pin configuration:
RST = 17
# Note the following are only used with SPI:
DC = 27
SPI_PORT = 0
SPI_DEVICE = 0
# 128x32 display with hardware I2C:
# disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST)
# 128x64 display with hardware I2C:
# disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
# 128x32 display with hardware SPI:
# disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST, dc=DC, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=8000000))
# 128x64 display with hardware SPI:
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST, dc=DC, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=8000000))
# Initialize library.
disp.begin()
# Clear display.
disp.clear()
disp.display()
# Load image based on OLED display height. Note that image is converted to 1 bit color.
if disp.height == 64:
image = Image.open('happycat_oled_64.ppm').convert('1')
else:
image = Image.open('happycat_oled_32.ppm').convert('1')
# Alternatively load a different format image, resize it, and convert to 1 bit color.
#image = Image.open('happycat.png').resize((disp.width, disp.height), Image.ANTIALIAS).convert('1')
# Display image.
disp.image(image)
disp.display()
将会输出一幅图片
原文参考自:http://www.embbnux.com/2014/08/10/raspberry_use_spi_oled_screen_python_c/