|
【micropython教程】+4 SPI驱动OLED显示成功
[复制链接]
看了不少人驱动OLED都是使用I2C,但是手上的OLED模块已经焊接好SPI的,所以尝试使用SPI进行驱动。。。
参考了 ESP8266 SPI驱动OLED
https://bbs.eeworld.com.cn/forum.php?mod=viewthread&tid=508570
下面是我移植过来到PYboard CN2上面,一样是可以成功显示的。
话不多说,上代码
- # main.py -- put your code here!
- from pyb import LED #add LED
- import machine
- from machine import Pin
- from machine import I2C
- from machine import SPI
- 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)
- # construct an SPI bus on the given pins
- # polarity is the idle state of SCK
- # phase=0 means sample on the first edge of SCK, phase=1 means the second
- # CN2 OLED
- #Pin(14)--SCK machine.Pin("Y6 ") --B13 --D0
- #Pin(13)--MOSI machine.Pin("Y8 ") --B15 --D1
- #Pin(12)--MISO machine.Pin("Y7 ") --B14 --
- #Pin(16)--CS machine.Pin("Y11") --B0 --CS
- #Pin(4)--RES machine.Pin("Y9") --B10 --RES
- #Pin(5)--DC machine.Pin("Y10") --B11 --DC
- spi = SPI(baudrate=10000000, polarity=1, phase=0, sck=machine.Pin("Y6"), mosi=machine.Pin("Y8"), miso=machine.Pin("Y7"))
- display = ssd1306.SSD1306_SPI(128, 64, spi, machine.Pin("Y10"),machine.Pin("Y9"), machine.Pin("Y11"))
- led_blue = LED(4)
- led_blue.on()
- try:
- display.poweron()
- display.init_display()
- display.text('ESP-mp SPI OLED',1,1)
- display.text('Hi, MicroPython!',1,16)
- display.text('By: hbzjt2012',1,31)
-
- # Write display buffer
- display.show()
- time.sleep(3)
- 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(3)
- 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.off()
- print('Unexpected error: {0}'.format(ex))
- display.poweroff()
复制代码
主要是修改了Pin管脚锁定和LED的增加,
另外板上是没有ssd1306.py文件,见附件
ssd1306.py
(5.35 KB, 下载次数: 52)
ssd1306.py
(5.35 KB, 下载次数: 52)
这个文件必须加到板子上的虚拟U盘上面,
编辑main.py文件,保存,重新复位上电就可以运行。。。
上图:
此内容由EEWORLD论坛网友飞扬自我原创,如需转载或用于商业用途需征得作者同意并注明出处
|
赞赏
-
1
查看全部赞赏
-
|