Skip to content

Commit dfb10ca

Browse files
authored
Add files via upload
1 parent 56c194d commit dfb10ca

File tree

5 files changed

+467
-0
lines changed

5 files changed

+467
-0
lines changed

GLCD SONAR/PCD8544.py

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
from micropython import const
2+
from pyb import Pin, SPI
3+
from utime import sleep_ms
4+
from ustruct import pack
5+
import framebuf
6+
7+
8+
PCD8544_DC_pin = 'X2'
9+
PCD8544_CS_pin = 'X3'
10+
PCD8544_RST_pin = 'X1'
11+
PCD8544_SCK_pin = 'X6'
12+
PCD8544_MOSI_pin = 'X8'
13+
14+
PCD8544_FUNCTION_SET = const(0x20)
15+
PCD8544_POWER_DOWN = const(0x04)
16+
PCD8544_ADDRESSING_VERT = const(0x02)
17+
PCD8544_EXTENDED_INSTR = const(0x01)
18+
19+
PCD8544_DISPLAY_BLANK = const(0x08)
20+
PCD8544_DISPLAY_ALL = const(0x09)
21+
PCD8544_DISPLAY_NORMAL = const(0x0C)
22+
PCD8544_DISPLAY_INVERSE = const(0x0D)
23+
24+
PCD8544_TEMP_COEFF_0 = const(0x04)
25+
PCD8544_TEMP_COEFF_1 = const(0x05)
26+
PCD8544_TEMP_COEFF_2 = const(0x06)
27+
PCD8544_TEMP_COEFF_3 = const(0x07)
28+
29+
PCD8544_BIAS_1_100 = const(0x10)
30+
PCD8544_BIAS_1_80 = const(0x11)
31+
PCD8544_BIAS_1_65 = const(0x12)
32+
PCD8544_BIAS_1_48 = const(0x13)
33+
PCD8544_BIAS_1_40 = const(0x14)
34+
PCD8544_BIAS_1_24 = const(0x15)
35+
PCD8544_BIAS_1_18 = const(0x16)
36+
PCD8544_BIAS_1_10 = const(0x17)
37+
PCD8544_SET_VOP = const(0x80)
38+
PCD8544_COL_ADDR = const(0x80)
39+
PCD8544_BANK_ADDR = const(0x40)
40+
41+
CMD = False
42+
DAT = True
43+
44+
LOW = False
45+
HIGH = True
46+
47+
PCD8544_GLCD_WIDTH = const(84)
48+
PCD8544_GLCD_HEIGHT = const(48)
49+
50+
51+
class PCD8544(framebuf.FrameBuffer):
52+
def __init__(self):
53+
self.width = PCD8544_GLCD_WIDTH
54+
self.height = PCD8544_GLCD_HEIGHT
55+
56+
self.BLACK = const(0xFF)
57+
self.WHITE = const(0x00)
58+
59+
self.PCD8544_CS = Pin(PCD8544_CS_pin, Pin.OUT_PP)
60+
self.PCD8544_RST = Pin(PCD8544_RST_pin, Pin.OUT_PP)
61+
self.PCD8544_SCK = Pin(PCD8544_SCK_pin, Pin.OUT_PP)
62+
self.PCD8544_MOSI = Pin(PCD8544_MOSI_pin, Pin.OUT_PP)
63+
64+
self.PCD8544_SPI = SPI(1, SPI.CONTROLLER, baudrate = 4_000_000, polarity = 0, phase = 0)
65+
66+
self.PCD8544_DC = Pin(PCD8544_DC_pin, Pin.OUT_PP)
67+
68+
self.buffer = bytearray((self.height * self.width) // 8) #84 x 48 = 504
69+
super().__init__(self.buffer, self.width, self.height, framebuf.MONO_VLSB)
70+
71+
self.init()
72+
73+
74+
75+
def init(self):
76+
self.function = PCD8544_FUNCTION_SET
77+
self.disp_reset()
78+
self.addressing(True)
79+
self.set_contrast(PCD8544_BIAS_1_40, PCD8544_TEMP_COEFF_2, 63)
80+
self.disp_invert(False)
81+
self.clear()
82+
83+
84+
def disp_reset(self):
85+
self.PCD8544_CS.value(HIGH)
86+
self.PCD8544_DC.value(HIGH)
87+
88+
self.PCD8544_RST.value(HIGH)
89+
sleep_ms(10)
90+
self.PCD8544_RST.value(LOW)
91+
sleep_ms(20)
92+
self.PCD8544_RST.value(HIGH)
93+
94+
95+
def send(self, value, mode):
96+
self.PCD8544_CS.value(LOW)
97+
self.PCD8544_DC.value(mode)
98+
99+
if(mode == DAT):
100+
self.PCD8544_SPI.write(pack('B'*len(value), *value))
101+
else:
102+
self.PCD8544_SPI.write(bytearray([value]))
103+
104+
self.PCD8544_CS.value(HIGH)
105+
106+
107+
def set_xy(self, x_pos, y_pos):
108+
self.send((PCD8544_COL_ADDR | x_pos), CMD)
109+
self.send((PCD8544_BANK_ADDR | y_pos), CMD)
110+
111+
112+
def addressing(self, horizontal_or_vertical = True):
113+
if(horizontal_or_vertical == True):
114+
self.function &= ~PCD8544_ADDRESSING_VERT
115+
else:
116+
self.function |= PCD8544_ADDRESSING_VERT
117+
118+
self.send(self.function, CMD)
119+
120+
121+
def clear(self):
122+
self.send(([0] * 504), DAT) #84 x 48 = 504
123+
self.set_xy(0, 0)
124+
125+
126+
def disp_invert(self, mode):
127+
if(mode == True):
128+
self.send(PCD8544_DISPLAY_INVERSE, CMD)
129+
else:
130+
self.send(PCD8544_DISPLAY_NORMAL, CMD)
131+
132+
133+
def disp_power(self, mode):
134+
if(mode == True):
135+
self.function &= ~PCD8544_POWER_DOWN
136+
else:
137+
self.function |= PCD8544_POWER_DOWN
138+
139+
self.send(self.function, CMD)
140+
141+
142+
def set_contrast(self, bias = PCD8544_BIAS_1_40, t_coff = PCD8544_TEMP_COEFF_2, contrast = 63):
143+
self.send((self.function | PCD8544_EXTENDED_INSTR), CMD)
144+
self.send(t_coff, CMD)
145+
self.send(bias, CMD)
146+
self.send(PCD8544_SET_VOP | contrast, CMD)
147+
self.send((self.function & ~PCD8544_EXTENDED_INSTR), CMD)
148+
149+
150+
151+
def show(self):
152+
self.send(self.buffer, DAT)
153+

GLCD SONAR/main.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
from pyb import Pin, LED, Timer
2+
from utime import sleep_ms, sleep_us
3+
from PCD8544 import PCD8544
4+
5+
6+
last_capture = 0
7+
pulse_width = 0
8+
9+
10+
led = LED(1)
11+
12+
echo_pin = Pin('X4', Pin.IN)
13+
trigger_pin = Pin('X5', Pin.OUT_PP)
14+
15+
16+
glcd = PCD8544()
17+
18+
glcd.fill(glcd.WHITE)
19+
glcd.show()
20+
21+
22+
def input_capture(timer):
23+
global last_capture, pulse_width
24+
25+
if echo_pin.value():
26+
last_capture = in_cap.capture()
27+
28+
else:
29+
pulse_width = (in_cap.capture() - last_capture)
30+
pulse_width &= 0x0FFFFFFF
31+
32+
33+
TIM2 = Timer(2,
34+
prescaler = 83,
35+
period = 0x0FFFFFFF)
36+
37+
in_cap = TIM2.channel(4,
38+
mode = Timer.IC,
39+
pin = echo_pin,
40+
polarity = Timer.BOTH,
41+
callback = input_capture)
42+
43+
44+
def map_value(v, x_min, x_max, y_min, y_max):
45+
return int(y_min + (((y_max - y_min) / (x_max - x_min)) * (v - x_min)))
46+
47+
48+
def constrain(value, min_value, max_value):
49+
if(value > max_value):
50+
return max_value
51+
52+
elif(value < min_value):
53+
return min_value
54+
55+
else:
56+
return value
57+
58+
59+
def trigger_sensor():
60+
led.on()
61+
trigger_pin.high()
62+
sleep_us(20)
63+
trigger_pin.low()
64+
sleep_ms(300)
65+
66+
67+
def display():
68+
global pulse_width
69+
70+
distance = constrain(pulse_width, 0, 42000)
71+
distance = (pulse_width / 5.8)
72+
bar = map_value(distance, 0, 4000, 2, 80)
73+
74+
glcd.fill(glcd.WHITE)
75+
glcd.text("PYB SONAR", 4 , 2, glcd.BLACK)
76+
glcd.text("D/mm: " + str("%3u " %distance), 1 , 16, glcd.BLACK)
77+
78+
for i in range(2, 83, 10):
79+
glcd.vline(i, 42, 5, glcd.BLACK)
80+
glcd.vline((i - 5) , 42, 3, glcd.BLACK)
81+
82+
glcd.rect(1, 32, 83, 8, glcd.BLACK)
83+
glcd.rect(2, 34, bar, 4, glcd.BLACK, True)
84+
85+
glcd.show()
86+
pulse_width = 0
87+
led.off()
88+
sleep_ms(300)
89+
90+
91+
while(True):
92+
trigger_sensor()
93+
display()

RCWL-1670 SONAR/RGB_LCD.py

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
from micropython import const
2+
from utime import sleep_ms
3+
4+
5+
AiP31068_LCD_I2C_address = const(0x3E)
6+
PCA9633_I2C_address = const(0x60)
7+
8+
REG_RED = const(0x04)
9+
REG_GREEN = const(0x03)
10+
REG_BLUE = const(0x02)
11+
REG_MODE_1 = const(0x00)
12+
REG_MODE_2 = const(0x01)
13+
REG_OUTPUT = const(0x08)
14+
15+
LCD_CLEAR_DISPLAY = const(0x01)
16+
LCD_RETURN_HOME = const(0x02)
17+
LCD_ENTRY_MODE_SET = const(0x04)
18+
LCD_DISPLAY_CONTROL = const(0x08)
19+
LCD_CURSOR_SHIFT = const(0x10)
20+
LCD_FUNCTION_SET = const(0x20)
21+
LCD_SET_CGRAM_ADDR = const(0x40)
22+
LCD_SET_DDRAM_ADDR = const(0x80)
23+
24+
LCD_ENTRY_RIGHT = const(0x00)
25+
LCD_ENTRY_LEFT = const(0x02)
26+
LCD_ENTRY_SHIFT_INCREMENT = const(0x01)
27+
LCD_ENTRY_SHIFT_DECREMENT = const(0x00)
28+
29+
LCD_DISPLAY_ON = const(0x04)
30+
LCD_DISPLAY_OFF = const(0x00)
31+
LCD_CURSOR_ON = const(0x02)
32+
LCD_CURSOR_OFF = const(0x00)
33+
LCD_BLINK_ON = const(0x01)
34+
LCD_BLINK_OFF = const(0x00)
35+
36+
LCD_DISPLAY_MOVE = const(0x08)
37+
LCD_CURSOR_MOVE = const(0x00)
38+
LCD_MOVE_RIGHT = const(0x04)
39+
LCD_MOVE_LEFT = const(0x00)
40+
41+
LCD_8_BIT_MODE = const(0x10)
42+
LCD_4_BIT_MODE = const(0x00)
43+
LCD_2_LINE = const(0x08)
44+
LCD_1_LINE = const(0x00)
45+
LCD_5x8_DOTS = const(0x00)
46+
47+
DAT = const(0x40)
48+
CMD = const(0x80)
49+
50+
51+
class RGB_LCD():
52+
53+
def __init__(self, _i2c):
54+
self._row = 2
55+
self._col = 16
56+
self.i2c = _i2c
57+
self._showfunction = (LCD_4_BIT_MODE | LCD_1_LINE | LCD_5x8_DOTS)
58+
self.init(self._row, self._col)
59+
60+
61+
def write_to_LCD(self, value, loc):
62+
self.i2c.writeto_mem(AiP31068_LCD_I2C_address, loc, chr(value))
63+
64+
65+
def write_to_RGB_LED(self, reg, value):
66+
self.i2c.writeto_mem(PCA9633_I2C_address, reg, chr(value))
67+
68+
69+
def set_RGB(self, r, g, b):
70+
self.write_to_RGB_LED(REG_RED, r)
71+
self.write_to_RGB_LED(REG_GREEN, g)
72+
self.write_to_RGB_LED(REG_BLUE, b)
73+
74+
75+
def goto_xy(self, x_pos, y_pos):
76+
if(y_pos == 0):
77+
x_pos |= 0x80
78+
else:
79+
x_pos |= 0xC0
80+
81+
self.i2c.writeto(AiP31068_LCD_I2C_address, bytearray([0x80, x_pos]))
82+
83+
84+
def clear_home(self):
85+
self.write_to_LCD(LCD_CLEAR_DISPLAY, CMD)
86+
self.write_to_LCD(LCD_RETURN_HOME, CMD)
87+
sleep_ms(2)
88+
89+
90+
def display(self):
91+
self._showcontrol |= LCD_DISPLAY_ON
92+
self.write_to_LCD((LCD_DISPLAY_CONTROL | self._showcontrol), CMD)
93+
94+
95+
def put_chr(self, ch):
96+
self.write_to_LCD(ord(ch), DAT)
97+
98+
99+
def put_str(self, ch_str):
100+
for chr in ch_str:
101+
self.put_chr(chr)
102+
103+
104+
def init(self, cols, rows):
105+
if (rows > 1):
106+
self._showfunction |= LCD_2_LINE
107+
sleep_ms(50)
108+
self.write_to_LCD((LCD_FUNCTION_SET | self._showfunction), CMD)
109+
sleep_ms(5)
110+
self.write_to_LCD((LCD_FUNCTION_SET | self._showfunction), CMD)
111+
sleep_ms(5)
112+
self.write_to_LCD((LCD_FUNCTION_SET | self._showfunction), CMD)
113+
self.write_to_LCD((LCD_FUNCTION_SET | self._showfunction), CMD)
114+
self._showcontrol = (LCD_DISPLAY_ON | LCD_CURSOR_OFF | LCD_BLINK_OFF)
115+
self.display()
116+
self.clear_home()
117+
self._showmode = (LCD_ENTRY_LEFT | LCD_ENTRY_SHIFT_DECREMENT)
118+
self.write_to_LCD((LCD_ENTRY_MODE_SET | self._showmode), CMD)
119+
120+
self.write_to_RGB_LED(REG_MODE_1, 0x00)
121+
self.write_to_RGB_LED(REG_OUTPUT, 0xFF)
122+
self.write_to_RGB_LED(REG_MODE_2, 0x20)
123+
self.set_RGB(255, 255, 255)

0 commit comments

Comments
 (0)