打印
[其它]

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

[复制链接]
1707|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
hbzjt2011|  楼主 | 2016-12-13 20:03 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
       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】程序源码:
# main.py -- put your code here!

import machine
from machine import Pin,I2C
import ssd1306
import math
import time

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

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

try:
  display.poweron()
  display.init_display()

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

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

  display.fill(0)

  x = 0
  y = 0
  direction_x = True
  direction_y = True

  while True:
    # Clear the previous lines
    prev_x = x
    prev_y = y

    # Move bars
    x += (1 if direction_x else -1)
    y += (1 if direction_y else -1)

    # Bounce back, if required
    if x == 128:
       direction_x = False
       x = 126
    elif x == -1:
       direction_x = True
       x = 1
    if y == 64:
       direction_y = False
       y = 63
    elif y == -1:
       direction_y = True
       y = 1

    # Draw new lines
    for i in range(64):
      display.pixel(prev_x, i, False)
      display.pixel(x, i, True)
    for i in range(128):
      display.pixel(i, prev_y, False)
      display.pixel(i, y, True)

    # Make sure the corners are active
    display.pixel(0,   0,  True)
    display.pixel(127, 0,  True)
    display.pixel(0,   63, True)
    display.pixel(127, 63, True)
   
    # Write display buffer
    display.show()


except Exception as ex:
  led_blue.low()
  print('Unexpected error: {0}'.format(ex))
  display.poweroff()
【3】效果演示:









相关帖子

沙发
zhanzr21| | 2016-12-15 01:03 | 只看该作者
不错, 支持!

使用特权

评论回复
板凳
巧克力娃娃| | 2016-12-15 09:45 | 只看该作者
持续关注!

使用特权

评论回复
地板
SD10A| | 2017-11-2 20:57 | 只看该作者
我想问问LZ  横排显示 是哪个指令  我做的变成纵排的了

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

157

主题

2338

帖子

40

粉丝