OLED与树莓派的连接
想要用树莓派成功的点亮OLED,就需要找到正确的接口进行连接。在树莓派的排针当中存在一组SPI接口,它们分别是:GPIO9(MISO) ,GPIO10(MOSI), GPIO11(SCL)。
需要特别说明的是,本文中用到的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管脚。
打开树莓派的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。
使用python开始驱动SPI口的OLED
还是用ssh登陆到树莓派上,安装一些并要的软件,如果因为网络安装不成功,请重复该命令:
sudo apt-get updatesudo apt-get install build-essential python-dev python-pipsudo pip install RPi.GPIOsudo apt-get install python-imaging python-smbussudo apt-get install git#clone git clone https://github.com/adafruit/Adafruit_Python_SSD1306.gitcd Adafruit_Python_SSD1306sudo python setup.py install
使用python来驱动OLED新建个python文件:spioled.pyimport timeimport Adafruit_GPIO.SPI as SPIimport Adafruit_SSD1306import Imageimport ImageDrawimport ImageFont# Raspberry Pi pin configuration:RST = 17# Note the following are only used with SPI:DC = 27SPI_PORT = 0SPI_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.widthheight = disp.heightimage = 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 = 2shape_width = 20top = paddingbottom = 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 timeimport Adafruit_GPIO.SPI as SPIimport Adafruit_SSD1306import Imageimport ImageDrawimport ImageFont# Raspberry Pi pin configuration:RST = 17# Note the following are only used with SPI:DC = 27SPI_PORT = 0SPI_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.widthheight = disp.heightimage = 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 = 1top = paddingx = 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,保存退出。
编译该c语言包 :
1. mkdir build
2. cd build
3. cmake ..
4. make
测试:
1.cd build/src
2.sudo ./testSSD1306
如果在过程当中没有遇到什么问题的话,在这里的程序完成之后就可以显示了,并且会有一段动画。本篇文章为大家介绍了如何使用树莓派使OLED运作的全过程,从接口设置到程序编写都进行了详尽的介绍。希望大家也能进行实际动手操作,提高自己的技能。