Skip to content

Commit 2d41cc1

Browse files
committed
hd44780: configure and write fixes
- Write only the high-part of the byte when configuring in 4-bit mode similar to the LiquidCrystal library around [here](https://github.com/arduino-libraries/LiquidCrystal/blob/1.0.7/src/LiquidCrystal.cpp#L116) - Pulse enable after writing data similar to the LiquidCrystal library: [4-bits here](https://github.com/arduino-libraries/LiquidCrystal/blob/1.0.7/src/LiquidCrystal.cpp#L312), [8-bits here](https://github.com/arduino-libraries/LiquidCrystal/blob/1.0.7/src/LiquidCrystal.cpp#L320) Addresses issue [#646](#646).
1 parent 3c5e174 commit 2d41cc1

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

hd44780/gpio.go

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,26 @@ package hd44780
22

33
import (
44
"errors"
5+
"time"
56

67
"machine"
78
)
89

10+
type WriteByteType int
11+
12+
const (
13+
FullByte WriteByteType = 3
14+
HighNibble WriteByteType = 2
15+
LowNibble WriteByteType = 1
16+
)
17+
918
type GPIO struct {
1019
dataPins []machine.Pin
1120
en machine.Pin
1221
rw machine.Pin
1322
rs machine.Pin
1423

15-
write func(data byte)
24+
write func(data byte, wt WriteByteType)
1625
read func() byte
1726
}
1827

@@ -48,6 +57,10 @@ func newGPIO(dataPins []machine.Pin, en, rs, rw machine.Pin, mode byte) Device {
4857
}
4958
}
5059

60+
func (g *GPIO) WriteHighNibble(data byte) {
61+
g.write(data, HighNibble)
62+
}
63+
5164
// SetCommandMode sets command/instruction mode
5265
func (g *GPIO) SetCommandMode(set bool) {
5366
if set {
@@ -68,26 +81,36 @@ func (g *GPIO) Write(data []byte) (n int, err error) {
6881
g.rw.Low()
6982
}
7083
for _, d := range data {
71-
g.write(d)
84+
g.write(d, FullByte)
7285
n++
7386
}
7487
return n, nil
7588
}
7689

77-
func (g *GPIO) write8BitMode(data byte) {
78-
g.en.High()
90+
func (g *GPIO) write8BitMode(data byte, _ WriteByteType) {
7991
g.setPins(data)
80-
g.en.Low()
92+
g.pulseEnable()
8193
}
8294

83-
func (g *GPIO) write4BitMode(data byte) {
84-
g.en.High()
85-
g.setPins(data >> 4)
86-
g.en.Low()
95+
func (g *GPIO) write4BitMode(data byte, wt WriteByteType) {
96+
if wt&HighNibble != 0 {
97+
g.setPins(data >> 4)
98+
g.pulseEnable()
99+
}
87100

101+
if wt&LowNibble != 0 {
102+
g.setPins(data)
103+
g.pulseEnable()
104+
}
105+
}
106+
107+
func (g *GPIO) pulseEnable() {
108+
g.en.Low()
109+
time.Sleep(time.Microsecond)
88110
g.en.High()
89-
g.setPins(data)
111+
time.Sleep(time.Microsecond)
90112
g.en.Low()
113+
time.Sleep(100 * time.Microsecond)
91114
}
92115

93116
// Read reads len(data) bytes from display RAM to data starting from RAM address counter position

hd44780/hd44780.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const (
2525

2626
type Buser interface {
2727
io.ReadWriter
28+
WriteHighNibble(data byte)
2829
SetCommandMode(set bool)
2930
WriteOnly() bool
3031
}
@@ -115,19 +116,17 @@ func (d *Device) Configure(cfg Config) error {
115116
time.Sleep(15 * time.Millisecond)
116117

117118
d.bus.SetCommandMode(true)
118-
d.bus.Write([]byte{DATA_LENGTH_8BIT})
119+
d.bus.WriteHighNibble(DATA_LENGTH_8BIT)
119120
time.Sleep(5 * time.Millisecond)
120121

121122
for i := 0; i < 2; i++ {
122-
d.bus.Write([]byte{DATA_LENGTH_8BIT})
123+
d.bus.WriteHighNibble(DATA_LENGTH_8BIT)
123124
time.Sleep(150 * time.Microsecond)
124-
125125
}
126126

127127
if d.datalength == DATA_LENGTH_4BIT {
128-
d.bus.Write([]byte{DATA_LENGTH_4BIT})
128+
d.bus.WriteHighNibble(DATA_LENGTH_4BIT)
129129
}
130-
131130
// Busy flag is now accessible
132131
d.SendCommand(memoryMap | cfg.Font | d.datalength)
133132
d.SendCommand(DISPLAY_OFF)

0 commit comments

Comments
 (0)