-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCD_HD44780_4BIT.c
463 lines (370 loc) · 17.4 KB
/
LCD_HD44780_4BIT.c
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/* ########################################################################## */
/*
* This file was created by www.DavesMotleyProjects.com
*
* This software is provided under the following conditions:
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* 'Software'), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* */
/* ########################################################################## */
/* ===========================================================================*/
/*
* FileName: LCD_HD44780_4BIT.c
*
* This example demonstrates writing to a 20x4 character LCD module based on
* the HD44780 display controller using the 4-bit parallel interface mode. The
* example uses a blocking delay to set the update speed of an 8-bit up-counter
* which is displayed on decimal, hexadecimal, and binary formats on the LCD.
* This example can be used to explore display characteristics such as refresh
* rate.
*
* Version 1.0
*
* Rev. 1.0, Initial Release
*
* */
/* ===========================================================================*/
#include "MSP430F5529LP.h"
#include "MSP430F5529LP_CLOCK.h"
#include "MSP430F5529LP_TIMERA2.h"
#include "MSP430F5529LP_GPIO.h"
#include "LCD_HD44780_4BIT.h"
#include "ctype.h"
#include "stdio.h"
#include "string.h"
#include "stdarg.h"
/******************************************************************************
PUBLIC DEFINITIONS
******************************************************************************/
/******************************************************************************
PUBLIC VARIABLES
******************************************************************************/
/******************************************************************************
PRIVATE DEFINITIONS (static const)
******************************************************************************/
// These delays are from the HD44780 datasheet for various hold times,
// minimum pulse width values, etc.
#define LCD_DELAY_5ns __delay_cycles((005e-9)/(1.0/CLOCK_FREQ))
#define LCD_DELAY_10ns __delay_cycles((010e-9)/(1.0/CLOCK_FREQ))
#define LCD_DELAY_40ns __delay_cycles((040e-9)/(1.0/CLOCK_FREQ))
#define LCD_DELAY_80ns __delay_cycles((080e-9)/(1.0/CLOCK_FREQ))
#define LCD_DELAY_230ns __delay_cycles((230e-9)/(1.0/CLOCK_FREQ))
#define LCD_DELAY_270ns __delay_cycles((270e-9)/(1.0/CLOCK_FREQ))
#define LCD_DELAY_150us __delay_cycles((150e-6)/(1.0/CLOCK_FREQ))
#define LCD_DELAY_50us __delay_cycles((050e-6)/(1.0/CLOCK_FREQ))
#define LCD_DELAY_2ms __delay_cycles((002e-3)/(1.0/CLOCK_FREQ))
#define LCD_DELAY_5ms __delay_cycles((005e-3)/(1.0/CLOCK_FREQ))
#define LCD_DELAY_100ms __delay_cycles((100e-3)/(1.0/CLOCK_FREQ))
typedef enum
{
COMMAND = 0,
DATA = 1
} LcdRegType_enum;
typedef enum
{
UPPER_NIBBLE = 0,
LOWER_NIBBLE = 1
} LcdNibble_enum;
/******************************************************************************
PRIVATE FUNCTION PROTOTYPES (static)
******************************************************************************/
static void LcdWriteReg(uint16_t value, LcdRegType_enum reg);
static void LcdWrite4bits(uint16_t value, LcdNibble_enum nibble);
static void SetPinsForRead(void);
static void SetPinsForWrite(void);
/******************************************************************************
PRIVATE VARIABLES (static)
******************************************************************************/
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// ---- READ ME - DEBUGGING INFO ----
//
// To view file scope static variables in the CCS debug watch window, the
// following syntax must be used. 'filename.c'::variableName
// The filename must be the full filename including the .c extension and
// must be surrounded by the single quotes, followed by a double-colon.
//
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
static uint16_t s_LcdStartTime;
static uint16_t s_LCD_RS;
static uint16_t s_LCD_RW;
static uint16_t s_LCD_EN;
static uint16_t s_LCD_D4;
static uint16_t s_LCD_D5;
static uint16_t s_LCD_D6;
static uint16_t s_LCD_D7;
static uint16_t s_str_size;
static char s_lcd_string[40];
static char* s_lcd_stringPtr;
/******************************************************************************
Subroutine: LCD_Initialize
Description: This function initializes the LCD display.
Inputs: uint16_t LcdPin_RS: Pin number of the Register Select pin
uint16_t LcdPin_RW: Pin number of the Read/Write pin
uint16_t LcdPin_EN: Pin number of the Enable pin
uint16_t LcdPin_D4: Pin number of data bus pin 4
uint16_t LcdPin_D5: Pin number of data bus pin 5
uint16_t LcdPin_D6: Pin number of data bus pin 6
uint16_t LcdPin_D7: Pin number of data bus pin 7
NOTE: All pin numbers are package pins (see GPIO hardware
library for more information)
Outputs: None
******************************************************************************/
void LCD_Initialize(uint16_t LcdPin_RS,
uint16_t LcdPin_RW,
uint16_t LcdPin_EN,
uint16_t LcdPin_D4,
uint16_t LcdPin_D5,
uint16_t LcdPin_D6,
uint16_t LcdPin_D7)
{
// Save the LCD pin assignments to file scope variables
s_LCD_RS = LcdPin_RS;
s_LCD_RW = LcdPin_RW;
s_LCD_EN = LcdPin_EN;
s_LCD_D4 = LcdPin_D4;
s_LCD_D5 = LcdPin_D5;
s_LCD_D6 = LcdPin_D6;
s_LCD_D7 = LcdPin_D7;
// Assign the direction of the LCD control lines. These will always be
// outputs from the MCU
pinDirection(s_LCD_EN, OUTPUT);
pinDirection(s_LCD_RS, OUTPUT);
pinDirection(s_LCD_RW, OUTPUT);
pinOutput(s_LCD_EN, 0); // Default state, EN = 0
// Set data lines as outputs
SetPinsForWrite();
// Wait 100 ms for Internal LCD Initialization to complete
LCD_DELAY_100ms;
// put the LCD into 4 bit mode. This is according to the hitachi
// HD44780 datasheet figure 24, pg 46
LcdWrite4bits(0x03, LOWER_NIBBLE);
LCD_DELAY_5ms;
LcdWrite4bits(0x03, LOWER_NIBBLE);
LCD_DELAY_5ms;
LcdWrite4bits(0x03, LOWER_NIBBLE);
LCD_DELAY_150us;
LcdWrite4bits(0x02, LOWER_NIBBLE);
// function set: 4-bit interface, 2 display lines, 5x7 font
LcdWriteReg(0x28, COMMAND);
// entry mode set: increment automatically, no display shift
LcdWriteReg(0x06, COMMAND);
// display control: turn display on, cursor off, no blinking
LcdWriteReg(0x0C, COMMAND);
// clear display: clear display, set cursor position to zero
LcdWriteReg(0x01, COMMAND);
LCD_DELAY_2ms; // clear command requires at least 1.5ms
return;
}
/******************************************************************************
Subroutine: LCD_SetPosition
Description: This function sets the cursor position of the LCD display.
Text sent via LCD_Print() will begin printing at the
cursor position specified.
Inputs: uint16_t value: The desired position of the cursor.
Outputs: None
******************************************************************************/
void LCD_SetPosition(uint16_t value)
{
// if the cursor value is illegal, take no action
if ( (LCD_ROW1 > value) || ((LCD_ROW4 + 20) < value) ) {return;}
/* This next section is critical to ensuring that writing text that is too
* large for the remaining characters in the row does not overflow beyond
* the end of the current row. The size of the string that will be allowed
* to be written using LCD_Print() will be limited based on the cursor
* position requested. Without this limitation, the LCD controller would
* normally overflow from row 1 to row 3, and from row 2 to row 4. This is
* because the controller is actually made for 40x2 LCDs, but is being used
* for a 20x4 LCD. If you're curious, comment this next section out, write
* a very large text string and see what happens.
*/
if (LCD_ROW4 <= value) {s_str_size = LCD_ROW4 + 20 - value;}
else if (LCD_ROW2 <= value) {s_str_size = LCD_ROW2 + 20 - value;}
else if (LCD_ROW3 <= value) {s_str_size = LCD_ROW3 + 20 - value;}
else if (LCD_ROW1 <= value) {s_str_size = LCD_ROW1 + 20 - value;}
// Write the new cursor position to the LCD
LcdWriteReg(value, COMMAND);
return;
}
/******************************************************************************
Subroutine: LCD_Print
Description: This function prints the text string to the LCD display.
Inputs: const char * format, ...: This is the string that contains
the text to be written to the LCD. It can optionally
contain embedded format tags that are replaced by the
values specified in subsequent additional arguments and
formatted as requested. Works the same as printf().
Outputs: None
******************************************************************************/
void LCD_Print(const char * format, ...)
{
int x;
/* Using the C library stdarg.h the 2 lines of code are required to access
* the unnamed arguments. One must declare a variable of type va_list. The
* macro va_start is then called with two arguments: the first is the
* variable declared of the type va_list, the second is the name of the
* last named parameter of the function. After this, each invocation of the
* va_arg macro yields the next argument. The first argument to va_arg
* is the va_list and the second is the type of the next argument passed to
* the function. Finally, the va_end macro must be called on the va_list
* before the function returns. (It is not required to read in all the
* arguments.)
*/
va_list args;
va_start(args, format);
// This shouldn't ever change, but just in case, make sure the string
// pointer is set to the beginning of the string variable.
s_lcd_stringPtr = &s_lcd_string[0];
// This function sends the formatted output to the string s_lcd_string
// using the argument list passed to it. Because this function accepts the
// arg list as-is, there is no need to parse the arguments.
vsprintf(s_lcd_string, format, args);
// If the string passed to LCD_Print is larger than the number of
// remaining characters, limit the size to not exceed the row.
if (s_str_size > strlen(s_lcd_string))
{
s_str_size = strlen(s_lcd_string);
}
// For each character in the string, send the character to the LCD display.
// The LCD was initialized for the cursor to auto-increment, so each new
// character will be placed at current position + 1.
for (x=0; x<s_str_size; x++)
{
LcdWriteReg(s_lcd_stringPtr[x], DATA);
}
// The va_end macro performs cleanup for the args object initialized by the
// call to vs_start(). This frees the va_list.
va_end(args);
return;
}
/******************************************************************************
Subroutine: LcdWriteReg
Description: This function writes the requested value to the LCD, and
blocks until the request is completed.
Inputs: uint16_t value: value to be written to the LCD
LcdRegType_enum reg: defines the register to be written.
Can be COMMAND or DATA register.
Outputs: None
******************************************************************************/
void LcdWriteReg(uint16_t value, LcdRegType_enum reg)
{
static uint16_t busy;
pinOutput(s_LCD_EN, 0); // EN = 0, should already be true
// In case it wasn't, hold enable low for the minimum address hold time
// before changing RS or RW.
LCD_DELAY_10ns;
pinOutput(s_LCD_RS, reg); // RS = 0 for Instruction registers
// RS = 1 for Data registers
pinOutput(s_LCD_RW, 0); // RW = 0, select Write operation
// After changing RS and RW, delay for the minimum address setup time
// before changing the enable state.
LCD_DELAY_40ns;
LcdWrite4bits(value, UPPER_NIBBLE); // Set the data D0-D7 = value (4-7)
LcdWrite4bits(value, LOWER_NIBBLE); // Set the data D0-D7 = value (0-3)
// commands require at least 37us to settle.
if (COMMAND == reg) { LCD_DELAY_50us; }
SetPinsForRead();
pinOutput(s_LCD_RS, 0); // RS = 0; Select Instruction reg
pinOutput(s_LCD_RW, 1); // RW = 0, select Read operation
LCD_DELAY_40ns;
s_LcdStartTime = GetTick();
do // Spin here until busy = False, Or the timeout expires.
{
pinOutput(s_LCD_EN, 1); // EN = 1, prepare to read data
LCD_DELAY_230ns;
busy = pinInput(s_LCD_D7); // Read BUSY flag
pinOutput(s_LCD_EN, 0); // EN = 0, strobe data write
LCD_DELAY_270ns;
// In 4-bit mode the 2nd nibble must be read even if it isn't used.
pinOutput(s_LCD_EN, 1); // EN = 1, prepare to read data
LCD_DELAY_230ns;
pinOutput(s_LCD_EN, 0); // EN = 0, strobe data write
LCD_DELAY_270ns;
} while ( busy && (!(Expired(1000, s_LcdStartTime, GetTick()))) );
SetPinsForWrite();
return;
}
/******************************************************************************
Subroutine: LcdWrite4bits
Description: In 4-bit interface mode the full 8-bit command is broken
into 2 nibbles, and sent one nibble at a time, upper
nibble first. This function is used to write the nibbles.
Inputs: uint16_t value: The 8-bit data value
LcdNibble_enum nibble: defines which nibble to put on the
data bus (UPPER_NIBBLE or LOWER_NIBBLE)
Outputs: None
******************************************************************************/
void LcdWrite4bits(uint16_t value, LcdNibble_enum nibble)
{
pinOutput(s_LCD_EN, 1); // EN = 1, prepare to write data
// Set the requested nibble to the data bus
if (UPPER_NIBBLE == nibble)
{
pinOutput(s_LCD_D4, ((value >> 4) & 1));
pinOutput(s_LCD_D5, ((value >> 5) & 1));
pinOutput(s_LCD_D6, ((value >> 6) & 1));
pinOutput(s_LCD_D7, ((value >> 7) & 1));
}
else
{
pinOutput(s_LCD_D4, (value & 1));
pinOutput(s_LCD_D5, ((value >> 1) & 1));
pinOutput(s_LCD_D6, ((value >> 2) & 1));
pinOutput(s_LCD_D7, ((value >> 3) & 1));
}
// Hold enable high for minimum high level pulse width
LCD_DELAY_230ns;
pinOutput(s_LCD_EN, 0); // EN = 0, strobe data write
// Hold enable low for minimum data hold time (which is only 5 ns), or
// the value required to ensure that the fastest enable cycle time is
// longer than 500 ns.
LCD_DELAY_270ns;
return;
}
/******************************************************************************
Subroutine: SetPinsForRead
Description: This function changes the direction of the data bus pins
to inputs. This is needed to read the busy flag to
determine if the requsted action has completed.
Inputs: None
Outputs: None
******************************************************************************/
void SetPinsForRead(void)
{
pinDirection(s_LCD_D4, INPUT);
pinDirection(s_LCD_D5, INPUT);
pinDirection(s_LCD_D6, INPUT);
pinDirection(s_LCD_D7, INPUT);
return;
}
/******************************************************************************
Subroutine: SetPinsForWrite
Description: This function changes the direction of the data bus pins
to outputs. This is needed to write the next request to
the LCD.
Inputs: None
Outputs: None
******************************************************************************/
void SetPinsForWrite(void)
{
pinDirection(s_LCD_D4, OUTPUT);
pinDirection(s_LCD_D5, OUTPUT);
pinDirection(s_LCD_D6, OUTPUT);
pinDirection(s_LCD_D7, OUTPUT);
return;
}
/******************************************************************************
End of File: LCD_HD44780_4bit.c
******************************************************************************/