注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题
hanyeguxingwo的个人空间 https://home.eeworld.com.cn/space-uid-288414.html [收藏] [复制] [分享] [RSS]
日志

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

已有 1002 次阅读2016-12-14 10:54 |个人分类:MicroPython

       昨天使用ESP-mp-01开发板的I2C成功驱动OLED显示,今天测试了SPI方式,同样成功点亮OLED。
【1】硬件连接:
ESP8266 SPI对应的引脚为Pin(14)--SCK,Pin(13)--MOSI,Pin(12)--MISO,Pin(16)--CS:


【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.   
  24.   # Write display buffer
  25.   display.show()
  26.   time.sleep(3)

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

  32.   display.fill(0)

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

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

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

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

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

  64.     # Make sure the corners are active
  65.     display.pixel(0,   0,  True)
  66.     display.pixel(127, 0,  True)
  67.     display.pixel(0,   63, True)
  68.     display.pixel(127, 63, True)
  69.    
  70.     # Write display buffer
  71.     display.show()


  72. except Exception as ex:
  73.   led_blue.low()
  74.   print('Unexpected error: {0}'.format(ex))
  75.   display.poweroff()
复制代码
【3】效果演示:






本文来自论坛,点击查看完整帖子内容。

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

热门文章