/
LCDBlocking.c
393 lines (361 loc) · 10.4 KB
/
LCDBlocking.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
/*********************************************************************
*
* LCD Access Routines
*
*********************************************************************
* FileName: LCDBlocking.c
* Dependencies: None
* Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F, PIC32
* Compiler: Microchip C32 v1.05 or higher
* Microchip C30 v3.12 or higher
* Microchip C18 v3.30 or higher
* HI-TECH PICC-18 PRO 9.63PL2 or higher
* Company: Microchip Technology, Inc.
*
* Software License Agreement
*
* Copyright (C) 2002-2009 Microchip Technology Inc. All rights
* reserved.
*
* Microchip licenses to you the right to use, modify, copy, and
* distribute:
* (i) the Software when embedded on a Microchip microcontroller or
* digital signal controller product ("Device") which is
* integrated into Licensee's product; or
* (ii) ONLY the Software driver source files ENC28J60.c, ENC28J60.h,
* ENCX24J600.c and ENCX24J600.h ported to a non-Microchip device
* used in conjunction with a Microchip ethernet controller for
* the sole purpose of interfacing with the ethernet controller.
*
* You should refer to the license agreement accompanying this
* Software for additional information regarding your rights and
* obligations.
*
* THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
* WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
* LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
* PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
* BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
* THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
* SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
* (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
*
*
* Author Date Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Howard Schlunder 4/03/06 Original
* Howard Schlunder 4/12/06 Changed from using PMP to LCDWrite()
* Howard Schlunder 8/10/06 Fixed a delay being too short
* when CLOCK_FREQ was a smaller
* value, added FOUR_BIT_MODE
********************************************************************/
#define __LCDBLOCKING_C
#include "Include/TCPIP_Stack/TCPIP.h"
#define USE_LCD
#if defined(USE_LCD)
//#define FOUR_BIT_MODE
#define SAMSUNG_S6A0032 // This LCD driver chip has a different means of entering 4-bit mode.
// LCDText is a 32 byte shadow of the LCD text. Write to it and
// then call LCDUpdate() to copy the string into the LCD module.
BYTE LCDText[16*2+1];
/******************************************************************************
* Function: static void LCDWrite(BYTE RS, BYTE Data)
*
* PreCondition: None
*
* Input: RS - Register Select - 1:RAM, 0:Config registers
* Data - 8 bits of data to write
*
* Output: None
*
* Side Effects: None
*
* Overview: Controls the Port I/O pins to cause an LCD write
*
* Note: None
*****************************************************************************/
static void LCDWrite(BYTE RS, BYTE Data)
{
#if defined(LCD_DATA_TRIS)
LCD_DATA_TRIS = 0x00;
#else
LCD_DATA0_TRIS = 0;
LCD_DATA1_TRIS = 0;
LCD_DATA2_TRIS = 0;
LCD_DATA3_TRIS = 0;
#if !defined(FOUR_BIT_MODE)
LCD_DATA4_TRIS = 0;
LCD_DATA5_TRIS = 0;
LCD_DATA6_TRIS = 0;
LCD_DATA7_TRIS = 0;
#endif
#endif
LCD_RS_TRIS = 0;
LCD_RD_WR_TRIS = 0;
LCD_RD_WR_IO = 0;
LCD_RS_IO = RS;
#if defined(FOUR_BIT_MODE)
#if defined(LCD_DATA_IO)
LCD_DATA_IO = Data>>4;
#else
LCD_DATA0_IO = Data & 0x10;
LCD_DATA1_IO = Data & 0x20;
LCD_DATA2_IO = Data & 0x40;
LCD_DATA3_IO = Data & 0x80;
#endif
Nop(); // Wait Data setup time (min 40ns)
Nop();
LCD_E_IO = 1;
Nop(); // Wait E Pulse width time (min 230ns)
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
LCD_E_IO = 0;
#endif
#if defined(LCD_DATA_IO)
LCD_DATA_IO = Data;
#else
LCD_DATA0_IO = ((Data & 0x01) == 0x01);
LCD_DATA1_IO = ((Data & 0x02) == 0x02);
LCD_DATA2_IO = ((Data & 0x04) == 0x04);
LCD_DATA3_IO = ((Data & 0x08) == 0x08);
#if !defined(FOUR_BIT_MODE)
LCD_DATA4_IO = ((Data & 0x10) == 0x10);
LCD_DATA5_IO = ((Data & 0x20) == 0x20);
LCD_DATA6_IO = ((Data & 0x40) == 0x40);
LCD_DATA7_IO = ((Data & 0x80) == 0x80);
#endif
#endif
Nop(); // Wait Data setup time (min 40ns)
Nop();
LCD_E_IO = 1;
Nop(); // Wait E Pulse width time (min 230ns)
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
LCD_E_IO = 0;
// // Uncomment if you want the data bus to go High-Z when idle
// // Note that this may make analog functions work poorly when using
// // Explorer 16 revision 5 boards with a 5V LCD on it. The 5V LCDs have
// // internal weak pull ups to 5V on each of the I/O pins, which will
// // backfeed 5V weekly onto non-5V tolerant PIC I/O pins.
// #if defined(LCD_DATA_TRIS)
// LCD_DATA_TRIS = 0xFF;
// #else
// LCD_DATA0_TRIS = 1;
// LCD_DATA1_TRIS = 1;
// LCD_DATA2_TRIS = 1;
// LCD_DATA3_TRIS = 1;
// #if !defined(FOUR_BIT_MODE)
// LCD_DATA4_TRIS = 1;
// LCD_DATA5_TRIS = 1;
// LCD_DATA6_TRIS = 1;
// LCD_DATA7_TRIS = 1;
// #endif
// #endif
// LCD_RS_TRIS = 1;
// LCD_RD_WR_TRIS = 1;
}
/******************************************************************************
* Function: void LCDInit(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: LCDText[] is blanked, port I/O pin TRIS registers are
* configured, and the LCD is placed in the default state
*
* Note: None
*****************************************************************************/
void LCDInit(void)
{
BYTE i;
memset(LCDText, ' ', sizeof(LCDText)-1);
LCDText[sizeof(LCDText)-1] = 0;
// Setup the I/O pins
LCD_E_IO = 0;
LCD_RD_WR_IO = 0;
#if defined(LCD_DATA_TRIS)
LCD_DATA_TRIS = 0x00;
#else
LCD_DATA0_TRIS = 0;
LCD_DATA1_TRIS = 0;
LCD_DATA2_TRIS = 0;
LCD_DATA3_TRIS = 0;
#if !defined(FOUR_BIT_MODE)
LCD_DATA4_TRIS = 0;
LCD_DATA5_TRIS = 0;
LCD_DATA6_TRIS = 0;
LCD_DATA7_TRIS = 0;
#endif
#endif
LCD_RD_WR_TRIS = 0;
LCD_RS_TRIS = 0;
LCD_E_TRIS = 0;
// Wait the required time for the LCD to reset
DelayMs(40);
// Set the default function
// Go to 8-bit mode first to reset the instruction state machine
// This is done in a loop 3 times to absolutely ensure that we get
// to 8-bit mode in case if the device was previously booted into
// 4-bit mode and our PIC got reset in the middle of the LCD
// receiving half (4-bits) of an 8-bit instruction
LCD_RS_IO = 0;
#if defined(LCD_DATA_IO)
LCD_DATA_IO = 0x03;
#else
LCD_DATA0_IO = 1;
LCD_DATA1_IO = 1;
LCD_DATA2_IO = 0;
LCD_DATA3_IO = 0;
#if !defined(FOUR_BIT_MODE)
LCD_DATA4_IO = 0;
LCD_DATA5_IO = 0;
LCD_DATA6_IO = 0;
LCD_DATA7_IO = 0;
#endif
#endif
Nop(); // Wait Data setup time (min 40ns)
Nop();
for(i = 0; i < 3u; i++)
{
LCD_E_IO = 1;
Delay10us(1); // Wait E Pulse width time (min 230ns)
LCD_E_IO = 0;
DelayMs(2);
}
#if defined(FOUR_BIT_MODE)
#if defined(SAMSUNG_S6A0032)
// Enter 4-bit mode (requires only 4-bits on the S6A0032)
#if defined(LCD_DATA_IO)
LCD_DATA_IO = 0x02;
#else
LCD_DATA0_IO = 0;
LCD_DATA1_IO = 1;
LCD_DATA2_IO = 0;
LCD_DATA3_IO = 0;
#endif
Nop(); // Wait Data setup time (min 40ns)
Nop();
LCD_E_IO = 1;
Delay10us(1); // Wait E Pulse width time (min 230ns)
LCD_E_IO = 0;
#else
// Enter 4-bit mode with two lines (requires 8-bits on most LCD controllers)
LCDWrite(0, 0x28);
#endif
#else
// Use 8-bit mode with two lines
LCDWrite(0, 0x38);
#endif
Delay10us(5);
// Set the entry mode
LCDWrite(0, 0x06); // Increment after each write, do not shift
Delay10us(5);
// Set the display control
LCDWrite(0, 0x0C); // Turn display on, no cusor, no cursor blink
Delay10us(5);
// Clear the display
LCDWrite(0, 0x01);
DelayMs(2);
}
/******************************************************************************
* Function: void LCDUpdate(void)
*
* PreCondition: LCDInit() must have been called once
*
* Input: LCDText[]
*
* Output: None
*
* Side Effects: None
*
* Overview: Copies the contents of the local LCDText[] array into the
* LCD's internal display buffer. Null terminators in
* LCDText[] terminate the current line, so strings may be
* printed directly to LCDText[].
*
* Note: None
*****************************************************************************/
void LCDUpdate(void)
{
BYTE i, j;
// Go home
LCDWrite(0, 0x02);
DelayMs(2);
// Output first line
for(i = 0; i < 16u; i++)
{
// Erase the rest of the line if a null char is
// encountered (good for printing strings directly)
if(LCDText[i] == 0u)
{
for(j=i; j < 16u; j++)
{
LCDText[j] = ' ';
}
}
LCDWrite(1, LCDText[i]);
Delay10us(5);
}
// Set the address to the second line
LCDWrite(0, 0xC0);
Delay10us(5);
// Output second line
for(i = 16; i < 32u; i++)
{
// Erase the rest of the line if a null char is
// encountered (good for printing strings directly)
if(LCDText[i] == 0u)
{
for(j=i; j < 32u; j++)
{
LCDText[j] = ' ';
}
}
LCDWrite(1, LCDText[i]);
Delay10us(5);
}
}
/******************************************************************************
* Function: void LCDErase(void)
*
* PreCondition: LCDInit() must have been called once
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: Clears LCDText[] and the LCD's internal display buffer
*
* Note: None
*****************************************************************************/
void LCDErase(void)
{
// Clear display
LCDWrite(0, 0x01);
DelayMs(2);
// Clear local copy
memset(LCDText, ' ', 32);
}
#endif //#if defined(USE_LCD)