/[H8]/trunk/PIC/Demo trimmet/xlcd.c
ViewVC logotype

Contents of /trunk/PIC/Demo trimmet/xlcd.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 91 - (show annotations) (download)
Tue May 8 09:37:15 2007 UTC (17 years ago) by hedin
File MIME type: text/plain
File size: 18479 byte(s)
added tcp/ip stack demo, trimmed.
1 /*********************************************************************
2 *
3 * External LCD access routines
4 *
5 *********************************************************************
6 * FileName: XLCD.c
7 * Dependencies: xlcd.h
8 * Processor: PIC18
9 * Complier: MCC18 v1.00.50 or higher
10 * HITECH PICC-18 V8.10PL1 or higher
11 * Company: Microchip Technology, Inc.
12 *
13 * Software License Agreement
14 *
15 * The software supplied herewith by Microchip Technology Incorporated
16 * (the “Company”) for its PICmicro® Microcontroller is intended and
17 * supplied to you, the Company’s customer, for use solely and
18 * exclusively on Microchip PICmicro Microcontroller products. The
19 * software is owned by the Company and/or its supplier, and is
20 * protected under applicable copyright laws. All rights are reserved.
21 * Any use in violation of the foregoing restrictions may subject the
22 * user to criminal sanctions under applicable laws, as well as to
23 * civil liability for the breach of the terms and conditions of this
24 * license.
25 *
26 * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
27 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
28 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
29 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
30 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
31 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
32 *
33 * HiTech PICC18 Compiler Options excluding device selection:
34 * -FAKELOCAL -G -E -C
35 *
36 *
37 *
38 *
39 * Author Date Comment
40 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
41 * Nilesh Rajbharti 5/8/02 Original (Rev 1.0)
42 * Nilesh Rajbharti 7/10/02 Optimized
43 ********************************************************************/
44 #include "xlcd.h"
45
46 /*********************************************************************
47 * Function: void XLCDInit(void)
48 *
49 * PreCondition: None
50 *
51 * Input: None
52 *
53 * Output: None
54 *
55 * Side Effects: None
56 *
57 * Overview: LCD is intialized
58 *
59 * Note: This function will work with all Hitachi HD447780
60 * LCD controller.
61 ********************************************************************/
62 void XLCDInit(void)
63 {
64 // The data bits must be either a 8-bit port or the upper or
65 // lower 4-bits of a port. These pins are made into inputs
66 #ifdef BIT8 // 8-bit mode, use whole port
67 DATA_PORT = 0;
68 TRIS_DATA_PORT = 0xff;
69 #else // 4-bit mode
70 #ifdef UPPER // Upper 4-bits of the port
71 DATA_PORT &= 0x0f;
72 TRIS_DATA_PORT |= 0xf0;
73 #else // Lower 4-bits of the port
74 DATA_PORT &= 0xf0;
75 TRIS_DATA_PORT |= 0x0f;
76 #endif
77 #endif
78 TRIS_RW = 0; // All control signals made outputs
79 TRIS_RS = 0;
80 TRIS_E = 0;
81 RW_PIN = 0; // R/W pin made low
82 RS_PIN = 0; // Register select pin made low
83 E_PIN = 0; // Clock pin made low
84
85 // Delay for 15ms to allow for LCD Power on reset
86 XLCDDelay15ms();
87
88 // Setup interface to LCD
89 #ifdef BIT8 // 8-bit mode interface
90 TRIS_DATA_PORT = 0; // Data port output
91 DATA_PORT = 0b00110000; // Function set cmd(8-bit interface)
92 #else // 4-bit mode interface
93 #ifdef UPPER // Upper nibble interface
94 TRIS_DATA_PORT &= 0x0f;
95 DATA_PORT &= 0x0f;
96 DATA_PORT |= 0b00100000; // Function set cmd(4-bit interface)
97 #else // Lower nibble interface
98 TRIS_DATA_PORT &= 0xf0;
99 DATA_PORT &= 0xf0;
100 DATA_PORT |= 0b00000010; // Function set cmd(4-bit interface)
101
102 #endif
103 #endif
104 E_PIN = 1; // Clock the cmd in
105 XLCDDelay500ns();
106 E_PIN = 0;
107
108
109 // Delay for at least 4.1ms
110 XLCDDelay4ms();
111
112
113 // Setup interface to LCD
114 #ifdef BIT8 // 8-bit interface
115 DATA_PORT = 0b00110000; // Function set cmd(8-bit interface)
116 #else // 4-bit interface
117 #ifdef UPPER // Upper nibble interface
118 DATA_PORT &= 0x0f; // Function set cmd(4-bit interface)
119 DATA_PORT |= 0b00100000;
120 #else // Lower nibble interface
121 DATA_PORT &= 0xf0; // Function set cmd(4-bit interface)
122 DATA_PORT |= 0b00000010;
123 #endif
124 #endif
125 E_PIN = 1; // Clock the cmd in
126 XLCDDelay500ns();
127 E_PIN = 0;
128
129 // Delay for at least 100us
130 XLCDDelay100us();
131
132 #if 1
133
134 #ifdef BIT8
135 DATA_PORT = 0b00110000; // Function set cmd(8-bit interface)
136 #else
137 #ifndef BIT8
138 #ifdef UPPER // Upper nibble interface
139 DATA_PORT &= 0x0f; // Function set cmd(4-bit interface)
140 DATA_PORT |= 0b00100000;
141 #else // Lower nibble interface
142 DATA_PORT &= 0xf0; // Function set cmd(4-bit interface)
143 DATA_PORT |= 0b00000010;
144 #endif
145 E_PIN = 1; // Clock cmd in
146 XLCDDelay500ns();
147 E_PIN = 0;
148 #endif
149 #endif
150
151 #endif
152
153
154 #ifdef BIT8 // 8-bit interface
155 TRIS_DATA_PORT = 0xff; // Make data port input
156 #else // 4-bit interface
157 #ifdef UPPER // Upper nibble interface
158 TRIS_DATA_PORT |= 0xf0; // Make data nibble input
159 #else // Lower nibble interface
160 TRIS_DATA_PORT |= 0x0f; // Make data nibble input
161 #endif
162 #endif
163
164 // Set data interface width, # lines, font
165 #if !defined(XLCD_IS_BLOCKING)
166 while(XLCDIsBusy()); // Wait if LCD busy
167 #endif
168 XLCDCommand(XCLD_TYPE); // Function set cmd
169
170
171 // Set DD Ram address to 0
172 #if !defined(XLCD_IS_BLOCKING)
173 while(XLCDIsBusy()); // Wait if LCD busy
174 #endif
175 XLCDCommand(XCLD_TYPE);
176
177
178 #if !defined(XLCD_IS_BLOCKING)
179 while(XLCDIsBusy()); // Wait if LCD busy
180 #endif
181 XLCDCommand(DOFF&XLCD_DISPLAY_SETUP);
182
183 #if !defined(XLCD_IS_BLOCKING)
184 while(XLCDIsBusy()); // Wait if LCD busy
185 #endif
186 XLCDCommand(DON&XLCD_DISPLAY_SETUP);
187
188 // Clear display
189 #if !defined(XLCD_IS_BLOCKING)
190 while(XLCDIsBusy()); // Wait if LCD busy
191 #endif
192 XLCDCommand(0x01); // Clear display
193
194 // Set entry mode inc, no shift
195 #if !defined(XLCD_IS_BLOCKING)
196 while(XLCDIsBusy()); // Wait if LCD busy
197 #endif
198 XLCDCommand(SHIFT_CUR_LEFT); // Entry Mode
199
200 // Set DD Ram address to 0
201 #if !defined(XLCD_IS_BLOCKING)
202 while(XLCDIsBusy()); // Wait if LCD busy
203 #endif
204 XLCDCommand(0x80);
205
206 return;
207 }
208
209 /*********************************************************************
210 * Function: void XLCDCommand(unsigned char cmd)
211 *
212 * PreCondition: XLCDIsBusy() == FALSE if !defined(XLCD_IS_BLOCKING)
213 *
214 * Input: cmd - Command to be set to LCD.
215 *
216 * Output: None
217 *
218 * Side Effects: None
219 *
220 * Overview: None
221 *
222 * Note: None
223 ********************************************************************/
224 void XLCDCommand(unsigned char cmd)
225 {
226 #if defined(XLCD_IS_BLOCKING)
227 while(XLCDIsBusy()); // Wait if LCD busy
228 #endif
229
230 TRIS_RW = 0; // All control signals made outputs
231 TRIS_RS = 0;
232
233 #ifdef BIT8 // 8-bit interface
234 TRIS_DATA_PORT = 0; // Data port output
235 DATA_PORT = cmd; // Write command to data port
236 RW_PIN = 0; // Set the control signals
237 RS_PIN = 0; // for sending a command
238
239 XLCDDelay500ns();
240
241 E_PIN = 1; // Clock the command in
242 XLCDDelay500ns();
243 E_PIN = 0;
244 XLCDDelay500ns();
245 TRIS_DATA_PORT = 0xff; // Data port input
246 #else // 4-bit interface
247 #ifdef UPPER // Upper nibble interface
248 TRIS_DATA_PORT &= 0x0f;
249 DATA_PORT &= 0x0f;
250 DATA_PORT |= cmd&0xf0;
251 #else // Lower nibble interface
252 TRIS_DATA_PORT &= 0xf0;
253 DATA_PORT &= 0xf0;
254 DATA_PORT |= (cmd>>4);
255 #endif
256
257 RW_PIN = 0; // Set control signals for command
258 RS_PIN = 0;
259 XLCDDelay500ns();
260 E_PIN = 1; // Clock command in
261 XLCDDelay500ns();
262 E_PIN = 0;
263 #ifdef UPPER // Upper nibble interface
264 DATA_PORT &= 0x0f;
265 DATA_PORT |= (cmd<<4)&0xf0;
266 #else // Lower nibble interface
267 DATA_PORT &= 0xf0;
268 DATA_PORT |= cmd&0x0f;
269 #endif
270 XLCDDelay500ns();
271 E_PIN = 1; // Clock command in
272 XLCDDelay500ns();
273 E_PIN = 0;
274 #ifdef UPPER // Make data nibble input
275 TRIS_DATA_PORT |= 0xf0;
276 #else
277 TRIS_DATA_PORT |= 0x0f;
278 #endif
279 #endif
280 return;
281 }
282
283 /*********************************************************************
284 * Function: char XLCDIsBusy(void)
285 *
286 * PreCondition: None
287 *
288 * Input: None
289 *
290 * Output: non-zero if LCD controller is ready to accept new
291 * data or command
292 * zero otherwise.
293 *
294 * Side Effects: None
295 *
296 * Overview: None
297 *
298 * Note: None
299 ********************************************************************/
300 char XLCDIsBusy(void)
301 {
302 #if !defined(XLCD_READ_BACK)
303 int i = 0;
304
305 for ( i = 0; i < 500; i++ )
306 ;
307 return 0;
308 #endif
309
310 RW_PIN = 1; // Set the control bits for read
311 RS_PIN = 0;
312
313 TRIS_RW = 0; // All control signals made outputs
314 TRIS_RS = 0;
315
316 XLCDDelay500ns();
317 E_PIN = 1; // Clock in the command
318 XLCDDelay500ns();
319 #ifdef BIT8 // 8-bit interface
320 if(DATA_PORT&0x80) // Read bit 7 (busy bit)
321 { // If high
322 E_PIN = 0; // Reset clock line
323 RW_PIN = 0; // Reset control line
324 return 1; // Return TRUE
325 }
326 else // Bit 7 low
327 {
328 E_PIN = 0; // Reset clock line
329 RW_PIN = 0; // Reset control line
330 return 0; // Return FALSE
331 }
332 #else // 4-bit interface
333 #ifdef UPPER // Upper nibble interface
334 if(DATA_PORT&0x80)
335 #else // Lower nibble interface
336 if(DATA_PORT&0x08)
337 #endif
338 {
339 E_PIN = 0; // Reset clock line
340 XLCDDelay500ns();
341 E_PIN = 1; // Clock out other nibble
342 XLCDDelay500ns();
343 E_PIN = 0;
344 RW_PIN = 0; // Reset control line
345 return 1; // Return TRUE
346 }
347 else // Busy bit is low
348 {
349 E_PIN = 0; // Reset clock line
350 XLCDDelay500ns();
351 E_PIN = 1; // Clock out other nibble
352 XLCDDelay500ns();
353 E_PIN = 0;
354 RW_PIN = 0; // Reset control line
355 return 0; // Return FALSE
356 }
357 #endif
358 }
359
360 /*********************************************************************
361 * Function: unsigned char XLCDGetAddr(void)
362 *
363 * PreCondition: XLCDIsBusy() == FALSE && !defined(XLCD_IS_BLOCKING)
364 *
365 * Input: None
366 *
367 * Output: Current address byte from LCD
368 *
369 * Side Effects: None
370 *
371 * Overview: None
372 *
373 * Note: The address is read from the character generator
374 * RAM or display RAM depending on current setup.
375 ********************************************************************/
376 #if defined(XLCD_ENABLE_LCD_READS)
377 unsigned char XLCDGetAddr(void)
378 {
379 char data; // Holds the data retrieved from the LCD
380
381 TRIS_RW = 0; // All control signals made outputs
382 TRIS_RS = 0;
383
384 #if defined(XLCD_IS_BLOCKING)
385 while(XLCDIsBusy()); // Wait if LCD busy
386 #endif
387
388
389 #ifdef BIT8 // 8-bit interface
390 RW_PIN = 1; // Set control bits for the read
391 RS_PIN = 0;
392 XLCDDelay500ns();
393 E_PIN = 1; // Clock data out of the LCD controller
394 XLCDDelay500ns();
395 data = DATA_PORT; // Save the data in the register
396 E_PIN = 0;
397 RW_PIN = 0; // Reset the control bits
398 #else // 4-bit interface
399 RW_PIN = 1; // Set control bits for the read
400 RS_PIN = 0;
401 XLCDDelay500ns();
402 E_PIN = 1; // Clock data out of the LCD controller
403 XLCDDelay500ns();
404 #ifdef UPPER // Upper nibble interface
405 data = DATA_PORT&0xf0; // Read the nibble into the upper nibble of data
406 #else // Lower nibble interface
407 data = (DATA_PORT<<4)&0xf0; // Read the nibble into the upper nibble of data
408 #endif
409 E_PIN = 0; // Reset the clock
410 XLCDDelay500ns();
411 E_PIN = 1; // Clock out the lower nibble
412 XLCDDelay500ns();
413 #ifdef UPPER // Upper nibble interface
414 data |= (DATA_PORT>>4)&0x0f; // Read the nibble into the lower nibble of data
415 #else // Lower nibble interface
416 data |= DATA_PORT&0x0f; // Read the nibble into the lower nibble of data
417 #endif
418 E_PIN = 0;
419 RW_PIN = 0; // Reset the control lines
420 #endif
421 return (data&0x7f); // Return the address, Mask off the busy bit
422 }
423 #endif
424
425 /*********************************************************************
426 * Function: char XLCDGet(void)
427 *
428 * PreCondition: XLCDIsBusy() == FALSE && !defined(XLCD_IS_BLOCKING)
429 *
430 * Input: None
431 *
432 * Output: Current data byte from LCD
433 *
434 * Side Effects: None
435 *
436 * Overview: None
437 *
438 * Note: The data is read from the character generator
439 * RAM or display RAM depending on current setup.
440 ********************************************************************/
441 #if defined(XLCD_ENABLE_LCD_READS)
442 char XLCDGet(void)
443 {
444 char data;
445
446 TRIS_RW = 0; // All control signals made outputs
447 TRIS_RS = 0;
448
449 #if defined(XLCD_IS_BLOCKING)
450 while(XLCDIsBusy()); // Wait if LCD busy
451 #endif
452
453 #ifdef BIT8 // 8-bit interface
454 RS_PIN = 1; // Set the control bits
455 RW_PIN = 1;
456 XLCDDelay500ns();
457 E_PIN = 1; // Clock the data out of the LCD
458 XLCDDelay500ns();
459 data = DATA_PORT; // Read the data
460 E_PIN = 0;
461 RS_PIN = 0; // Reset the control bits
462 RW_PIN = 0;
463 #else // 4-bit interface
464 RW_PIN = 1;
465 RS_PIN = 1;
466 XLCDDelay500ns();
467 E_PIN = 1; // Clock the data out of the LCD
468 XLCDDelay500ns();
469 #ifdef UPPER // Upper nibble interface
470 data = DATA_PORT&0xf0; // Read the upper nibble of data
471 #else // Lower nibble interface
472 data = (DATA_PORT<<4)&0xf0; // read the upper nibble of data
473 #endif
474 E_PIN = 0; // Reset the clock line
475 XLCDDelay500ns();
476 E_PIN = 1; // Clock the next nibble out of the LCD
477 XLCDDelay500ns();
478 #ifdef UPPER // Upper nibble interface
479 data |= (DATA_PORT>>4)&0x0f; // Read the lower nibble of data
480 #else // Lower nibble interface
481 data |= DATA_PORT&0x0f; // Read the lower nibble of data
482 #endif
483 E_PIN = 0;
484 RS_PIN = 0; // Reset the control bits
485 RW_PIN = 0;
486 #endif
487 return(data); // Return the data byte
488 }
489 #endif
490
491
492 void XLCDPutString(char *string)
493 {
494 char v;
495
496 while( v = *string )
497 {
498 #if !defined(XLCD_IS_BLOCKING)
499 while(XLCDIsBusy()); // Wait if LCD busy
500 #endif
501 XLCDPut(v);
502 string++;
503 }
504 }
505
506 void XLCDPutROMString(ROM char *string)
507 {
508 char v;
509
510 while( v = *string )
511 {
512 #if !defined(XLCD_IS_BLOCKING)
513 while(XLCDIsBusy()); // Wait if LCD busy
514 #endif
515 XLCDPut(v);
516 string++;
517 }
518 }
519
520
521 /*********************************************************************
522 * Function: void XLCDPut(char data)
523 *
524 * PreCondition: XLCDInit() is already called AND
525 * (XLCDIsBusy() == FALSE AND !defined(XLCD_IS_BLOCKING)
526 *
527 * Input: data - Data to be written
528 *
529 * Output: None
530 *
531 * Side Effects: None
532 *
533 * Overview: None
534 *
535 * Note: Data is written to character generator RAM or
536 * display data RAM depending on how the access is
537 * setup.
538 ********************************************************************/
539 void XLCDPut(char data)
540 {
541 #if defined(XLCD_IS_BLOCKING)
542 while(XLCDIsBusy()); // Wait if LCD busy
543 #endif
544
545 TRIS_RW = 0; // All control signals made outputs
546 TRIS_RS = 0;
547
548 #ifdef BIT8 // 8-bit interface
549 TRIS_DATA_PORT = 0; // Make port output
550 DATA_PORT = data; // Write data to port
551 RS_PIN = 1; // Set control bits
552 RW_PIN = 0;
553 XLCDDelay500ns();
554 E_PIN = 1; // Clock data into LCD
555 XLCDDelay500ns();
556 E_PIN = 0;
557 RS_PIN = 0; // Reset control bits
558 TRIS_DATA_PORT = 0xff; // Make port input
559 #else // 4-bit interface
560 #ifdef UPPER // Upper nibble interface
561 TRIS_DATA_PORT &= 0x0f;
562 DATA_PORT &= 0x0f;
563 DATA_PORT |= data&0xf0;
564 #else // Lower nibble interface
565 TRIS_DATA_PORT &= 0xf0;
566 DATA_PORT &= 0xf0;
567 DATA_PORT |= ((data>>4)&0x0f);
568 #endif
569 RS_PIN = 1; // Set control bits
570 RW_PIN = 0;
571 XLCDDelay500ns();
572 E_PIN = 1; // Clock nibble into LCD
573 XLCDDelay500ns();
574 E_PIN = 0;
575 #ifdef UPPER // Upper nibble interface
576 DATA_PORT &= 0x0f;
577 DATA_PORT |= ((data<<4)&0xf0);
578 #else // Lower nibble interface
579 DATA_PORT &= 0xf0;
580 DATA_PORT |= (data&0x0f);
581 #endif
582 XLCDDelay500ns();
583 E_PIN = 1; // Clock nibble into LCD
584 XLCDDelay500ns();
585 E_PIN = 0;
586 #ifdef UPPER // Upper nibble interface
587 TRIS_DATA_PORT |= 0xf0;
588 #else // Lower nibble interface
589 TRIS_DATA_PORT |= 0x0f;
590 #endif
591 #endif
592 return;
593 }
594

  ViewVC Help
Powered by ViewVC 1.1.20