8321|2

187

帖子

0

TA的资源

一粒金砂(高级)

楼主
 

【micropython教程】+4 SPI驱动OLED显示成功 [复制链接]

 

看了不少人驱动OLED都是使用I2C,但是手上的OLED模块已经焊接好SPI的,所以尝试使用SPI进行驱动。。。
参考了ESP8266 SPI驱动OLED
https://bbs.eeworld.com.cn/forum.php?mod=viewthread&tid=508570
下面是我移植过来到PYboard CN2上面,一样是可以成功显示的。
话不多说,上代码
  1. # main.py -- put your code here!

  2. from pyb import LED  #add LED
  3. import machine
  4. from machine import Pin
  5. from machine import I2C
  6. from machine import SPI
  7. import ssd1306
  8. import math
  9. import time

  10. # construct an I2C bus
  11. #i2c = I2C(scl=Pin(14), sda=Pin(2), freq=100000)
  12. #display = ssd1306.SSD1306_I2C(128,64, i2c)

  13. # construct an SPI bus on the given pins
  14. # polarity is the idle state of SCK
  15. # phase=0 means sample on the first edge of SCK, phase=1 means the second

  16. #                                  CN2    OLED
  17. #Pin(14)--SCK  machine.Pin("Y6 ") --B13  --D0
  18. #Pin(13)--MOSI machine.Pin("Y8 ") --B15  --D1
  19. #Pin(12)--MISO machine.Pin("Y7 ") --B14  --
  20. #Pin(16)--CS   machine.Pin("Y11") --B0   --CS
  21. #Pin(4)--RES   machine.Pin("Y9")  --B10  --RES
  22. #Pin(5)--DC    machine.Pin("Y10") --B11  --DC


  23. spi = SPI(baudrate=10000000, polarity=1, phase=0, sck=machine.Pin("Y6"), mosi=machine.Pin("Y8"), miso=machine.Pin("Y7"))
  24. display = ssd1306.SSD1306_SPI(128, 64, spi, machine.Pin("Y10"),machine.Pin("Y9"), machine.Pin("Y11"))

  25. led_blue = LED(4)
  26. led_blue.on()

  27. try:
  28.   display.poweron()
  29.   display.init_display()

  30.   display.text('ESP-mp SPI OLED',1,1)
  31.   display.text('Hi, MicroPython!',1,16)
  32.   display.text('By: hbzjt2012',1,31)
  33.   
  34.   # Write display buffer
  35.   display.show()
  36.   time.sleep(3)

  37.   display.fill(0)
  38.   for x in range(0, 128):
  39.     display.pixel(x, 32+int(math.sin(x/64*math.pi)*7 + 8), 1)
  40.   display.show()
  41.   time.sleep(3)

  42.   display.fill(0)

  43.   x = 0
  44.   y = 0
  45.   direction_x = True
  46.   direction_y = True

  47.   while True:
  48.     # Clear the previous lines
  49.     prev_x = x
  50.     prev_y = y

  51.     # Move bars
  52.     x += (1 if direction_x else -1)
  53.     y += (1 if direction_y else -1)

  54.     # Bounce back, if required
  55.     if x == 128:
  56.        direction_x = False
  57.        x = 126
  58.     elif x == -1:
  59.        direction_x = True
  60.        x = 1
  61.     if y == 64:
  62.        direction_y = False
  63.        y = 63
  64.     elif y == -1:
  65.        direction_y = True
  66.        y = 1

  67.     # Draw new lines
  68.     for i in range(64):
  69.       display.pixel(prev_x, i, False)
  70.       display.pixel(x, i, True)
  71.     for i in range(128):
  72.       display.pixel(i, prev_y, False)
  73.       display.pixel(i, y, True)

  74.     # Make sure the corners are active
  75.     display.pixel(0,   0,  True)
  76.     display.pixel(127, 0,  True)
  77.     display.pixel(0,   63, True)
  78.     display.pixel(127, 63, True)
  79.    
  80.     # Write display buffer
  81.     display.show()


  82. except Exception as ex:
  83.   led_blue.off()
  84.   print('Unexpected error: {0}'.format(ex))
  85.   display.poweroff()
复制代码



主要是修改了Pin管脚锁定和LED的增加,
另外板上是没有ssd1306.py文件,见附件 ssd1306.py (5.35 KB, 下载次数: 52)
ssd1306.py (5.35 KB, 下载次数: 52)
这个文件必须加到板子上的虚拟U盘上面,
编辑main.py文件,保存,重新复位上电就可以运行。。。


上图:

















此内容由EEWORLD论坛网友飞扬自我原创,如需转载或用于商业用途需征得作者同意并注明出处


查看本帖全部内容,请登录或者注册

最新回复

  详情 回复 发表于 2018-7-12 10:47

赞赏

1

查看全部赞赏

点赞 关注(3)
 
 

回复
举报

1278

帖子

2

TA的资源

版主

沙发
 
I2C刷屏慢,适合做菜单显示,
SPI速度快,可以快速刷屏
 
 
 

回复

1万

帖子

24

TA的资源

版主

板凳
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条
有奖直播:当AI遇见仿真,会有什么样的电子行业革新之路?
首场直播:Simcenter AI 赋能电子行业研发创新
直播时间:04月15日14:00-14:50

查看 »

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

 
机器人开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网 13

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表