2017年1月27日 星期五

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

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

ESP8266 SPI对应的引脚为Pin(14)--SCK,Pin(13)--MOSI,Pin(12)--MISO,Pin(16)--CS:

http://www.micropython.org.cn/forum.php?mod=viewthread&tid=421&extra=page%3D1

【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】效果演示:







沒有留言:

張貼留言

WOKWI DHT22 & LED , Node-Red + SQLite database

 WOKWI DHT22 & LED , Node-Red + SQLite database Node-Red程式 [{"id":"6f0240353e534bbd","type":"comment&...