-
Notifications
You must be signed in to change notification settings - Fork 18
/
lcdkey.rm1xx.sb
361 lines (317 loc) · 10 KB
/
lcdkey.rm1xx.sb
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//******************************************************************************
// Copyright (c) 2016, Laird
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// SPDX-License-Identifier:ISC
//
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++ ++
// +++++ When UwTerminal downloads the app it will store it as a filenname ++
// +++++ which consists of all characters up to the first . and excluding it ++
// +++++ ++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// This application ineracts with the OSEPP 16 x 2 LCD and keypad. The LCD is
// controlled using a 4-bit wide parallel bus and two control lines. The code
// below is based on the LiquidCrystal Library for the Arduino platform.
// https://www.arduino.cc/en/Reference/LiquidCrystal
//
// The LCD module uses the SPLC780D 16 COMs x 40 SEGs dot-matrix LCD controller
// and driver - See more at:
// http://osepp.com/products/shield-arduino-compatible/16x2-lcd-display-keypad-shield/#sthash.7z3CTc51.dpuf
//
// In addition to the LCD, there is are 5 push buttons that correspond to up,
// down, left, right and select. These push buttons are connected to a single
// analog input. By reading the voltage on the analog pin, the application can
// tell which button has been pressed.
//
// This applicaiton will display which button has been pressed on the LCD.
//
// Please see the corresponding application note for directions on how to
// configure the RM1xx DVK for this application.
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Convention : (1) Case sensitive, and commands are presented in alphabetic order
// (2) If line ends with \ then it continues on next line. That does
// not mean that it should be sent as multiple lines
// (3) Replace anything between ##
// (4) #INTaaaa# means a number in decimal, hex, octal or binary
// format -> 23 == 0x17 == h'17 == o'23 == b'10111
// aaaa is just a description
// (5) #HEXaaaa# means a string without delimitors consisting of hex
// characters only aaaa is just a description
// (6) #STRaaaa# means a string without delimitors
// aaaa is just a description
// (7) "STRaaaa" means a string which must have the " delimitor
// aaaa is just a description
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//******************************************************************************
//Pin definitions for the LCD
#define RS_PIN 0
#define ENABLE_PIN 17
#define D0_PIN 3
#define D1_PIN 4
#define D2_PIN 5
#define D3_PIN 30
//Pin definitions for the push buttons
#define PUSH_BUTTONS 6
#define DIGITAL_IN 1
#define DIGITAL_OUT 2
#define ANALOG_IN 3
#define TEN_BIT_ADC_UNITY 0x11
#define TEN_BIT_ADC_ONETHIRD 0x13
#define TEN_BIT_ADC_TWOTHIRD 0x00 //System default
//Commands
#define LCD_CLEARDISPLAY 0x01
#define LCD_RETURNHOME 0x02
#define LCD_ENTRYMODESET 0x04
#define LCD_DISPLAYCONTROL 0x08
#define LCD_CURSORSHIFT 0x10
#define LCD_FUNCTIONSET 0x20
#define LCD_SETCGRAMADDR 0x40
#define LCD_SETDDRAMADDR 0x80
//Flags for display entry mode
#define LCD_ENTRYRIGHT 0x00
#define LCD_ENTRYLEFT 0x02
#define LCD_ENTRYSHIFTINCREMENT 0x01
#define LCD_ENTRYSHIFTDECREMENT 0x00
//Flags for display on/off control
#define LCD_DISPLAYON 0x04
#define LCD_DISPLAYOFF 0x00
#define LCD_CURSORON 0x02
#define LCD_CURSOROFF 0x00
#define LCD_BLINKON 0x01
#define LCD_BLINKOFF 0x00
//Flags for display/cursor shift
#define LCD_DISPLAYMOVE 0x08
#define LCD_CURSORMOVE 0x00
#define LCD_MOVERIGHT 0x04
#define LCD_MOVELEFT 0x00
//Flags for function set
#define LCD_8BITMODE 0x10
#define LCD_4BITMODE 0x00
#define LCD_2LINE 0x08
#define LCD_1LINE 0x00
#define LCD_5x10DOTS 0x04
#define LCD_5x8DOTS 0x00
dim rc
dim adcVal
dim displayControl
dim displayMode
dim s$
//Sleep for ms milliseconds
sub Sleep(ms)
dim C, E
C = 0
E = GETTICKCOUNT() + ms
while (C < E)
C = GETTICKCOUNT()
endwhile
endsub
function InitPins()
//Set all of the LCD pins as digital outputs
rc=GpioSetFunc(RS_PIN, DIGITAL_OUT, 0)
if (rc != 0) then
print "Error initialising RS_PIN\n"
exitfunc rc
endif
rc=GpioSetFunc(ENABLE_PIN, DIGITAL_OUT, 0)
if (rc != 0) then
print "Error initialising ENABLE_PIN\n"
exitfunc rc
endif
rc=GpioSetFunc(D0_PIN, DIGITAL_OUT, 0)
if (rc != 0) then
print "Error initialising D0\n"
exitfunc rc
endif
rc=GpioSetFunc(D1_PIN, DIGITAL_OUT, 0)
if (rc != 0) then
print "Error initialising D1\n"
exitfunc rc
endif
rc=GpioSetFunc(D2_PIN, DIGITAL_OUT, 0)
if (rc != 0) then
print "Error initialising D2\n"
exitfunc rc
endif
rc=GpioSetFunc(D3_PIN, DIGITAL_OUT, 0)
if (rc != 0) then
print "Error initialising D3\n"
exitfunc rc
endif
//Assign all of the pushbutton pins to one analog input
rc=GpioSetFunc(PUSH_BUTTONS, ANALOG_IN, TEN_BIT_ADC_UNITY)
if (rc != 0) then
print "Error initialising PUSH BUTTON ADC\n"
exitfunc rc
endif
print "Successfully initialised GPIOs\n"
endfunc rc
sub PulseEnable()
GpioWrite(ENABLE_PIN, 0)
Sleep(1)
GpioWrite(ENABLE_PIN, 1)
Sleep(1) //Enable pulse must be >450ns
GpioWrite(ENABLE_PIN, 0)
Sleep(1) //Commands need > 37us to settle
endsub
sub Write4Bits(value)
dim val
val = ((value >> 0) & 0x01)
GpioWrite(D0_PIN, ((value >> 0) & 0x01))
val = ((value >> 1) & 0x01)
GpioWrite(D1_PIN, ((value >> 1) & 0x01))
val = ((value >> 2) & 0x01)
GpioWrite(D2_PIN, ((value >> 2) & 0x01))
val = ((value >> 3) & 0x01)
GpioWrite(D3_PIN, ((value >> 3) & 0x01))
PulseEnable()
endsub
sub lcdCommand(value)
GpioWrite(RS_PIN, 0)
//Send an 8-bit command to the LCD
Write4Bits(value>>4)
Write4Bits(value)
endsub
sub lcdWrite(char)
GpioWrite(RS_PIN, 1)
//Send an 8-bit command to the LCD
Write4Bits(char>>4)
Write4Bits(char)
endsub
sub lcdPrint(string$)
dim index
index = 0
while index < strlen(string$)
lcdWrite(STRGETCHR(string$, index))
index = index + 1
endwhile
endsub
sub lcdClear()
lcdCommand(LCD_CLEARDISPLAY)
Sleep(2)
endsub
sub lcdHome()
lcdCommand(LCD_RETURNHOME)
Sleep(2)
endsub
sub lcdSetCursor(col, row)
lcdCommand(LCD_SETDDRAMADDR | (col + (row*0x40)))
endsub
sub lcdScrollLeft()
lcdCommand(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT)
endsub
sub lcdFlushLeft()
dim index : index = 0
while index < 16
lcdScrollLeft()
Sleep(250)
index = index+1
endwhile
endsub
sub lcdScrollRight()
lcdCommand(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT)
endsub
//Turn the display on/off (quickly)
sub lcdNoDisplay()
displayControl = (displayControl& ~LCD_DISPLAYON)
lcdCommand(LCD_DISPLAYCONTROL | displayControl)
endsub
sub lcdDisplay()
displayControl = (displayControl | LCD_DISPLAYON)
lcdCommand(LCD_DISPLAYCONTROL | displayControl)
endsub
sub lcdBegin()
rc=InitPins()
if (rc != 0) then
print "error initialising GPIO pins\n"
endif
//Put the LCD into 4 bit or 8 bit mode
Write4Bits(0x03)
Sleep(5) //Wait min 4.1ms
//Second try
Write4Bits(0x03)
Sleep(5) //Wait min 4.1ms
//Third go!
Write4Bits(0x03)
Sleep(1)
//Finally, set to 4-bit interface
Write4Bits(0x02)
//Turn the display on with no cursor or blinking default
displayControl = (LCD_CURSOROFF | LCD_BLINKOFF)
lcdDisplay()
//Initialise to default text direction (for romance languages)
displayMode = (LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT)
//Set the entry mode
lcdCommand(LCD_ENTRYMODESET | displayMode)
endsub
sub LeftButton()
lcdClear()
s$="Left"
lcdPrint(s$)
print "Left\n"
endsub
sub RightButton()
lcdClear()
s$="Right"
lcdPrint(s$)
print "Right\n"
endsub
sub UpButton()
lcdClear()
s$="Up"
lcdPrint(s$)
print "Up\n"
endsub
sub DownButton()
lcdClear()
s$="Down"
lcdPrint(s$)
print "Down\n"
endsub
sub SelectButton()
lcdClear()
s$="Select"
lcdPrint(s$)
print "Select\n"
endsub
sub CheckButtons(adcVal)
if (adcVal <= 50) then
RightButton()
elseif (adcVal <= 105) then
UpButton()
elseif (adcVal <= 250) then
DownButton()
elseif (adcVal <= 400) then
LeftButton()
elseif (adcVal <= 620) then
SelectButton()
endif
endsub
lcdBegin()
lcdClear()
s$ = "Press some"
lcdPrint(s$)
s$ = "buttons!"
lcdSetCursor(0,1)
lcdPrint(s$)
print "Press some buttons\n"
//Loop and check the push buttons
do
adcVal = gpioread(PUSH_BUTTONS)
checkButtons(adcVal)
sleep(100)
dowhile 1
waitevent