forked from makecode-extensions/i2cLCD1602
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCD1602_I2C.ts
313 lines (279 loc) · 8.06 KB
/
LCD1602_I2C.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**
* makecode I2C LCD1602 package for microbit.
* From microbit/micropython Chinese community.
* http://www.micropython.org.cn
*/
/**
* Custom blocks
*/
//% weight=20 color=#0fbc11 icon="▀"
namespace I2C_LCD1602 {
// ************************************* START
let LCD_CLEARDISPLAY = 0x01
let LCD_RETURNHOME = 0x02
let LCD_ENTRYMODESET = 0x04
let LCD_DISPLAYCONTROL = 0x08
let LCD_CURSORSHIFT = 0x10
let LCD_FUNCTIONSET = 0x20
let LCD_SETCGRAMADDR = 0x40
let LCD_SETDDRAMADDR = 0x80
// Entry flags
let LCD_ENTRYRIGHT = 0x00
let LCD_ENTRYLEFT = 0x02
let LCD_ENTRYSHIFTINCREMENT = 0x01
let LCD_ENTRYSHIFTDECREMENT = 0x00
// Control flags
let LCD_DISPLAYON = 0x04
let LCD_DISPLAYOFF = 0x00
let LCD_CURSORON = 0x02
let LCD_CURSOROFF = 0x00
let LCD_BLINKON = 0x01
let LCD_BLINKOFF = 0x00
// Move flags
let LCD_DISPLAYMOVE = 0x08
let LCD_CURSORMOVE = 0x00
let LCD_MOVERIGHT = 0x04
let LCD_MOVELEFT = 0x00
// Function set flags
let LCD_8BITMODE = 0x10
let LCD_4BITMODE = 0x00
let LCD_2LINE = 0x08
let LCD_1LINE = 0x00
let LCD_5x10DOTS = 0x04
let LCD_5x8DOTS = 0x00
let rs : DigitalPin
let en : DigitalPin
let d7 : DigitalPin
let d6 : DigitalPin
let d5 : DigitalPin
let d4 : DigitalPin
let backlight : DigitalPin
// ***************************************************END
let i2cAddr: number // 0x3F: PCF8574A, 0x27: PCF8574
let BK: number // backlight control
let RS: number // command/data
// set LCD reg
function setreg(d: number) {
pins.i2cWriteNumber(i2cAddr, d, NumberFormat.Int8LE)
basic.pause(1)
}
// send data to I2C bus
function set(d: number) {
d = d & 0xF0
d = d + BK + RS
setreg(d)
setreg(d + 4)
setreg(d)
}
// send command
function cmd(d: number) {
RS = 0
set(d)
set(d << 4)
}
// send data
function dat(d: number) {
RS = 1
set(d)
set(d << 4)
}
// auto get LCD address
function AutoAddr() {
let k = true
let addr = 0x20
let d1 = 0, d2 = 0
while (k && (addr < 0x28)) {
pins.i2cWriteNumber(addr, -1, NumberFormat.Int32LE)
d1 = pins.i2cReadNumber(addr, NumberFormat.Int8LE) % 16
pins.i2cWriteNumber(addr, 0, NumberFormat.Int16LE)
d2 = pins.i2cReadNumber(addr, NumberFormat.Int8LE)
if ((d1 == 7) && (d2 == 0)) k = false
else addr += 1
}
if (!k) return addr
addr = 0x38
while (k && (addr < 0x40)) {
pins.i2cWriteNumber(addr, -1, NumberFormat.Int32LE)
d1 = pins.i2cReadNumber(addr, NumberFormat.Int8LE) % 16
pins.i2cWriteNumber(addr, 0, NumberFormat.Int16LE)
d2 = pins.i2cReadNumber(addr, NumberFormat.Int8LE)
if ((d1 == 7) && (d2 == 0)) k = false
else addr += 1
}
if (!k) return addr
else return 0
}
/**
* initial LCD, set I2C address. Address is 39/63 for PCF8574/PCF8574A
* @param Addr is i2c address for LCD, eg: 0, 39, 63. 0 is auto find address
*/
//% blockId="I2C_LCD1620_SET_ADDRESS" block="LCD initialize with Address %addr"
//% weight=100 blockGap=8
//% parts=LCD1602_I2C trackArgs=0
export function LcdInit(Addr: number) {
if (Addr == 0) i2cAddr = AutoAddr()
else i2cAddr = Addr
BK = 8
RS = 0
cmd(0x33) // set 4bit mode
basic.pause(5)
set(0x30)
basic.pause(5)
set(0x20)
basic.pause(5)
cmd(0x28) // set mode
cmd(0x0C)
cmd(0x06)
cmd(0x01) // clear
//HAK1
basic.pause(10)
CreateCustomChar()
basic.pause(10)
CreateCustomChar2()
}
/**
* Custom Char
*/
//% blockId="I2C_LCD1620_CUSTOM_CHR" block="Custom Char"
//% weight=60 blockGap=8
//% parts=LCD1602_I2C trackArgs=0
export function CustomChar(): void {//display characters
cmd(LCD_CURSORON)
basic.pause(5)
cmd(0x86)
basic.pause(5)
dat(0x00)
cmd(0x88)
basic.pause(5)
dat(0x01)
}
//Create Custom Char At initialization خ
function CreateCustomChar() {
let bChar: number[] = [0x04, 0x00, 0x1E, 0x03, 0x0E, 0x10, 0x10, 0x0F]
let location: number
// we only have 8 locations 0-7
location = 0
location &= 0x7
cmd(LCD_SETCGRAMADDR | (location << 3))
//cmd(LCD_SETCGRAMADDR)
for (let i = 0; i < 8; i++) {
dat(bChar[i])
}
}
//Create Custom Char At initialization ع
function CreateCustomChar2() {
let bChar: number[] = [0x0E, 0x08, 0x0F, 0x08, 0x10, 0x10, 0x10, 0x0F]
let location: number
// we only have 8 locations 0-7
location = 1
location &= 0x7
cmd(LCD_SETCGRAMADDR | (location << 3))
//cmd(LCD_SETCGRAMADDR)
for (let i = 0; i < 8; i++) {
dat(bChar[i])
}
}
/**
* show a number in LCD at given position
* @param n is number will be show, eg: 10, 100, 200
* @param x is LCD column position, eg: 0
* @param y is LCD row position, eg: 0
*/
//% blockId="I2C_LCD1620_SHOW_NUMBER" block="show number %n|at x %x|y %y"
//% weight=90 blockGap=8
//% x.min=0 x.max=15
//% y.min=0 y.max=1
//% parts=LCD1602_I2C trackArgs=0
export function ShowNumber(n: number, x: number, y: number): void {
let s = n.toString()
ShowString(s, x, y)
}
/**
* show a string in LCD at given position
* @param s is string will be show, eg: "Hello"
* @param x is LCD column position, [0 - 15], eg: 0
* @param y is LCD row position, [0 - 1], eg: 0
*/
//% blockId="I2C_LCD1620_SHOW_STRING" block="show string %s|at x %x|y %y"
//% weight=90 blockGap=8
//% x.min=0 x.max=15
//% y.min=0 y.max=1
//% parts=LCD1602_I2C trackArgs=0
export function ShowString(s: string, x: number, y: number): void {
let a: number
if (y > 0)
a = 0xC0
else
a = 0x80
a += x
cmd(a)
for (let i = 0; i < s.length; i++) {
dat(s.charCodeAt(i))
}
}
/**
* turn on LCD
*/
//% blockId="I2C_LCD1620_ON" block="turn on LCD"
//% weight=81 blockGap=8
//% parts=LCD1602_I2C trackArgs=0
export function on(): void {
cmd(0x0C)
}
/**
* turn off LCD
*/
//% blockId="I2C_LCD1620_OFF" block="turn off LCD"
//% weight=80 blockGap=8
//% parts=LCD1602_I2C trackArgs=0
export function off(): void {
cmd(0x08)
}
/**
* clear all display content
*/
//% blockId="I2C_LCD1620_CLEAR" block="clear LCD"
//% weight=85 blockGap=8
//% parts=LCD1602_I2C trackArgs=0
export function clear(): void {
cmd(0x01)
}
/**
* turn on LCD backlight
*/
//% blockId="I2C_LCD1620_BACKLIGHT_ON" block="turn on backlight"
//% weight=71 blockGap=8
//% parts=LCD1602_I2C trackArgs=0
export function BacklightOn(): void {
BK = 8
cmd(0)
}
/**
* turn off LCD backlight
*/
//% blockId="I2C_LCD1620_BACKLIGHT_OFF" block="turn off backlight"
//% weight=70 blockGap=8
//% parts=LCD1602_I2C trackArgs=0
export function BacklightOff(): void {
BK = 0
cmd(0)
}
/**
* shift left
*/
//% blockId="I2C_LCD1620_SHL" block="Shift Left"
//% weight=61 blockGap=8
//% parts=LCD1602_I2C trackArgs=0
export function shl(): void {
cmd(0x18)
}
/**
* shift right
*/
//% blockId="I2C_LCD1620_SHR" block="Shift Right"
//% weight=60 blockGap=8
//% parts=LCD1602_I2C trackArgs=0
export function shr(): void {
cmd(0x1C)
}
}