Skip to content

Instantly share code, notes, and snippets.

@c0deaddict
Created November 13, 2015 21:22
Show Gist options
  • Save c0deaddict/9a81624af40be6c8e0e8 to your computer and use it in GitHub Desktop.
Save c0deaddict/9a81624af40be6c8e0e8 to your computer and use it in GitHub Desktop.
NodeMCU HD44780
local bor, band, bnot = bit.bor, bit.band, bit.bnot
local function write4bits(self, bits, char_mode)
tmr.delay(1000)
gpio.write(self.pin_rs, char_mode and gpio.HIGH or gpio.LOW)
for n = 1, 2 do
for i, pin in ipairs(self.pin_db) do
local j = (2-n)*4 + (i-1)
local val = (bit.isset(bits, j))
gpio.write(pin, val and gpio.HIGH or gpio.LOW)
end
gpio.write(self.pin_e, gpio.LOW)
tmr.delay(1000)
gpio.write(self.pin_e, gpio.HIGH)
tmr.delay(1000)
gpio.write(self.pin_e, gpio.LOW)
tmr.delay(37000)
end
end
local function home(self)
write4bits(self, 0x02)
tmr.delay(3000000)
end
local function clear(self)
write4bits(self, 0x01)
tmr.delay(3000000)
end
local function message(self, text)
tmptext = text:gsub("\n", string.char(0xC0))
for i = 1, #tmptext do
local c = tmptext:byte(i)
write4bits(self, c, (c ~= 0xC0))
end
end
function init(pin_rs, pin_e, pin_db)
local self = {
pin_rs = pin_rs,
pin_e = pin_e,
pin_db = pin_db
}
gpio.mode(self.pin_rs, gpio.OUTPUT)
gpio.mode(self.pin_e, gpio.OUTPUT)
for _, pin in ipairs(self.pin_db) do
gpio.mode(pin, gpio.OUTPUT)
end
self.begin = begin
self.home = home
self.clear = clear
self.message = message
self.write4bits = write4bits
self:write4bits(0x33) -- initialization
write4bits(self, 0x32) -- initialization
write4bits(self, 0x28) -- 2 line 5x7 matrix
write4bits(self, 0x0C) -- turn cursor off 0x0E to enable cursor
write4bits(self, 0x06) -- shift cursor right
self:clear()
return self
end
m = init(1, 2, {3, 4, 5, 6})
m:message("Hello World\nFrom ESP8266")
@c0deaddict
Copy link
Author

Translated https://github.com/Tieske/rpi-gpio/blob/master/lua/module/lcd-hd44780.lua from the Raspberry Pi to ESP8266 NodeMCU. Left most of the functions out because they do not fit into memory...

@Lemon91
Copy link

Lemon91 commented Aug 11, 2016

Why so mutch delay? Example Clear and Home needs only 1.52ms not 3s!

@milkpirate
Copy link

milkpirate commented Oct 18, 2016

Nice work! But could you explain line 36 a bit? Would'nt write4bits(self, c, 1) suffice?

EDIT: Ahh got it. Forces the line break if neccessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment