Skip to content

Commit 28eb361

Browse files
committed
2 parents 29accfd + 3ab2a41 commit 28eb361

12 files changed

Lines changed: 1254 additions & 415 deletions

File tree

02.inputs/uart.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import time
2+
from machine import UART
3+
4+
uart = UART(2) # UART(2) RX16 TX17
5+
uart.init(57600, bits=8, parity=None, stop=1)
6+
7+
while True:
8+
recvlen = uart.any()
9+
if recvlen > 0:
10+
buffer = uart.read(recvlen)
11+
uart.write(buffer)
12+
print(buffer)

07.sensors/cs5460a/cs5460a.py

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
from machine import Pin, SPI
2+
3+
from micropython import const
4+
5+
CS5460A_RMS_VOLTAGE = const(0x18)
6+
7+
CS5460A_RMS_CURRENT = const(0x16)
8+
9+
CS5460A_TRUE_POWER = const(0x14)
10+
11+
CS5460A_CFG_READ = const(0x00) # //reg read: config
12+
13+
CS5460A_CFG_READ_IGN = const(0x04) # //reg read: Ign
14+
15+
CS5460A_CFG_READ_VGN = const(0x08) # //reg read: Vgn
16+
17+
CS5460A_CFG_READ_CYCLE = const(0x0a) # //reg read: cycle count
18+
19+
CS5460A_CFG_POWER_UP = const(0xa0) # // power-up/halt
20+
21+
CS5460A_CFG_GAIN = const(0x40) # // reg write: config. PGA Gain 10x, IHPF=1, VHPF=1
22+
23+
CS5460A_CFG_GAIN1 = const(0x01)
24+
25+
CS5460A_CFG_GAIN2 = const(0x00)
26+
27+
CS5460A_CFG_GAIN3 = const(0x61)
28+
29+
CS5460A_CFG_IGN= const(0x44) # // reg write: Ign [current chan gain].
30+
31+
CS5460A_CFG_IGN1 = const(0x40)
32+
33+
CS5460A_CFG_IGN2 = const(0x00)
34+
35+
CS5460A_CFG_IGN3 = const(0x00)
36+
37+
CS5460A_CFG_VGN = const(0x48) # reg write: Vgn [voltage chan gain]
38+
39+
CS5460A_CFG_VGN1 = const(0x41)
40+
41+
CS5460A_CFG_VGN2 = const(0xA0)
42+
43+
CS5460A_CFG_VGN3 = const(0xEA)
44+
45+
CS5460A_START_CONV = const(0xe8) #command : start convert
46+
47+
48+
class cs5460a(object):
49+
import utime
50+
def __init__(self, spi, cs=2, rst=4):
51+
self.spi = spi
52+
self.cs = Pin(cs, Pin.OUT)
53+
self.rst = Pin(rst, Pin.OUT)
54+
self.FLOAT24 = 16777216.0 # 2^24
55+
self.VOLTAGE_MULTIPLIER = (1 / self.FLOAT24 * 367)
56+
self.CURRENT_MULTIPLIER = (1 / self.FLOAT24 * 11.66)
57+
self.POWER_MULTIPLIER = (1 / self.FLOAT24 * 1.024 * 367 * 11.66 * 2)
58+
59+
def read(self, addr):
60+
b = bytearray([0xfe, 0xfe, 0xfe])
61+
buf = bytearray(3)
62+
self.cs.value(0)
63+
self.spi.write(bytearray([addr]))
64+
self.spi.write_readinto(b, buf)
65+
self.cs.value(1)
66+
# print(buf[0],buf[1],buf[2])
67+
return buf
68+
69+
def write(self, addr, data):
70+
self.cs.value(0)
71+
self.spi.write(bytearray([addr]))
72+
self.spi.write(data)
73+
self.cs.value(1)
74+
# print('write_ok')
75+
76+
def cs5460a_setup(self):
77+
self.rst.value(0)
78+
utime.sleep_ms(50)
79+
self.rst.value(1) # reset the cs5460a
80+
81+
self.cs.value(0)
82+
self.spi.write(bytearray([CS5460A_CFG_POWER_UP]))
83+
self.cs.value(1) # the command of the power_up
84+
85+
self.write(CS5460A_CFG_GAIN, bytearray([CS5460A_CFG_GAIN1, CS5460A_CFG_GAIN2, CS5460A_CFG_GAIN3])) # set
86+
self.write(CS5460A_CFG_VGN , bytearray([CS5460A_CFG_VGN1, CS5460A_CFG_VGN2, CS5460A_CFG_VGN3]))#V
87+
self.write(CS5460A_CFG_IGN, bytearray([CS5460A_CFG_IGN1, CS5460A_CFG_IGN2, CS5460A_CFG_IGN3]))#A
88+
89+
self.cs.value(0)
90+
self.spi.write(bytearray([CS5460A_START_CONV]))
91+
self.cs.value(1) # start to convert
92+
93+
def _conv(self, true_power):
94+
if true_power[0] > 0x80:
95+
a = bytearray([~true_power[0]])
96+
a[0] &= 0x7f
97+
# print(a)
98+
b = bytearray([~true_power[1]])
99+
# print(b)
100+
c = bytearray([~true_power[2]])
101+
# print(c)
102+
temp = ((c[0] + b[0] * 256 + a[0] * 65536) + 1)
103+
else:
104+
temp = ((true_power[0] + true_power[0] * 256 + true_power[0] * 65536) + 1)
105+
return temp
106+
107+
def read_u(self):
108+
voltage = self.read(CS5460A_RMS_VOLTAGE)
109+
temp = (voltage[2] + voltage[1] * 256 + voltage[0] * 65536)
110+
V = self.VOLTAGE_MULTIPLIER * temp
111+
return V
112+
113+
def read_i(self):
114+
current = self.read(CS5460A_RMS_CURRENT)
115+
temp = (current[2] + current[1] * 256 + current[0] * 65536)
116+
A = self.CURRENT_MULTIPLIER * temp
117+
return A
118+
119+
def read_p(self):
120+
true_power = self.read(CS5460A_TRUE_POWER)
121+
temp = self._conv(true_power)
122+
P = self.POWER_MULTIPLIER * temp
123+
return P
124+
125+
126+
from machine import Pin
127+
import utime
128+
129+
p = Pin(18, Pin.OUT)
130+
p.value(1)
131+
132+
133+
def unit_test():
134+
vspi = SPI(-1, sck=Pin(5), mosi=Pin(23), miso=Pin(19), baudrate=2000000) # -1 software spi
135+
ts = cs5460a(vspi)
136+
ts.cs5460a_setup() # 初始化
137+
138+
while True:
139+
utime.sleep_ms(1000)
140+
k = ts.read(0x1e)
141+
# print(k)
142+
# current=ts.read(0x16)
143+
# temp = (current[2] + current[1] * 256 + current[0] * 65536)/16777216
144+
# print(temp)
145+
V = ts.read_u()
146+
A = ts.read_i()
147+
P = ts.read_p()
148+
print('current=%.2f A' % A)
149+
print('voltage=%.2f V' % V)
150+
print('ture_power=%.2f W' % P)
151+
152+
153+
if __name__ == '__main__':
154+
unit_test()

07.sensors/ssd1306.py

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# MicroPython SSD1306 OLED driver, I2C and SPI interfaces
2+
3+
import time
4+
import framebuf
5+
6+
7+
# register definitions
8+
SET_CONTRAST = const(0x81)
9+
SET_ENTIRE_ON = const(0xa4)
10+
SET_NORM_INV = const(0xa6)
11+
SET_DISP = const(0xae)
12+
SET_MEM_ADDR = const(0x20)
13+
SET_COL_ADDR = const(0x21)
14+
SET_PAGE_ADDR = const(0x22)
15+
SET_DISP_START_LINE = const(0x40)
16+
SET_SEG_REMAP = const(0xa0)
17+
SET_MUX_RATIO = const(0xa8)
18+
SET_COM_OUT_DIR = const(0xc0)
19+
SET_DISP_OFFSET = const(0xd3)
20+
SET_COM_PIN_CFG = const(0xda)
21+
SET_DISP_CLK_DIV = const(0xd5)
22+
SET_PRECHARGE = const(0xd9)
23+
SET_VCOM_DESEL = const(0xdb)
24+
SET_CHARGE_PUMP = const(0x8d)
25+
26+
27+
class SSD1306:
28+
def __init__(self, width, height, external_vcc):
29+
self.width = width
30+
self.height = height
31+
self.external_vcc = external_vcc
32+
self.pages = self.height // 8
33+
# Note the subclass must initialize self.framebuf to a framebuffer.
34+
# This is necessary because the underlying data buffer is different
35+
# between I2C and SPI implementations (I2C needs an extra byte).
36+
self.poweron()
37+
self.init_display()
38+
39+
def init_display(self):
40+
for cmd in (
41+
SET_DISP | 0x00, # off
42+
# address setting
43+
SET_MEM_ADDR, 0x00, # horizontal
44+
# resolution and layout
45+
SET_DISP_START_LINE | 0x00,
46+
SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0
47+
SET_MUX_RATIO, self.height - 1,
48+
SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0
49+
SET_DISP_OFFSET, 0x00,
50+
SET_COM_PIN_CFG, 0x02 if self.height == 32 else 0x12,
51+
# timing and driving scheme
52+
SET_DISP_CLK_DIV, 0x80,
53+
SET_PRECHARGE, 0x22 if self.external_vcc else 0xf1,
54+
SET_VCOM_DESEL, 0x30, # 0.83*Vcc
55+
# display
56+
SET_CONTRAST, 0xff, # maximum
57+
SET_ENTIRE_ON, # output follows RAM contents
58+
SET_NORM_INV, # not inverted
59+
# charge pump
60+
SET_CHARGE_PUMP, 0x10 if self.external_vcc else 0x14,
61+
SET_DISP | 0x01): # on
62+
self.write_cmd(cmd)
63+
self.fill(0)
64+
self.show()
65+
66+
def poweroff(self):
67+
self.write_cmd(SET_DISP | 0x00)
68+
69+
def contrast(self, contrast):
70+
self.write_cmd(SET_CONTRAST)
71+
self.write_cmd(contrast)
72+
73+
def invert(self, invert):
74+
self.write_cmd(SET_NORM_INV | (invert & 1))
75+
76+
def show(self):
77+
x0 = 0
78+
x1 = self.width - 1
79+
if self.width == 64:
80+
# displays with width of 64 pixels are shifted by 32
81+
x0 += 32
82+
x1 += 32
83+
self.write_cmd(SET_COL_ADDR)
84+
self.write_cmd(x0)
85+
self.write_cmd(x1)
86+
self.write_cmd(SET_PAGE_ADDR)
87+
self.write_cmd(0)
88+
self.write_cmd(self.pages - 1)
89+
self.write_framebuf()
90+
91+
def fill(self, col):
92+
self.framebuf.fill(col)
93+
94+
def pixel(self, x, y, col):
95+
self.framebuf.pixel(x, y, col)
96+
97+
def scroll(self, dx, dy):
98+
self.framebuf.scroll(dx, dy)
99+
100+
def text(self, string, x, y, col=1):
101+
self.framebuf.text(string, x, y, col)
102+
103+
104+
class SSD1306_I2C(SSD1306):
105+
def __init__(self, width, height, i2c, addr=0x3c, external_vcc=False):
106+
self.i2c = i2c
107+
self.addr = addr
108+
self.temp = bytearray(2)
109+
# Add an extra byte to the data buffer to hold an I2C data/command byte
110+
# to use hardware-compatible I2C transactions. A memoryview of the
111+
# buffer is used to mask this byte from the framebuffer operations
112+
# (without a major memory hit as memoryview doesn't copy to a separate
113+
# buffer).
114+
self.buffer = bytearray(((height // 8) * width) + 1)
115+
self.buffer[0] = 0x40 # Set first byte of data buffer to Co=0, D/C=1
116+
self.framebuf = framebuf.FrameBuffer1(memoryview(self.buffer)[1:], width, height)
117+
super().__init__(width, height, external_vcc)
118+
119+
def write_cmd(self, cmd):
120+
self.temp[0] = 0x80 # Co=1, D/C#=0
121+
self.temp[1] = cmd
122+
self.i2c.writeto(self.addr, self.temp)
123+
124+
def write_framebuf(self):
125+
# Blast out the frame buffer using a single I2C transaction to support
126+
# hardware I2C interfaces.
127+
self.i2c.writeto(self.addr, self.buffer)
128+
129+
def poweron(self):
130+
pass
131+
132+
133+
class SSD1306_SPI(SSD1306):
134+
def __init__(self, width, height, spi, dc, res, cs, external_vcc=False):
135+
self.rate = 10 * 1024 * 1024
136+
dc.init(dc.OUT, value=0)
137+
res.init(res.OUT, value=0)
138+
cs.init(cs.OUT, value=1)
139+
self.spi = spi
140+
self.dc = dc
141+
self.res = res
142+
self.cs = cs
143+
self.buffer = bytearray((height // 8) * width)
144+
self.framebuf = framebuf.FrameBuffer1(self.buffer, width, height)
145+
super().__init__(width, height, external_vcc)
146+
147+
def write_cmd(self, cmd):
148+
self.spi.init(baudrate=self.rate, polarity=0, phase=0)
149+
self.cs.high()
150+
self.dc.low()
151+
self.cs.low()
152+
self.spi.write(bytearray([cmd]))
153+
self.cs.high()
154+
155+
def write_framebuf(self):
156+
self.spi.init(baudrate=self.rate, polarity=0, phase=0)
157+
self.cs.high()
158+
self.dc.high()
159+
self.cs.low()
160+
self.spi.write(self.buffer)
161+
self.cs.high()
162+
163+
def poweron(self):
164+
self.res.high()
165+
time.sleep_ms(1)
166+
self.res.low()
167+
time.sleep_ms(10)
168+
self.res.high()
169+
170+
def unit_test():
171+
import machine
172+
from ssd1306 import SSD1306_I2C
173+
174+
WIDTH = const(128)
175+
HEIGHT = const(64)
176+
sda_pin = machine.Pin(21)
177+
scl_pin = machine.Pin(22)
178+
179+
i2c = machine.I2C(scl=scl_pin, sda=sda_pin)
180+
181+
print(i2c.scan())
182+
183+
ssd = SSD1306_I2C(WIDTH, HEIGHT, i2c)
184+
ssd.fill(1)
185+
ssd.show()

0 commit comments

Comments
 (0)