Created
April 13, 2020 08:26
-
-
Save a-h/852c64d6aa2fa6590e9632f2bad6002a to your computer and use it in GitHub Desktop.
Raspberry Pi LCD without i2c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"time" | |
"github.com/stianeikeland/go-rpio" | |
) | |
const InstructionClearDisplay = 0b00000001 | |
const InstructionReturnHome = 0b00000010 | |
const InstructionFunctionSet = 0b00100000 | |
const InstructionFunctionSetDataLength8Bit = 0b00010000 | |
const InstructionFunctionSetOneLine = 0b00001000 | |
const InstructionFunctionSetTwoLine = 0b00000000 | |
const InstructionFunctionSet5By11 = 0b00000000 | |
const InstructionFunctionSet5By8 = 0b00000001 | |
const InstructionDisplayControl = 0b00001000 | |
const InstructionDisplayControlOn = 0b00000100 | |
const InstructionDisplayControlOff = 0b00000000 | |
const InstructionDisplayControlCursorOn = 0b00000010 | |
const InstructionDisplayControlCursorOff = 0b00000000 | |
const InstructionDisplayControlCursorBlinkOn = 0b00000001 | |
const InstructionDisplayControlCursorBlinkOff = 0b00000000 | |
const InstructionCursorOrDisplayShift = 0b00010000 | |
const InstructionCursorOrDisplayShiftCursor = 0b00001000 | |
var characters = map[rune]uint8{ | |
' ': 0b00000000, | |
'A': 0b01000001, | |
'B': 0b01000010, | |
'C': 0b01000011, | |
'D': 0b01000100, | |
'E': 0b01000101, | |
'F': 0b01000110, | |
'G': 0b01000111, | |
'H': 0b01001000, | |
'I': 0b01001001, | |
'J': 0b01001010, | |
'K': 0b01001011, | |
'L': 0b01001100, | |
'M': 0b01001101, | |
'N': 0b01001110, | |
'O': 0b01001111, | |
'P': 0b01010000, | |
'Q': 0b01010001, | |
'R': 0b01010010, | |
'S': 0b01010011, | |
'T': 0b01010100, | |
'U': 0b01010101, | |
'V': 0b01010110, | |
'W': 0b01010111, | |
'X': 0b01011000, | |
'Y': 0b01011001, | |
'Z': 0b01011010, | |
} | |
func main() { | |
err := rpio.Open() | |
if err != nil { | |
fmt.Printf("error opening up GPIO: %v\n", err) | |
os.Exit(1) | |
} | |
registerSelect := rpio.Pin(17) | |
registerSelect.Output() | |
enable := rpio.Pin(27) | |
enable.Output() | |
enable.Low() | |
bit0 := rpio.Pin(22) | |
bit0.Output() | |
bit1 := rpio.Pin(10) | |
bit1.Output() | |
bit2 := rpio.Pin(9) | |
bit2.Output() | |
bit3 := rpio.Pin(11) | |
bit3.Output() | |
bit4 := rpio.Pin(23) | |
bit4.Output() | |
bit5 := rpio.Pin(24) | |
bit5.Output() | |
bit6 := rpio.Pin(25) | |
bit6.Output() | |
bit7 := rpio.Pin(8) | |
bit7.Output() | |
write := func(instruction bool, data uint8) { | |
highLow(!instruction, registerSelect) | |
writeBit(data, 1, bit0) | |
writeBit(data, 2, bit1) | |
writeBit(data, 4, bit2) | |
writeBit(data, 8, bit3) | |
writeBit(data, 16, bit4) | |
writeBit(data, 32, bit5) | |
writeBit(data, 64, bit6) | |
writeBit(data, 128, bit7) | |
enable.High() | |
time.Sleep(100 * time.Microsecond) | |
enable.Low() | |
} | |
// Initialise as per Hitachi data sheet. | |
time.Sleep(100 * time.Millisecond) | |
write(true, InstructionFunctionSet | InstructionFunctionSetDataLength8Bit) | |
time.Sleep(5 * time.Millisecond) | |
write(true, InstructionFunctionSet | InstructionFunctionSetDataLength8Bit) | |
time.Sleep(1 * time.Millisecond) | |
write(true, InstructionFunctionSet | InstructionFunctionSetDataLength8Bit) | |
time.Sleep(1 * time.Millisecond) | |
write(true, InstructionFunctionSet | InstructionFunctionSetDataLength8Bit | InstructionFunctionSetTwoLine | InstructionFunctionSet5By8) | |
time.Sleep(1 * time.Millisecond) | |
write(true, InstructionDisplayControl|InstructionDisplayControlOn) | |
time.Sleep(1 * time.Millisecond) | |
write(true, InstructionClearDisplay) | |
time.Sleep(1 * time.Millisecond) | |
writeText := func(text string) { | |
for _, c := range text { | |
if data, ok := characters[c]; ok { | |
write(false, data) | |
} | |
} | |
} | |
writeText("ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ") | |
} | |
func writeBit(data, bit uint8, pin rpio.Pin) { | |
highLow((data&bit) == bit, pin) | |
} | |
func highLow(v bool, pin rpio.Pin) { | |
if v { | |
pin.High() | |
} else { | |
pin.Low() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment