micropython驅動sh1106點亮oled


繼上一帖給esp32刷入micropython之后,忍不住給以esp12e也刷了micropython

這里先說一下webrepl:

通過wifi可以和esp8266交互,以及便攜的傳輸文件

首次使用:

1 import webrepl
2 webrepl.start()

中間可能讓你import 一個配置模塊來設置密碼具體即執行上兩句就知道了.

WebREPL - a prompt over WiFi
WebREPL allows you to use the Python prompt over WiFi, connecting through a browser. The latest versions of Firefox and Chrome are supported.

For your convenience, WebREPL client is hosted at http://micropython.org/webrepl . Alternatively, you can install it locally from the the GitHub repository https://github.com/micropython/webrepl .

Before connecting to WebREPL, you should set a password and enable it via a normal serial connection. Initial versions of MicroPython for ESP8266 came with WebREPL automatically enabled on the boot and with the ability to set a password via WiFi on the first connection, but as WebREPL was becoming more widely known and popular, the initial setup has switched to a wired connection for improved security:

import webrepl_setup
Follow the on-screen instructions and prompts. To make any changes active, you will need to reboot your device.

To use WebREPL connect your computer to the ESP8266’s access point (MicroPython-xxxxxx, see the previous section about this). If you have already reconfigured your ESP8266 to connect to a router then you can skip this part.

Once you are on the same network as the ESP8266 you click the “Connect” button (if you are connecting via a router then you may need to change the IP address, by default the IP address is correct when connected to the ESP8266’s access point). If the connection succeeds then you should see a password prompt.

Once you type the password configured at the setup step above, press Enter once more and you should get a prompt looking like >>>. You can now start typing Python commands!

以上參見micropython docs

初次設置好會提示重啟,然后電腦就可以通過serial或者瀏覽器http://micropython.org/webrepl/和micropython交互了,貼張圖:

先通過repl交互,如果之前設置好網絡的話,esp8266開機之后會自動連接,這一點很方便,

這里通過可以通過.ifconfig()查看IP或者進入路由器后台查看也行,然后把地址填入webrepl點擊連接,輸入密碼即可.

可以看到兩側的信息交互是同步的,在一端輸入另一端自動跟隨.右側可以上傳或者下載文件.


現在進入正題,micropython實現了spi iic等接口,還寫了ssd1306,但是看過我之前的博文可以知道,我自己做過1.3寸oled的轉接板,這用的是sh1106.

然后查了查好像micropython還沒有實現,可能沒工夫管這么小的地方吧,畢竟.96的ssd1306廣為人知.

然后之前自己也改過ssh1306的程序,就是有偏移而已,正尋思着要不要自己改改用?百度了一下,國內好像沒有,打梯子google一下,發現頁首兩個好像靠譜:

貼上地址吧:

https://github.com/robert-hh/SH1106 這個項目里面好像說是基於別人的,然后由於看到另一個更為直觀

https://blog.boochow.com/article/453949310.html這個是個日本佬的博客,直接放出代碼,我采用了這個,效果不錯!

當然啦,它用的SPI那會的和現在的有點區別可能,根據traceback刪掉第一個參數(你進這個地址去看,和我下面貼的程序就知道哪個參數了,就是哪個1),之后ctrl e進去 ctrl d屏幕竟然就直接點亮了,(當然這里還稍微改了改pin)

貼上我的代碼:

from micropython import const
from ssd1306 import SSD1306_SPI

SET_LOW_COLUMN      = const(0x00)
SET_HIGH_COLUMN     = const(0x10)
SET_PAGE_ADDR       = const(0xb0)
SET_DISP_START_LINE = const(0x40)

class SH1106_SPI(SSD1306_SPI):
    def show(self):
        for pg in range(0, self.pages):
            for cmd in (
                SET_PAGE_ADDR | pg,
                SET_LOW_COLUMN | 2,
                SET_HIGH_COLUMN | 0,
                ):
                self.write_cmd(cmd)
            self.write_data(self.buffer[pg * 0x80:(pg + 1) * 0x80])

#sh1106_spi.py

from sh1106 import SH1106_SPI
from machine import Pin, SPI
spi = SPI(baudrate=8000000, polarity=0, phase=0, sck=Pin(14), mosi=Pin(13), miso=Pin(12))
oled = SH1106_SPI(128, 64, spi, dc=Pin(16), res=Pin(15), cs=Pin(5))
oled.line(0,0,128,64,1);oled.show()
oled.line(128,0,0,64,1);oled.show()
oled.text('SH1106',36,0);oled.show()
oled.text('Micropython',24,56);oled.show()

注釋之前的保存為sh1106.py 然后上傳到esp,然后下面的直接ctrl e,效果很好!

這里可以體現出前人栽樹后人乘涼的好處了,也意識到梯子很有用.

這里這塊板子直接用esp12e,左側兩個按鍵,一個復位一個io0,背面飛線接spi,就不貼圖了,

我給出引腳定義:

GND VCC D0 D1 RES DC CS(我做的板子cs直接接地了)

D0接sck D1接mosi res接的io15 dc接的io16 至於程序里面dc我分配了引腳,是因為現在還沒搞清楚怎樣丟給它一個None,我直接賦值None不行,直接丟個空的io給它吧,之后再研究...

本來是想做個時鍾,數碼管io占用太多,然后想到了oled,我有兩種.66和1.3,.66的太小了,也想過兩塊拼一起,emmm太麻煩了.

先進行到這一步吧!

ADDED:

對上面括號參數的附加解釋:(僅僅是那個1)

上地址:

這是github ss1306.py

這是docs spi

class machine.SPI(id, ...)
Construct an SPI object on the given bus, id. Values of id depend on a particular port and its hardware. Values 0, 1, etc. are commonly used to select hardware SPI block #0, #1, etc. Value -1 can be used for bitbanging (software) implementation of SPI (if supported by a port).
SPI.init(baudrate=1000000, *, polarity=0, phase=0, bits=8, firstbit=SPI.MSB, sck=None, mosi=None, miso=None, pins=(SCK, MOSI, MISO))
Initialise the SPI bus with the given parameters:

baudrate is the SCK clock rate.
polarity can be 0 or 1, and is the level the idle clock line sits at.
phase can be 0 or 1 to sample data on the first or second clock edge respectively.
bits is the width in bits of each transfer. Only 8 is guaranteed to be supported by all hardware.
firstbit can be SPI.MSB or SPI.LSB.
sck, mosi, miso are pins (machine.Pin) objects to use for bus signals. For most hardware SPI blocks (as selected by id parameter to the constructor), pins are fixed and cannot be changed. In some cases, hardware blocks allow 2-3 alternative pin sets for a hardware SPI block. Arbitrary pin assignments are possible only for a bitbanging SPI driver (id = -1).
pins - WiPy port doesn’t sck, mosi, miso arguments, and instead allows to specify them as a tuple of pins parameter.

 

也就是說,在SPI的構造里面如果給定了id那么后面的sck ,mosi, miso就不需要指定啦,反之如果沒有指定id就需要了.

像這樣:

1 from sh1106 import SH1106_SPI
2 from machine import Pin, SPI
3 #spi = SPI(baudrate=8000000, polarity=0, phase=0, sck=Pin(14), mosi=Pin(13), miso=Pin(12))
4 spi = SPI(1,baudrate=8000000, polarity=0, phase=0)
5 oled = SH1106_SPI(128, 64, spi, dc=Pin(16), res=Pin(15), cs=Pin(5))
6 oled.line(0,0,128,64,1);oled.show()
7 oled.line(128,0,0,64,1);oled.show()
8 oled.text('SH1106',36,0);oled.show()
9 oled.text('Micropython',24,56);oled.show()

至於cs引腳,

class SSD1306_SPI(SSD1306):
    def __init__(self, width, height, spi, dc, res, cs, external_vcc=False):
        self.rate = 10 * 1024 * 1024
        dc.init(dc.OUT, value=0)
        res.init(res.OUT, value=0)
        cs.init(cs.OUT, value=1)
        self.spi = spi
        self.dc = dc
        self.res = res
        self.cs = cs
        import time
        self.res(1)
        time.sleep_ms(1)
        self.res(0)
        time.sleep_ms(10)
        self.res(1)
        super().__init__(width, height, external_vcc)

可以看到cs被傳入構造,作為時序中的一個動作,不能省略啊!暫時就賦值給不用的io吧


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM