Skip to content

Commit

Permalink
hd44780: configure and write fixes
Browse files Browse the repository at this point in the history
- 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).
  • Loading branch information
sorf committed Jan 26, 2024
1 parent aa7bd47 commit 67f5ced
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
43 changes: 33 additions & 10 deletions hd44780/gpio.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@ package hd44780

import (
"errors"
"time"

"machine"
)

type WriteByteType int

const (
FullByte WriteByteType = 3
HighNibble WriteByteType = 2
LowNibble WriteByteType = 1
)

type GPIO struct {
dataPins []machine.Pin
en machine.Pin
rw machine.Pin
rs machine.Pin

write func(data byte)
write func(data byte, wt WriteByteType)
read func() byte
}

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

func (g *GPIO) WriteHighNibble(data byte) {
g.write(data, HighNibble)
}

// SetCommandMode sets command/instruction mode
func (g *GPIO) SetCommandMode(set bool) {
if set {
Expand All @@ -68,26 +81,36 @@ func (g *GPIO) Write(data []byte) (n int, err error) {
g.rw.Low()
}
for _, d := range data {
g.write(d)
g.write(d, FullByte)
n++
}
return n, nil
}

func (g *GPIO) write8BitMode(data byte) {
g.en.High()
func (g *GPIO) write8BitMode(data byte, _ WriteByteType) {
g.setPins(data)
g.en.Low()
g.pulseEnable()
}

func (g *GPIO) write4BitMode(data byte) {
g.en.High()
g.setPins(data >> 4)
g.en.Low()
func (g *GPIO) write4BitMode(data byte, wt WriteByteType) {
if wt&HighNibble != 0 {
g.setPins(data >> 4)
g.pulseEnable()
}

if wt&LowNibble != 0 {
g.setPins(data)
g.pulseEnable()
}
}

func (g *GPIO) pulseEnable() {
g.en.Low()
time.Sleep(time.Microsecond)
g.en.High()
g.setPins(data)
time.Sleep(time.Microsecond)
g.en.Low()
time.Sleep(100 * time.Microsecond)
}

// Read reads len(data) bytes from display RAM to data starting from RAM address counter position
Expand Down
9 changes: 4 additions & 5 deletions hd44780/hd44780.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (

type Buser interface {
io.ReadWriter
WriteHighNibble(data byte)
SetCommandMode(set bool)
WriteOnly() bool
}
Expand Down Expand Up @@ -115,19 +116,17 @@ func (d *Device) Configure(cfg Config) error {
time.Sleep(15 * time.Millisecond)

d.bus.SetCommandMode(true)
d.bus.Write([]byte{DATA_LENGTH_8BIT})
d.bus.WriteHighNibble(DATA_LENGTH_8BIT)
time.Sleep(5 * time.Millisecond)

for i := 0; i < 2; i++ {
d.bus.Write([]byte{DATA_LENGTH_8BIT})
d.bus.WriteHighNibble(DATA_LENGTH_8BIT)
time.Sleep(150 * time.Microsecond)

}

if d.datalength == DATA_LENGTH_4BIT {
d.bus.Write([]byte{DATA_LENGTH_4BIT})
d.bus.WriteHighNibble(DATA_LENGTH_4BIT)
}

// Busy flag is now accessible
d.SendCommand(memoryMap | cfg.Font | d.datalength)
d.SendCommand(DISPLAY_OFF)
Expand Down

0 comments on commit 67f5ced

Please sign in to comment.