-
Notifications
You must be signed in to change notification settings - Fork 4
/
LCD1602_I2C.ts
211 lines (192 loc) · 5.48 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
/**
* makecode I2C LCD1602 package for microbit.
* From microbit/micropython Chinese community.
* http://www.micropython.org.cn
*/
/**
* I2C LCD1602 液晶软件包
*/
//% weight=20 color=#0fbc11 icon="▀"
namespace I2C_LCD1602 {
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)
}
// 自动识别I2C地址
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
}
/**
* 初始化 LCD, 设置 I2C 地址。根据芯片不同地址有两种,PCF8574 是 39,PCF8574A 是 63
* 地址设置为0 代表自动识别
* @param address is i2c address for LCD, eg: 0, 39, 63
*/
//% blockId="I2C_LCD1620_SET_ADDRESS" block="初始化液晶,I2C 地址 %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
}
/**
* 在液晶的指定位置显示数字
* @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="显示数字 %n|位置 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)
}
/**
* 在液晶的指定位置显示字符串
* @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="显示字符串 %s|位置 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))
}
}
/**
* 打开液晶显示
*/
//% blockId="I2C_LCD1620_ON" block="打开液晶"
//% weight=81 blockGap=8
//% parts=LCD1602_I2C trackArgs=0
export function on(): void {
cmd(0x0C)
}
/**
* 关闭液晶显示
*/
//% blockId="I2C_LCD1620_OFF" block="关闭液晶"
//% weight=80 blockGap=8
//% parts=LCD1602_I2C trackArgs=0
export function off(): void {
cmd(0x08)
}
/**
* 清除液晶上显示的内容
*/
//% blockId="I2C_LCD1620_CLEAR" block="清除液晶显示内容"
//% weight=85 blockGap=8
//% parts=LCD1602_I2C trackArgs=0
export function clear(): void {
cmd(0x01)
}
/**
* 打开液晶的背光
*/
//% blockId="I2C_LCD1620_BACKLIGHT_ON" block="打开液晶背光"
//% weight=71 blockGap=8
//% parts=LCD1602_I2C trackArgs=0
export function BacklightOn(): void {
BK = 8
cmd(0)
}
/**
* 关闭液晶的背光
*/
//% blockId="I2C_LCD1620_BACKLIGHT_OFF" block="关闭液晶背光"
//% weight=70 blockGap=8
//% parts=LCD1602_I2C trackArgs=0
export function BacklightOff(): void {
BK = 0
cmd(0)
}
/**
* 屏幕向左移动
*/
//% blockId="I2C_LCD1620_SHL" block="屏幕左移"
//% weight=61 blockGap=8
//% parts=LCD1602_I2C trackArgs=0
export function shl(): void {
cmd(0x18)
}
/**
* 屏幕向右移动
*/
//% blockId="I2C_LCD1620_SHR" block="屏幕右移"
//% weight=60 blockGap=8
//% parts=LCD1602_I2C trackArgs=0
export function shr(): void {
cmd(0x1C)
}
}