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

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

已有 1047 次阅读2016-12-13 19:51 |个人分类:MicroPython

       ESP-mp-01开发板是基于ESP8266 Soc的可以运行MicroPython的开发板,不像STM32版本的Pyboard,ESP8266本身没有USB,只能通过TTL串口和Wifi访问ESP8266。文件传输需要借助uPyLoader或ESPyHarp等工具来进行文件传输。此次使用开发板的I2C功能来驱动OLED显示。
【1】硬件连接:

ESP8266 I2C对应的引脚为Pin(14)--SCL,Pin(2)--SDA:
【2】程序源码:
  1. # main.py -- put your code here!

  2. import machine
  3. from machine import Pin,I2C
  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. led_blue = machine.Pin(2, Pin.OUT)  # 设置 GPIO2 为输出
  11. led_blue.high()

  12. try:
  13.   display.poweron()
  14.   display.init_display()

  15.   display.text('PYBNano I2C OLED',1,1)
  16.   display.text('Hi, MicroPython!',1,16)
  17.   display.text('By: hbzjt2012',1,31)
  18.   
  19.   # Write display buffer
  20.   display.show()
  21.   time.sleep(10)

  22.   display.fill(0)
  23.   for x in range(0, 128):
  24.     display.pixel(x, 32+int(math.sin(x/64*math.pi)*7 + 8), 1)
  25.   display.show()
  26.   time.sleep(10)

  27.   display.fill(0)

  28.   x = 0
  29.   y = 0
  30.   direction_x = True
  31.   direction_y = True

  32.   while True:
  33.     # Clear the previous lines
  34.     prev_x = x
  35.     prev_y = y

  36.     # Move bars
  37.     x += (1 if direction_x else -1)
  38.     y += (1 if direction_y else -1)

  39.     # Bounce back, if required
  40.     if x == 128:
  41.        direction_x = False
  42.        x = 126
  43.     elif x == -1:
  44.        direction_x = True
  45.        x = 1
  46.     if y == 64:
  47.        direction_y = False
  48.        y = 63
  49.     elif y == -1:
  50.        direction_y = True
  51.        y = 1

  52.     # Draw new lines
  53.     for i in range(64):
  54.       display.pixel(prev_x, i, False)
  55.       display.pixel(x, i, True)
  56.     for i in range(128):
  57.       display.pixel(i, prev_y, False)
  58.       display.pixel(i, y, True)

  59.     # Make sure the corners are active
  60.     display.pixel(0,   0,  True)
  61.     display.pixel(127, 0,  True)
  62.     display.pixel(0,   63, True)
  63.     display.pixel(127, 63, True)
  64.    
  65.     # Write display buffer
  66.     display.show()


  67. except Exception as ex:
  68.   led_blue.low()
  69.   print('Unexpected error: {0}'.format(ex))
  70.   display.poweroff()
复制代码

【3】效果演示:







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

评论 (0 个评论)

facelist doodle 涂鸦板

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

热门文章