PIC18 LaurTec Library  3.1.2
Open Source C Library for PIC18 Microcontrollers based on C18 - XC8 Compilers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
LCD_44780.c
Go to the documentation of this file.
1 /*******************************************************************************
2 
3 Author : Mauro Laurenti
4 Version : 1.6
5 
6 Created on Date : 04/09/2006
7 Last update : 01/11/2013
8 
9 CopyRight 2006-2013 all rights are reserved
10 
11 ********************************************************
12 SOFTWARE LICENSE AGREEMENT
13 ********************************************************
14 
15 The usage of the supplied software imply the acceptance of the following license.
16 
17 The software supplied herewith by Mauro Laurenti (the Author) is intended for
18 use solely and exclusively on Microchip PIC Microcontroller (registered mark).
19 The software is owned by the Author, and is protected under applicable
20 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 civil liability
23 for the breach of the terms and conditions of this license.
24 Commercial use is forbidden without a written acknowledgment with the Author.
25 Personal or educational use is allowed if the application containing the
26 following software doesn't aim to commercial use or monetary earning of any kind.
27 
28 THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
29 WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
30 TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
31 PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE AUTHOR SHALL NOT,
32 IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
33 CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
34 
35 *******************************************************************************/
36 
37 
38 #ifdef __XC8
39 #include <xc.h>
40 #include <stdlib.h>
41 #endif
42 
43 #include "LCD_44780.h"
44 
45 
46 //************************************************************
47 // enable_pulse_LCD Implementation
48 //************************************************************
49 void enable_pulse_LCD (void) {
50 
51  LCD_E = 1;
52  delay_ms (1);
53  LCD_E = 0;
54  delay_ms (1);
55 }
56 
57 
58 //************************************************************
59 // send_command_LCD Implementation
60 //************************************************************
61 void send_command_LCD (unsigned char D3, unsigned char D2, unsigned char D1, unsigned char D0) {
62 
63  LCD_D0 = D0;
64  LCD_D1 = D1;
65  LCD_D2 = D2;
66  LCD_D3 = D3;
68 }
69 
70 
71 //************************************************************
72 // home_LCD Implementation
73 //************************************************************
74 void home_LCD(void) {
75 
76  send_command_LCD (0,0,0,0);
77  send_command_LCD (0,0,1,0);
78 }
79 
80 
81 //************************************************************
82 // shift_LCD Implementation
83 //************************************************************
84 void shift_LCD(unsigned char shift, unsigned char number_of_shift) {
85 
86  unsigned char i;
87 
88  for (i=0; i < number_of_shift; i++) {
89  send_command_LCD (0,0,0,1);
90  send_command_LCD (1,shift,0,0);
91  }
92 }
93 
94 
95 //************************************************************
96 // shift_cursor_LCD Implementation
97 //************************************************************
98 void shift_cursor_LCD(unsigned char shift, unsigned char number_of_shift){
99 
100  unsigned char i;
101 
102  for (i=0; i < number_of_shift; i++) {
103  send_command_LCD (0,0,0,1);
104  send_command_LCD (0,shift,0,0);
105  }
106 }
107 
108 //************************************************************
109 // goto_line_LCD Implementation
110 //************************************************************
111 void goto_line_LCD (unsigned char line) {
112 
113 switch(line) {
114 
115  case 1: send_command_LCD(1,0,0,0);
116  send_command_LCD(0,0,0,0);
117  break;
118 
119  case 2: send_command_LCD(1,1,0,0);
120  send_command_LCD(0,0,0,0);
121  break;
122 
123  case 3: send_command_LCD(1,0,0,1);
124  send_command_LCD(0,1,0,0);
125  break;
126 
127  case 4: send_command_LCD(1,1,0,1);
128  send_command_LCD(0,1,0,0);
129  }
130 }
131 
132 
133 //************************************************************
134 // goto_xy_LCD Implementation
135 //************************************************************
136 void goto_xy_LCD (unsigned char x, unsigned char y){
137 
138  goto_line_LCD (y);
139  shift_cursor_LCD (RIGHT, x-1);
140 }
141 
142 //************************************************************
143 // write_char_LCD Implementation
144 //************************************************************
145 void write_char_LCD (unsigned char value) {
146 
147  unsigned char D3,D2,D1,D0;
148 
149  LCD_RS = 1;
150 
151  // Splitting of the first nibble
152  D3 = (value & 0b10000000) >> 7;
153  D2 = (value & 0b01000000) >> 6;
154  D1 = (value & 0b00100000) >> 5;
155  D0 = (value & 0b00010000) >> 4;
156 
157  send_command_LCD (D3,D2,D1,D0);
158 
159  // Splitting of the second nibble
160  D3 = (value & 0b00001000) >> 3;
161  D2 = (value & 0b00000100) >> 2;
162  D1 = (value & 0b00000010) >> 1;
163  D0 = (value & 0b00000001);
164 
165  send_command_LCD (D3,D2,D1,D0);
166 
167  LCD_RS = 0;
168 }
169 
170 //************************************************************
171 // write_message_LCD Implementation
172 //************************************************************
173 #ifndef __XC8
174 void write_message_LCD(const rom unsigned char *buffer) {
175 #endif
176 
177 #ifdef __XC8
178 void write_message_LCD(const unsigned char *buffer) {
179 #endif
180  // Write data to LCD up to null
181  while(*buffer) {
182 
183  // Write character to LCD
184  write_char_LCD(*buffer);
185  // Increment buffer
186  buffer++;
187  }
188 }
189 
190 
191 //************************************************************
192 // write_string_LCD Implementation
193 //************************************************************
194 void write_string_LCD(unsigned char *buffer) {
195 
196  // Write data to LCD up to null
197  while(*buffer){
198 
199  // Write character to LCD
200  write_char_LCD(*buffer);
201  // Increment buffer
202  buffer++;
203  }
204 }
205 
206 
207 //************************************************************
208 // write_integer_LCD Implementation
209 //************************************************************
210 void write_integer_LCD(int value, unsigned char number_of_digits){
211 
212  // The array size is 5 plus end of string \0
213  unsigned char convertedInt [6] = {0,0,0,0,0,0};
214 
215  // Index used to shift to the right the digit
216  unsigned char index;
217 
218  // Integer is converted to string
219  #ifndef __XC8
220  itoa (value, (unsigned char*) convertedInt);
221  #endif
222 
223  #ifdef __XC8
224  itoa ((unsigned char*) convertedInt, value,10);
225  #endif
226 
227 
228  if (number_of_digits >0 ) {
229 
230  convertedInt[number_of_digits] = '\0';
231 
232  // Shift the digit to the right removing the empty one
233  //while (!isdigit(convertedInt[number_of_digits-1]))
234  while (!(convertedInt[number_of_digits-1] <= '9' && convertedInt[number_of_digits-1] >= '0')){
235 
236  for (index = number_of_digits-1; index > 0; index--){
237  convertedInt[index] = convertedInt[index-1];
238  convertedInt[index-1] = ' ';
239  }
240  }
241  }
242 
243  write_string_LCD (convertedInt);
244 
245 }
246 
247 
248 //************************************************************
249 // clear_LCD Implementation
250 //************************************************************
251 void clear_LCD (void){
252 
253  send_command_LCD (0,0,0,0);
254  send_command_LCD (0,0,0,1);
255 }
256 
257 //************************************************************
258 // cursor_LCD Implementation
259 //************************************************************
260 void cursor_LCD(unsigned char active, unsigned char blinking) {
261 
262  send_command_LCD (0,0,0,0);
263  send_command_LCD (1,1,active,blinking);
264 }
265 
266 //************************************************************
267 // back_light_LCD Implementation
268 //************************************************************
269 void backlight_LCD (unsigned char active) {
270 
271  LCD_LED = active;
272 }
273 
274 //************************************************************
275 // initialize_LCD Implementation
276 //************************************************************
277 void initialize_LCD(unsigned char quartz_frequency) {
278 
279  setQuartz (quartz_frequency);
280 
281  LCD_RS = 0x00;
282  LCD_E = 0x00;
283  LCD_RW = 0x00;
284 
285  delay_ms (100);
286  send_command_LCD (0,0,1,1);
287  delay_ms (100);
288  send_command_LCD (0,0,1,1);
289  delay_ms (10);
290  send_command_LCD (0,0,1,1);
291 
292  send_command_LCD (0,0,1,0);
293 
294  send_command_LCD (0,0,1,0);
295  send_command_LCD (1,0,0,0);
296 
297  send_command_LCD (0,0,0,0);
298  send_command_LCD (1,0,0,0);
299 
300  send_command_LCD (0,0,0,0);
301  send_command_LCD (0,0,0,1);
302 
303  send_command_LCD (0,0,0,0);
304  send_command_LCD (0,1,1,0);
305 
306  clear_LCD ();
307 
308  cursor_LCD (0,0);
309 
310 }
311