【树莓派教程】——SPI驱动OLED 128*64

2016-01-102.7万
AI 快速预览详细 收起
本文是一篇关于如何在树莓派上通过SPI接口驱动OLED 128*64显示屏的教程。首先介绍了树莓派针脚定义和OLED各针脚的连接方式,然后详细说明了如何启用树莓派的SPI接口。接着提供了安装必要库文件的步骤,最后给出了一个Python示例程序,展示了如何在OLED屏幕上显示多行文本。通过这些步骤,用户可以轻松实现树莓派上的OLED显示功能。
【树莓派教程】——SPI驱动OLED  128*64图4

树莓派针脚定义
【树莓派教程】——SPI驱动OLED  128*64图1

OLED各针脚定义以及联线
GND——rasp gnd
VCC ——rasp vcc(3.3/5V)
D0(SCLK)  ——rasp SCLK (BCM:11/BOARD:23)
D1(MOSI )——rasp MOSI(BCM:10/BOARD:19)
RST——rasp GPIO(BCM:17/BOARD:11)
DC(数据与命令选择)——rasp (BCM:27/BOARD:13)
CS(SPI片选)——rasp (BCM:8/BOARD:24)

重要的部分要开始啦~~~~ATTENTION!!!!!!!!!!
1.打开树莓派的SPI。
树莓派的SPI默认是关闭的(这一点和I2C类似),打开方法有多种,在这仅介绍一种
vi /etc/modprobe.d/raspi-blacklist.conf
修改为如图
【树莓派教程】——SPI驱动OLED  128*64图2

注释掉两行表示启用SPI/I2C
  1. # blacklist spi and i2c by default (many users don't need them)
  2.   
  3.    #blacklist spi-bcm2708
  4.    #blacklist i2c-bcm2708
复制代码
reboot 以下
  1. cd /dev
  2. ls -al
复制代码

你会看到spidev0.0以及spidev0.1
【树莓派教程】——SPI驱动OLED  128*64图3


2.库
  1. sudo apt-get update
  2. sudo apt-get install build-essential python-dev python-pip
  3. sudo pip install RPi.GPIO
  4. sudo apt-get install python-imaging python-smbus
  5. sudo apt-get install git
复制代码
安装完成之后我们就可以用git 啦~~在此要感谢@孙毅带我入git的坑~~O(∩_∩)O哈哈~
  1. git clone https://github.com/adafruit/Adafruit_Python_SSD1306.git
  2. cd Adafruit_Python_SSD1306
  3. sudo python setup.py install
复制代码
到这就差不多了,来一个python的程序
  1. #!/usr/bin/python/
  2. # coding: utf-8
  3. import time
  4. import Adafruit_GPIO.SPI as SPI
  5. import Adafruit_SSD1306
  6. import Image
  7. import ImageDraw
  8. import ImageFont
  9. # Raspberry Pi pin configuration:
  10. RST = 17
  11. # Note the following are only used with SPI:
  12. DC = 27
  13. SPI_PORT = 0
  14. SPI_DEVICE = 0
  15. # 128x64 display with hardware SPI:
  16. disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST, dc=DC, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=8000000))
  17. # Initialize library.
  18. disp.begin()
  19. # Clear display.
  20. disp.clear()
  21. disp.display()
  22. # Create blank image for drawing.
  23. # Make sure to create image with mode '1' for 1-bit color.
  24. width = disp.width
  25. height = disp.height
  26. image = Image.new('1', (width, height))
  27. # Get drawing object to draw on image.
  28. draw = ImageDraw.Draw(image)
  29. # Draw a black filled box to clear the image.
  30. draw.rectangle((0,0,width,height), outline=0, fill=0)
  31. # Draw some shapes.
  32. # First define some constants to allow easy resizing of shapes.
  33. padding = 1
  34. top = padding
  35. x = padding
  36. # Load default font.
  37. font = ImageFont.load_default()
  38. # Alternatively load a TTF font.
  39. # Some other nice fonts to try: http://www.dafont.com/bitmap.php
  40. #font = ImageFont.truetype('Minecraftia.ttf', 8)
  41. # Write two lines of text.
  42. draw.text((x, top), 'This is first line', font=font, fill=255)
  43. draw.text((x, top+10), 'This is second line', font=font, fill=255)
  44. draw.text((x, top+20), 'This is third line', font=font, fill=255)
  45. draw.text((x, top+30), 'This is fourth line', font=font, fill=255)
  46. draw.text((x, top+40), 'This is fifth line', font=font, fill=255)
  47. draw.text((x, top+50), 'This is last line', font=font, fill=255)
  48. # Display image.
  49. disp.image(image)
  50. disp.display()
复制代码

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(6)
dsweiliang
凌风清羽
应该是不可以的
驱动芯片估计不一样
qq815076543322
大神 是不是要和你求师了
qq815076543322
评论内容
凌风清羽
评论内容
丄帝De咗臂
树莓派被你玩透了
凌风清羽
还差的远呢~~~~~
hnyzcj
刚买了一块OLED,正好用用。
hnyzcj
用ARDUINO软体
孙毅
你oled接哪里用啊,陈老师?
用什么“软体”?
孙毅
要不要这么任性。。。哈哈哈,牛!
凌风清羽
凌风清羽
作者
舒爽
qq815076543322
加油 加油
凌风清羽
嘿嘿,刚刚起步~~
共4条回复,已加载2条,点击查看更多共4条回复,已加载全部
- 没有更多了 -