|
在之前的帖子中PYB Nano开发板使用了SPI方式驱动OLED进行显示,今天发现手里的小e开发板使用的OLED为I2C方式,于是PYB Nano开发板用I2C驱动OLED进行了验证。
【1】硬件连接:
使用PYB Nano开发板的I2C(1),对应的引脚为Y0(SDA)、Y1(SCL):
【2】程序源码:
库文件参看:PYB Nano驱动OLED显示- import pyb
- from ssd1306 import SSD1306
- # SPI
- #display = SSD1306(pinout={'dc': 'Y9',
- # 'res': 'Y10'},
- # height=64,
- # external_vcc=False)
- # I2C connected to Y0, Y1 (I2C bus 1)
- display = SSD1306(pinout={'sda': 'Y0',
- 'scl': 'Y1'},
- 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】显示效果:
|
赞赏
-
1
查看全部赞赏
-
|