ESP-mp-01开发板SPI驱动OLED显示

[复制链接]
 楼主| hbzjt2011 发表于 2016-12-14 11:01 | 显示全部楼层 |阅读模式
       昨天使用ESP-mp-01开发板的I2C成功驱动OLED显示,今天测试了SPI方式,同样成功点亮OLED。
【1】硬件连接:
ESP8266 SPI对应的引脚为Pin(14)--SCK,Pin(13)--MOSI,Pin(12)--MISO,Pin(16)--CS:
Screenshot_1.png
107764672465961399.jpg
【2】程序源码:
  1. # main.py -- put your code here!

  2. import machine
  3. from machine import Pin,I2C,SPI
  4. import ssd1306
  5. import math
  6. import time

  7. # construct an I2C bus
  8. #i2c = I2C(scl=Pin(14), sda=Pin(2), freq=100000)
  9. #display = ssd1306.SSD1306_I2C(128,64, i2c)

  10. # construct an SPI bus on the given pins
  11. # polarity is the idle state of SCK
  12. # phase=0 means sample on the first edge of SCK, phase=1 means the second
  13. spi = SPI(baudrate=10000000, polarity=1, phase=0, sck=Pin(14,Pin.OUT), mosi=Pin(13,Pin.OUT), miso=Pin(12))
  14. display = ssd1306.SSD1306_SPI(128, 64, spi, Pin(5),Pin(4), Pin(16))

  15. led_blue = machine.Pin(2, Pin.OUT) # 设置 GPIO2 为输出
  16. led_blue.high()

  17. try:
  18. display.poweron()
  19. display.init_display()

  20. display.text('ESP-mp SPI OLED',1,1)
  21. display.text('Hi, MicroPython!',1,16)
  22. display.text('By: hbzjt2012',1,31)

  23. # Write display buffer
  24. display.show()
  25. time.sleep(3)

  26. display.fill(0)
  27. for x in range(0, 128):
  28. display.pixel(x, 32+int(math.sin(x/64*math.pi)*7 + 8), 1)
  29. display.show()
  30. time.sleep(3)

  31. display.fill(0)

  32. x = 0
  33. y = 0
  34. direction_x = True
  35. direction_y = True

  36. while True:
  37. # Clear the previous lines
  38. prev_x = x
  39. prev_y = y

  40. # Move bars
  41. x += (1 if direction_x else -1)
  42. y += (1 if direction_y else -1)

  43. # Bounce back, if required
  44. if x == 128:
  45. direction_x = False
  46. x = 126
  47. elif x == -1:
  48. direction_x = True
  49. x = 1
  50. if y == 64:
  51. direction_y = False
  52. y = 63
  53. elif y == -1:
  54. direction_y = True
  55. y = 1

  56. # Draw new lines
  57. for i in range(64):
  58. display.pixel(prev_x, i, False)
  59. display.pixel(x, i, True)
  60. for i in range(128):
  61. display.pixel(i, prev_y, False)
  62. display.pixel(i, y, True)

  63. # Make sure the corners are active
  64. display.pixel(0, 0, True)
  65. display.pixel(127, 0, True)
  66. display.pixel(0, 63, True)
  67. display.pixel(127, 63, True)

  68. # Write display buffer
  69. display.show()


  70. except Exception as ex:
  71. led_blue.low()
  72. print('Unexpected error: {0}'.format(ex))
  73. display.poweroff()

【3】效果演示:
898966750992991030.jpg

360432675130792179.jpg
432100278202971415.jpg
335765872437636975.jpg
QQ图片20161214102726.gif





zhanzr21 发表于 2016-12-15 01:01 | 显示全部楼层
好,支持!
phlr 发表于 2016-12-20 14:48 | 显示全部楼层
支持一下!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:欢迎参与LabVIEW版块的讨论学习! 点我一键即达

256

主题

2827

帖子

44

粉丝
快速回复 在线客服 返回列表 返回顶部
个人签名:欢迎参与LabVIEW版块的讨论学习! 点我一键即达

256

主题

2827

帖子

44

粉丝
快速回复 在线客服 返回列表 返回顶部