Skip to content

Commit e50ed68

Browse files
committed
add auto address feature
when set address to 0 in LcdInit(), it will try to get correctly address automitic.
1 parent 58820c8 commit e50ed68

4 files changed

Lines changed: 41 additions & 10 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ to search box then search.
2020
## I2C Address
2121
- PCF8574: 39
2222
- PCF8574A: 63
23+
- Auto: 0
2324

2425
## API
2526

2627
- LcdInit(Addr: number)
2728
Initial LCD
28-
Addr: I2C Address.
29+
Addr: I2C Address. If Addr is zero, it will automatic recognition correctly address.
2930

3031
- ShowNumber(n: number, x: number, y: number)
3132
show a number in LCD at given position.

main.ts

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
2-
* makecode I2C LCD1602 package for microbit.
3-
* From microbit/micropython Chinese community.
4-
* http://www.micropython.org.cn
5-
*/
2+
* makecode I2C LCD1602 package for microbit.
3+
* From microbit/micropython Chinese community.
4+
* http://www.micropython.org.cn
5+
*/
66

77
/**
88
* Custom blocks
@@ -42,14 +42,44 @@ namespace I2C_LCD1602 {
4242
set(d << 4)
4343
}
4444

45+
// auto get LCD address
46+
function AutoAddr() {
47+
let k = true
48+
let addr = 0x20
49+
let d1 = 0, d2 = 0
50+
while (k && (addr < 0x28)) {
51+
pins.i2cWriteNumber(addr, 0xffffffff, NumberFormat.Int32LE)
52+
d1 = pins.i2cReadNumber(addr, NumberFormat.Int8LE) % 16
53+
pins.i2cWriteNumber(addr, 0, NumberFormat.Int16LE)
54+
d2 = pins.i2cReadNumber(addr, NumberFormat.Int8LE)
55+
if ((d1 == 7) && (d2 == 0)) k = false
56+
else addr += 1
57+
}
58+
if (!k) return addr
59+
60+
addr = 0x38
61+
while (k && (addr < 0x40)) {
62+
pins.i2cWriteNumber(addr, 0xffffffff, NumberFormat.Int32LE)
63+
d1 = pins.i2cReadNumber(addr, NumberFormat.Int8LE) % 16
64+
pins.i2cWriteNumber(addr, 0, NumberFormat.Int16LE)
65+
d2 = pins.i2cReadNumber(addr, NumberFormat.Int8LE)
66+
if ((d1 == 7) && (d2 == 0)) k = false
67+
else addr += 1
68+
}
69+
if (!k) return addr
70+
else return 0
71+
72+
}
73+
4574
/**
4675
* initial LCD, set I2C address. Address is 39/63 for PCF8574/PCF8574A
47-
* @param Addr is i2c address for LCD, eg: 39, 63
76+
* @param Addr is i2c address for LCD, eg: 0, 39, 63. 0 is auto find address
4877
*/
4978
//% blockId="I2C_LCD1620_SET_ADDRESS" block="LCD initialize with Address %addr"
5079
//% weight=100 blockGap=8
5180
export function LcdInit(Addr: number) {
52-
i2cAddr = Addr
81+
if (Addr == 0) i2cAddr = AutoAddr()
82+
else i2cAddr = Addr
5383
BK = 8
5484
RS = 0
5585
cmd(0x33) // set 4bit mode
@@ -168,4 +198,4 @@ namespace I2C_LCD1602 {
168198
export function shr(): void {
169199
cmd(0x1C)
170200
}
171-
}
201+
}

pxt.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "i2cLCD1602",
3-
"version": "1.3.0",
3+
"version": "1.5.0",
44
"description": "makecode I2C LCD1602 package for micro:bit",
55
"license": "MIT",
66
"dependencies": {

test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
let item = 0
2-
I2C_LCD1602.LcdInit(63)
2+
I2C_LCD1602.LcdInit(0)
33
I2C_LCD1602.ShowString("Hello", 0, 0)
44
basic.forever(() => {
55
item += 1

0 commit comments

Comments
 (0)