PYB Nano驱动OLED显示

这是论坛网友hbzjt2012的作品:
http://www.micropython.org.cn/forum.php?mod=viewthread&tid=408&extra=

这次使用PYB Nano驱动0.96寸OLED显示屏,此次参考了国外网友的例程,OLED可以使用SPI或者I2C均可。

【1】硬件连接:
这里以SPI连接方式为例,使用PYB Nano的SPI1:

162856l7rjgegij9re6hlb.jpg
【2】程序源码:
库文件:SSD1306

import pyb
from ssd1306 import SSD1306

# SPI
display = SSD1306(pinout={'dc': 'Y9',
                          'res': 'Y10'},
                  height=64,
                  external_vcc=False)

# I2C connected to Y9, Y10 (I2C bus 2)
# display = SSD1306(pinout={'sda': 'Y10',
#                           'scl': 'Y9'},
#                   height=64,
#                   external_vcc=False)

led_red = pyb.LED(1)
led_red.off()

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

  display.draw_text(1,1,'PYB Nano OLED Test',size=1,space=1)
  display.draw_text(1,10,'Hello MicroPython!',size=1,space=1)
  
  # Write display buffer
  display.display()
  pyb.delay(10000)

  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.set_pixel(prev_x, i, False)
      display.set_pixel(x, i, True)
    for i in range(128):
      display.set_pixel(i, prev_y, False)
      display.set_pixel(i, y, True)

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

except Exception as ex:
  led_red.on()
  print('Unexpected error: {0}'.format(ex))
  display.poweroff()

【3】显示效果:

162858jl7dksjs7z604z0k.jpg 
162858r4pgptg6tgm4w8ms.jpg 

转载于:https://my.oschina.net/shaoziyang/blog/800349

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值