Skip to content

Commit 18a2e2e

Browse files
authored
Add files via upload
1 parent ed00078 commit 18a2e2e

File tree

5 files changed

+464
-0
lines changed

5 files changed

+464
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
from micropython import const
2+
import framebuf
3+
4+
5+
SH1107_I2C_address = const(0x3C)
6+
7+
8+
SH1107_SET_LOWER_COLUMN_ADDRESS = const(0x00)
9+
SH1107_SET_UPPER_COLUMN_ADDRESS = const(0x10)
10+
SH1107_SET_PAGE_MEMORY_ADDRESSING_MODE = const(0x20)
11+
SH1107_SET_VERTICAL_MEMORY_ADDRESSING_MODE = const(0x21)
12+
SH1107_SET_CONSTRAST_CONTROL = const(0x81)
13+
SH1107_SET_DC_DC_OFF_MODE = const(0x8A)
14+
SH1107_SET_DC_DC_ON_MODE = const(0x8B)
15+
SH1107_SET_SEGMENT_REMAP_NORMAL = const(0xA0)
16+
SH1107_SET_SEGMENT_REMAP_REVERSE = const(0xA1)
17+
SH1107_SET_ENTIRE_DISPLAY_OFF = const(0xA4)
18+
SH1107_SET_ENTIRE_DISPLAY_ON = const(0xA5)
19+
SH1107_SET_NORMAL_DISPLAY = const(0xA6)
20+
SH1107_SET_REVERSE_DISPLAY = const(0xA7)
21+
SH1107_SET_MULTIPLEX_RATIO = const(0xA8)
22+
SH1107_SET_DC_DC_CONTROL_MODE = const(0xAD)
23+
SH1107_DISPLAY_OFF = const(0xAE)
24+
SH1107_DISPLAY_ON = const(0xAF)
25+
SH1107_SET_PAGE_ADDRESS = const(0xB0)
26+
SH1107_SET_COMMON_OUTPUT_SCAN_DIRECTION = const(0xC0)
27+
SH1107_SET_DISPLAY_OFFSET = const(0xD3)
28+
SH1107_SET_DISPLAY_CLOCK_FREQUENCY = const(0xD5)
29+
SH1107_SET_PRECHARGE_DISCHARGE_PERIOD = const(0xD9)
30+
SH1107_SET_VCOM_DESELECT_LEVEL = const(0xDB)
31+
SH1107_SET_DISPLAY_START_LINE = const(0xDC)
32+
33+
34+
disp_width = const(128)
35+
disp_height = const(64)
36+
disp_pages = const((disp_height >> 3))
37+
38+
39+
class OLED(framebuf.FrameBuffer):
40+
41+
def __init__(self, _i2c, _i2c_address = SH1107_I2C_address):
42+
self.width = disp_width
43+
self.height = disp_height
44+
self.pages = disp_pages
45+
46+
self.line_bytes = (self.width >> 3)
47+
size = (self.width * self.pages)
48+
self.curr_buffer = bytearray(b'\x00' * size)
49+
self.prev_buffer = bytearray(b'\xFF' * size)
50+
51+
self.write_list = [b"\x40", None]
52+
self.temp = bytearray(0x02)
53+
54+
self.WHITE = 1
55+
self.BLACK = 0
56+
57+
self.i2c = _i2c
58+
self.i2c_addr = _i2c_address
59+
60+
super().__init__(self.curr_buffer, self.width, self.height, framebuf.MONO_HMSB)
61+
self.init_display()
62+
63+
64+
def write_command(self, cmd):
65+
self.temp[0] = 0x80
66+
self.temp[1] = cmd
67+
self.i2c.writeto(self.i2c_addr, self.temp)
68+
69+
70+
def write_data(self, data_buffer):
71+
self.write_list[1] = data_buffer
72+
self.i2c.writevto(self.i2c_addr, self.write_list)
73+
74+
75+
def init_display(self):
76+
self.write_command(SH1107_DISPLAY_OFF)
77+
78+
self.write_command(SH1107_SET_LOWER_COLUMN_ADDRESS)
79+
self.write_command(SH1107_SET_UPPER_COLUMN_ADDRESS)
80+
81+
self.write_command(SH1107_SET_PAGE_ADDRESS)
82+
83+
self.write_command(SH1107_SET_DISPLAY_START_LINE)
84+
self.write_command(0x00)
85+
self.write_command(SH1107_SET_CONSTRAST_CONTROL)
86+
self.write_command(0x44)
87+
self.write_command(SH1107_SET_VERTICAL_MEMORY_ADDRESSING_MODE)
88+
89+
self.write_command(SH1107_SET_SEGMENT_REMAP_REVERSE)
90+
self.write_command(SH1107_SET_COMMON_OUTPUT_SCAN_DIRECTION)
91+
self.write_command(SH1107_SET_ENTIRE_DISPLAY_OFF)
92+
93+
self.write_command(SH1107_SET_NORMAL_DISPLAY)
94+
self.write_command(SH1107_SET_MULTIPLEX_RATIO)
95+
self.write_command(0x7F)
96+
97+
self.write_command(SH1107_SET_DISPLAY_OFFSET)
98+
self.write_command(0x60)
99+
100+
self.write_command(SH1107_SET_DISPLAY_CLOCK_FREQUENCY)
101+
self.write_command(0x81)
102+
103+
self.write_command(SH1107_SET_PRECHARGE_DISCHARGE_PERIOD)
104+
self.write_command(0x22)
105+
106+
self.write_command(SH1107_SET_VCOM_DESELECT_LEVEL)
107+
self.write_command(0x35)
108+
109+
self.write_command(SH1107_SET_DC_DC_CONTROL_MODE)
110+
self.write_command(SH1107_SET_DC_DC_OFF_MODE)
111+
self.write_command(SH1107_DISPLAY_ON)
112+
113+
114+
def modify_buf(self, offs, width):
115+
ptr = offs
116+
width += offs
117+
while(ptr < width):
118+
while((ptr < width) and (self.curr_buffer[ptr : (ptr + 0x08)] == self.prev_buffer[ptr : (ptr + 0x08)])):
119+
ptr += 0x08
120+
121+
if(ptr < width):
122+
first = ptr
123+
ptr += 0x08
124+
while((ptr < width) and (self.curr_buffer[ptr : (ptr + 0x08)] != self.prev_buffer[ptr : (ptr + 0x08)])):
125+
ptr += 0x08
126+
127+
yield first, ptr
128+
ptr += 0x08
129+
130+
131+
def show(self):
132+
for col in range(self.height):
133+
noffs = (col * self.line_bytes)
134+
for page1, page2 in self.modify_buf(noffs, self.line_bytes):
135+
self.write_command(SH1107_SET_PAGE_ADDRESS | (page1 - noffs))
136+
self.write_command(SH1107_SET_LOWER_COLUMN_ADDRESS | (col & 0x0F))
137+
self.write_command(SH1107_SET_UPPER_COLUMN_ADDRESS | ((col & 0x70) >> 0x04))
138+
self.write_data(self.curr_buffer[page1 : page2])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from pyb import Pin, ADC
2+
from machine import I2C
3+
from SH1107 import OLED
4+
from utime import sleep_ms, sleep_us
5+
import framebuf
6+
7+
8+
background = framebuf.FrameBuffer(bytearray(
9+
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x7f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
10+
b'\x80\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x40\x7f\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
11+
b'\xa0\xff\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xd0\xff\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
12+
b'\xe8\xff\xfb\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x07\xe8\xff\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08'
13+
b'\xe8\xff\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\xe8\xff\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10'
14+
b'\xe8\xff\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\xe8\xff\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10'
15+
b'\xe8\xff\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\xe8\xff\xfb\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x07'
16+
b'\xd0\xff\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa0\xff\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
17+
b'\x40\x7f\x11\x42\x08\x21\x84\x10\x42\x08\x21\x84\x10\x42\x08\x01\x80\x80\x10\x42\x08\x21\x84\x10\x42\x08\x21\x84\x10\x42\x08\x01'
18+
b'\x00\x7f\x10\x42\x08\x21\x84\x10\x42\x08\x21\x84\x10\x42\x08\x01\x00\x00\x10\x40\x00\x01\x04\x10\x40\x00\x01\x04\x10\x40\x00\x01'
19+
b'\x00\x00\x10\x40\x00\x01\x04\x10\x40\x00\x01\x04\x10\x40\x00\x01\x00\x00\x10\x40\x00\x01\x04\x10\x40\x00\x01\x04\x10\x40\x00\x01'
20+
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
21+
b'\x00\x00\x38\x00\xe0\x0e\x00\xea\x00\xe0\x0e\x00\xee\x00\x98\x3b\x00\x00\x28\x00\x80\x0a\x00\xaa\x00\x20\x0a\x00\xaa\x00\x90\x2a'
22+
b'\x00\x00\x28\x00\xe0\x0a\x00\xae\x00\xe0\x0a\x00\xae\x00\x90\x2a\x00\x00\x28\x00\x20\x0a\x00\xa8\x00\xa0\x0a\x00\xaa\x00\x90\x2a'
23+
b'\x00\x00\x38\x00\xe0\x0e\x00\xe8\x00\xe0\x0e\x00\xee\x00\xb8\x3b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'),
24+
128, 30, framebuf.MONO_HMSB)
25+
26+
27+
TWI = I2C(scl = 'PB9', sda = 'PB8', freq = 400000)
28+
29+
oled = OLED(TWI)
30+
31+
adc = ADC(Pin('A0'))
32+
33+
34+
def map_value(v, x_min, x_max, y_min, y_max):
35+
return (y_min + (((y_max - y_min) / (x_max - x_min)) * (v - x_min)))
36+
37+
38+
def get_adc_avg():
39+
samples = 64
40+
temp = 0
41+
42+
while(samples > 0):
43+
temp += adc.read()
44+
sleep_us(100)
45+
samples -= 1
46+
47+
temp >>= 6
48+
49+
return temp
50+
51+
52+
oled.fill(oled.BLACK)
53+
oled.show()
54+
55+
56+
while(True):
57+
adc_value = get_adc_avg()
58+
59+
t = map_value(adc_value, 810, 3200, 0, 100)
60+
61+
if((t >= 0) and (t <= 100)):
62+
t_str = "T/deg C: " + str("%3.1f" %t)
63+
else:
64+
t_str = "T/deg C: ---"
65+
66+
oled.fill(oled.BLACK)
67+
oled.text(t_str, 6, 4, oled.WHITE)
68+
oled.text("ADC: " + str("%4u" %adc_value), 6, 16, oled.WHITE)
69+
oled.blit(background, 0, 30)
70+
l = int(map_value(t, 0, 100, 0, 100))
71+
oled.fill_rect(20, 38, l, 4, oled.WHITE)
72+
oled.show()
73+
print(t_str)
74+
sleep_ms(400)
75+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
from micropython import const
2+
from utime import sleep_ms
3+
4+
5+
class MODBUS_DS18B20():
6+
def __init__(self, _uart):
7+
self.tx_data_frame = bytearray(8)
8+
self.rx_data_frame = bytearray(9)
9+
10+
self.uart = _uart
11+
12+
13+
def generate_CRC16(self, value, length):
14+
n = 0
15+
s = 0
16+
crc_word = 0xFFFF
17+
18+
for s in range (0x00, length):
19+
crc_word ^= value[s]
20+
21+
for n in range (0x00, 0x08):
22+
if((crc_word & 0x0001) == 0):
23+
crc_word >>= 1
24+
25+
else:
26+
crc_word >>= 1
27+
crc_word ^= 0xA001
28+
29+
return crc_word
30+
31+
32+
def check_crc(self, value, s, e):
33+
hb = value[e]
34+
lb = value[s]
35+
crc = hb
36+
crc <<= 8
37+
crc |= lb
38+
39+
return crc
40+
41+
42+
def MODBUS_RX(self):
43+
if(self.uart.any() > 0x00):
44+
self.rx_data_frame = self.uart.read(9)
45+
46+
47+
def MODBUS_TX(self):
48+
crc = 0
49+
self.tx_data_frame[0x00] = 0x01
50+
self.tx_data_frame[0x01] = 0x03
51+
self.tx_data_frame[0x02] = 0x00
52+
self.tx_data_frame[0x03] = 0x00
53+
self.tx_data_frame[0x04] = 0x00
54+
self.tx_data_frame[0x05] = 0x02
55+
56+
crc = self.generate_CRC16(self.tx_data_frame, 6)
57+
58+
self.tx_data_frame[0x06] = (crc & 0x00FF)
59+
self.tx_data_frame[0x07] = ((crc & 0xFF00) >> 0x08)
60+
61+
self.uart.write(self.tx_data_frame)
62+
sleep_ms(100)
63+
64+
65+
def get_temp(self):
66+
crc_1 = 0x0000
67+
crc_2 = 0xFFFF
68+
temp = 1000
69+
70+
self.MODBUS_TX()
71+
self.MODBUS_RX()
72+
73+
if(self.rx_data_frame[0x00] == 0x01):
74+
if(self.rx_data_frame[0x01] == 0x03):
75+
crc_1 = self.generate_CRC16(self.rx_data_frame, 7)
76+
crc_2 = self.check_crc(self.rx_data_frame, 7, 8)
77+
78+
if(crc_1 == crc_2):
79+
temp = self.rx_data_frame[3]
80+
temp <<= 0x08
81+
temp |= self.rx_data_frame[4]
82+
temp /= 10.0
83+
84+
if((temp >= -30) and (temp <= 110)):
85+
return temp
86+
else:
87+
return -1000
88+
89+
return 1000
90+

0 commit comments

Comments
 (0)