GuangXingLiu

一蓑一笠一孤舟,一丈丝纶一鱼钩,一曲高歌一樽酒,一人独钓一江秋.

Raspberry Pi B使用SPI接口OLED(SSD1306)显示屏:基于python和c

一 首先介绍使用python来驱动这个SPI接口的OLED
(1) OLED与树莓派连线
树莓派自带的26个排针接口里面就有一组SPI接口:GPIO9(MISO) ,GPIO10(MOSI), GPIO11(SCL)。


然后看一下我使用的OLED的接口(注意,OLED的驱动芯片必须是SSD1306):



分别是GND VCC D0 D1 RST DC CS
各个口的功能与树莓派的IO口连线分别如下:
GND接树莓派的GND, VCC接树莓派的3v3 POWER口,就是电源口,不要接到5V
CS是SPI的片选口,可以多组SPI同时使用,这里接树莓派的GPIO8(CE0)口,第24个管脚
DC口是数据与命令选择口,这里接到第13管脚,对于R1版本的树莓派就是GPIO21,我的是R2版本的(内存512MB),对应的是GPIO27(其实是同一个口)。
RST是复位口,这里接到GPIO17也就是11管脚
D1(MOSI)口,接到树莓派的GPIO10(MOSI)口,也就是21管脚
D0(SCLK)口,接到树莓派的GPIO11(SCLK)口,也就是23管脚




(2) 打开树莓派的spi口
树莓派默认的SPI和I2C口都是被禁用的,使用之前必须先打开
首先是ssh登陆到树莓派上:


1.vi /etc/modprobe.d/raspi-blacklist.conf
2.#blacklist spi-bcm2708 #使用井号注释掉这行
3.blacklist i2c-bcm2708 #如果要使用i2c就注释掉这行

保存后重启树莓派,sudo reboot,这样就会打开树莓派的spi口,具体你可以在/dev目录下看到两个文件:spidev0.0 spidev0.1,对应于GPIO口上的SPI口,0和1表示片选管脚CE0和CE1


(3) 使用python开始驱动SPI口的OLED
还是用ssh登陆到树莓派上,安装一些并要的软件,如果因为网络安装不成功,请重复该命令:

sudo apt-get update
sudo apt-get install build-essential python-dev python-pip
sudo pip install RPi.GPIO
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.py
import time
import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
import Image
import ImageDraw
import ImageFont

# Raspberry Pi pin configuration:
RST = 17
# Note the following are only used with SPI:
DC = 27
SPI_PORT = 0
SPI_DEVICE = 0

# 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()

# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)

# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)

# Draw some shapes.
# First define some constants to allow easy resizing of shapes.
padding = 2
shape_width = 20
top = padding
bottom = height-padding
# 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)

# Display image.
disp.image(image)
disp.display()



运行该程序,sudo python spioled.py

就会发现显示屏开始有显示了。

然后呢,我又改写了一个程序,只输出字符:

import time
import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
import Image
import ImageDraw
import ImageFont

# Raspberry Pi pin configuration:
RST = 17
# Note the following are only used with SPI:
DC = 27
SPI_PORT = 0
SPI_DEVICE = 0

# 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()

# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)

# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)

# Draw some shapes.
# First define some constants to allow easy resizing of shapes.
padding = 1
top = padding
x = 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), 'This is first line', font=font, fill=255)
draw.text((x, top+10), 'This is second line', font=font, fill=255)
draw.text((x, top+20), 'This is third line', font=font, fill=255)
draw.text((x, top+30), 'This is fourth line', font=font, fill=255)
draw.text((x, top+40), 'This is fifth line', font=font, fill=255)
draw.text((x, top+50), 'This is last line', font=font, fill=255)

# Display image.
disp.image(image)
disp.display()






二 使用c语言来驱动这个spi接口的oled
树莓派是支持使用c语言来操作底层的GPIO的,SPI也可以,这里介绍使用C来使这个OLED屏显示:
与树莓派的连线和上面的一样,这里就不介绍了.一样要打开spi口才可以使用
直接ssh登陆到树莓派 :

sudo apt-get update
sudo apt-get install vim
sudo apt-get install build-essential
sudo apt-get install cmake
sudo apt-get install git
git clone https://github.com/michaelKle/libssd1306.git
cd libssd1306
vim src/testSSD1306
int dcPin = 27; #如果是r1版本的,这里改为21,保存退出,:wq



编译该c语言包 :

1. mkdir build
2. cd build
3. cmake ..
4. make


测试:

1.cd build/src
2.sudo ./testSSD1306


没遇到什么问题的话,这里应该可以显示了,会有一段动画。


文章参考连接(感谢作者):http://www.embbnux.com/2014/08/10/raspberry_use_spi_oled_screen_python_c/

发表评论:

Powered By Z-BlogPHP 1.7.0