-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlcd1602.lua
209 lines (172 loc) · 4.69 KB
/
lcd1602.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
local lcd = {}
local _address
local _backlight
local _displayfunction
local _displaycontrol
local _displaymode
local LCD_8BITMODE = 0x10
local LCD_4BITMODE = 0x00
local LCD_2LINE = 0x08
local LCD_1LINE = 0x00
local LCD_5x10DOTS = 0x04
local LCD_5x8DOTS = 0x00
local LCD_CLEARDISPLAY = 0x01
local LCD_RETURNHOME = 0x02
local LCD_ENTRYMODESET = 0x04
local LCD_DISPLAYCONTROL = 0x08
local LCD_SETDDRAMADDR = 0x80
local LCD_ENTRYRIGHT = 0x00
local LCD_ENTRYLEFT = 0x02
local LCD_ENTRYSHIFTINCREMENT = 0x01
local LCD_ENTRYSHIFTDECREMENT = 0x00
local LCD_DISPLAYON = 0x04
local LCD_CURSORON = 0x02
local LCD_BLINKON = 0x01
local LCD_BACKLIGHT = 0x08
local LCD_NOBACKLIGHT = 0x00
--- Writes the given data to the I2C bus
-- @param data the data
--
local function writeData(data)
i2c.start(0)
i2c.address(0, _address, i2c.TRANSMITTER)
i2c.write(0, bit.bor(data, _backlight))
i2c.stop(0)
end
--- Writes the given data and the enable pulse to the display
-- @param data the data to write
--
local function writeEnablePulse(data)
writeData(bit.bor(data, 0x04))
tmr.delay(1)
writeData(bit.band(data, bit.bnot(0x04)))
tmr.delay(50)
end
--- Writes 4 bits to the display and sends the enable pulse
-- @param value the data to write
--
local function write4Bits(value)
writeData(value)
writeEnablePulse(value)
end
--- Sends the given data to the display
-- @param value the data
-- @param mode the mode. May be 0 for a command, 1 for a register write
--
local function send(value, mode)
local high = bit.band(value, 0xF0)
local low = bit.lshift(bit.band(value, 0x0F), 4)
write4Bits(bit.bor(high, mode))
write4Bits(bit.bor(low, mode))
end
--- Sends a command to the display
-- @param value the command
--
local function command(value)
send(value, 0)
end
--- Writes the display register
-- @param value the data to write
--
local function write(value)
send(value, 1)
end
--- Toggles a single display control flag
-- @param flag the flag
-- @param enabled true if the flag should be enabled; false otherwise
--
local function displaycontrol(flag, enabled)
if enabled == true then
_displaycontrol = bit.bor(_displaycontrol, flag)
else
_displaycontrol = bit.band(_displaycontrol, bit.bnot(flag))
end
command(bit.bor(LCD_DISPLAYCONTROL, _displaycontrol))
end
--- Clears the display
--
function lcd.clear()
command(LCD_CLEARDISPLAY)
tmr.delay(2000)
end
function lcd.home()
command(LCD_RETURNHOME)
tmr.delay(2000)
end
--- Turns the backlight on or off
-- @param enabled true if the backlight should be enabled; false if not
--
function lcd.backlight(enabled)
_backlight = enabled and LCD_BACKLIGHT or LCD_NOBACKLIGHT
writeData(0)
end
--- Sets the cursor position
-- @param col the column
-- @param row the row
--
function lcd.cursorPosition(col, row)
command(LCD_SETDDRAMADDR + (col + (row * 0x40)))
end
--- Turns the display on or off
-- @param enabled true if the display should be enabled; false if not
--
function lcd.display(enabled)
displaycontrol(LCD_DISPLAYON, enabled)
end
--- Turns the cursor on or off
-- @param enabled true if the cursor should be enabled; false if not
--
function lcd.cursor(enabled)
displaycontrol(LCD_CURSORON, enabled)
end
--- Turns the cursor blinking on or off
-- @param enabled true if the blinking cursor should be enabled; false if not
--
function lcd.blink(enabled)
displaycontrol(LCD_BLINKON, enabled)
end
--- Initializes the LCD
-- @param address the I2C device address
--
function lcd.init(address)
_address = address
_backlight = LCD_BACKLIGHT
_displayfunction = bit.bor(LCD_4BITMODE, LCD_2LINE, LCD_5x8DOTS)
writeData(_backlight)
tmr.delay(1000000)
write4Bits(bit.lshift(0x03, 4))
tmr.delay(4500)
write4Bits(bit.lshift(0x03, 4))
tmr.delay(4500)
write4Bits(bit.lshift(0x03, 4))
tmr.delay(150)
write4Bits(bit.lshift(0x02, 4))
command(bit.bor(0x20, _displayfunction))
-- Default parameters for display initialization
_displaycontrol = bit.bor(LCD_DISPLAYON)
command(bit.bor(LCD_DISPLAYCONTROL, _displaycontrol))
lcd.clear()
_displaymode = bit.bor(LCD_ENTRYLEFT, LCD_ENTRYSHIFTDECREMENT)
command(bit.bor(LCD_ENTRYMODESET, _displaymode))
lcd.home()
end
--- Prints the given text to the specified cursor position
-- @param col the column
-- @param row the row
-- @param value the text to print
--
function lcd.print(value)
for i = 1, #value do
write(value:byte(i))
end
end
--- Prints the given text to the specified cursor position
-- @param col the column
-- @param row the row
-- @param value the text to print
--
function lcd.printAt(col, row, value)
lcd.cursorPosition(col, row)
lcd.print(value)
end
return lcd