1 |
1: /*
|
2 |
2: * LCD interface example
|
3 |
3: * Uses routines from delay.c
|
4 |
4: * This code will interface to a standard LCD controller
|
5 |
5: * like the Hitachi HD44780. It uses it in 4 or 8 bit mode
|
6 |
6: *
|
7 |
7: */
|
8 |
8:
|
9 |
9: #include <pic18.h>
|
10 |
10: #include "lcd.h"
|
11 |
11: #include "delay.h"
|
12 |
12:
|
13 |
13:
|
14 |
14: static bit fourbit; // four or eight bit mode?
|
15 |
15:
|
16 |
16: #ifdef CHECKBUSY
|
17 |
17:
|
18 |
18: unsigned char
|
19 |
19: lcd_read_cmd_nowait(void)
|
20 |
20: {
|
21 |
21: unsigned char c, readc;
|
22 |
22:
|
23 |
23: LCD_DATA_TRIS = INPUT_DATA;
|
24 |
24:
|
25 |
25: LCD_RW = 1; // Read LCD
|
26 |
26: asm("nop"); // short propagation delay
|
27 |
27: asm("nop"); // short propagation delay
|
28 |
28:
|
29 |
29: if (fourbit)
|
30 |
30: {
|
31 |
31: LCD_STROBE_READ(readc); // Read high nibble
|
32 |
32: // Move 4 bits to high nibble while zeroing low nibble
|
33 |
33: c = ( ( readc << 4 ) & 0xF0 );
|
34 |
34: LCD_STROBE_READ(readc); // Read low nibble
|
35 |
35: c |= ( readc & 0x0F ); // Or in 4 more bits to low nibble
|
36 |
36: }
|
37 |
37: else
|
38 |
38: {
|
39 |
39: LCD_STROBE_READ(readc);
|
40 |
40: c = readc;
|
41 |
41: }
|
42 |
42: LCD_RW = 0; // Return to default mode of writing LCD
|
43 |
43: LCD_DATA_TRIS = OUTPUT_DATA; // Return to default mode of writing LCD
|
44 |
44:
|
45 |
45: return(c);
|
46 |
46: }
|
47 |
47:
|
48 |
48: void
|
49 |
49: lcd_check_busy(void) // Return when the LCD is no longer busy, or we've waiting long enough!
|
50 |
50: {
|
51 |
51: // To avoid hanging forever in event there's a bad or
|
52 |
52: // missing LCD on hardware. Will just run SLOW, but still run.
|
53 |
53: unsigned int retry;
|
54 |
54: unsigned char c;
|
55 |
55:
|
56 |
56: for (retry=1000; retry-- > 0; ) {
|
57 |
57: c = lcd_read_cmd_nowait();
|
58 |
58: if (0==(c&0x80)) break; // Check busy bit. If zero, no longer busy
|
59 |
59: }
|
60 |
60: }
|
61 |
61:
|
62 |
62: #endif
|
63 |
63:
|
64 |
64: /* send a command to the LCD */
|
65 |
65: void
|
66 |
66: lcd_cmd(unsigned char c)
|
67 |
67: {
|
68 |
68: LCD_WAIT; // may check LCD busy flag, or just delay a little, depending on lcd.h
|
69 |
69:
|
70 |
70: if (fourbit)
|
71 |
71: {
|
72 |
72: LCD_DATA = ( ( c >> 4 ) & 0x0F );
|
73 |
73: LCD_STROBE();
|
74 |
74: LCD_DATA = ( c & 0x0F );
|
75 |
75: LCD_STROBE();
|
76 |
76: }
|
77 |
77: else
|
78 |
78: {
|
79 |
79: LCD_DATA = c;
|
80 |
80: LCD_STROBE();
|
81 |
81: }
|
82 |
82: }
|
83 |
83:
|
84 |
84: /* send data to the LCD */
|
85 |
85: void
|
86 |
86: lcd_data(unsigned char c)
|
87 |
87: {
|
88 |
88: LCD_WAIT; // may check LCD busy flag, or just delay a little, depending on lcd.h
|
89 |
89:
|
90 |
90: LCD_DATA = 0;
|
91 |
91: LCD_RS = 1;
|
92 |
92: if (fourbit)
|
93 |
93: {
|
94 |
94: LCD_DATA |= ( ( c >> 4 ) & 0x0F );
|
95 |
95: LCD_STROBE();
|
96 |
96: LCD_DATA &= 0xF0;
|
97 |
97: LCD_DATA |= ( c & 0x0F );
|
98 |
98: LCD_STROBE();
|
99 |
99: }
|
100 |
100: else
|
101 |
101: {
|
102 |
102: LCD_DATA = c;
|
103 |
103: LCD_STROBE();
|
104 |
104: }
|
105 |
105: LCD_RS = 0;
|
106 |
106: }
|
107 |
107:
|
108 |
108: /* write a string of chars to the LCD */
|
109 |
109:
|
110 |
110: void
|
111 |
111: lcd_puts(const char * s)
|
112 |
112: {
|
113 |
113: while(*s)
|
114 |
114: lcd_data(*s++);
|
115 |
115: }
|
116 |
116:
|
117 |
117: /* initialize the LCD */
|
118 |
118: void
|
119 |
119: lcd_init(unsigned char mode)
|
120 |
120: {
|
121 |
121: char init_value;
|
122 |
122:
|
123 |
123: fourbit = 0;
|
124 |
124: if (mode == FOURBIT_MODE){
|
125 |
125: fourbit = 1;
|
126 |
126: init_value = 0x3;
|
127 |
127: }else{
|
128 |
128: init_value = 0x3F;
|
129 |
129: }
|
130 |
130: LCD_RS = 0;
|
131 |
131: LCD_EN = 0;
|
132 |
132: LCD_RW = 0;
|
133 |
133: LCD_RS_TRIS = OUTPUT_PIN;
|
134 |
134: LCD_EN_TRIS = OUTPUT_PIN;
|
135 |
135: LCD_RW_TRIS = OUTPUT_PIN;
|
136 |
136: LCD_DATA_TRIS = OUTPUT_DATA;
|
137 |
137: DelayMs(15);
|
138 |
138: LCD_DATA = init_value;
|
139 |
139: LCD_STROBE();
|
140 |
140: DelayMs(5);
|
141 |
141: LCD_DATA = init_value;
|
142 |
142: LCD_STROBE();
|
143 |
143: DelayUs(200);
|
144 |
144: LCD_DATA = init_value;
|
145 |
145: LCD_STROBE();
|
146 |
146:
|
147 |
147: if (fourbit){
|
148 |
148: LCD_WAIT; //may check LCD busy flag, or just delay a little, depending on lcd.h
|
149 |
149: LCD_DATA = 0x2; // Set 4-bit mode
|
150 |
150: LCD_STROBE();
|
151 |
151:
|
152 |
152: lcd_cmd(0x28); // Function Set
|
153 |
153: }else{
|
154 |
154: lcd_cmd(0x38);
|
155 |
155: }
|
156 |
156: lcd_cmd(0xF); //Display On, Cursor On, Cursor Blink
|
157 |
157: lcd_cmd(0x1); //Display Clear
|
158 |
158: lcd_cmd(0x6); //Entry Mode
|
159 |
159: lcd_cmd(0x80); //Initialize DDRAM address to zero
|
160 |
160: }
|
161 |
161:
|
162 |
162:
|
163 |
163:
|