PIC18 LaurTec Library  3.3.1
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.7
5 
6 Created on Date : 04/09/2006
7 Last update : 15/12/2014
8 
9 CopyRight 2006-2014 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 acknowledgement 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 LCD_enable_pulse (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 LCD_send_command (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 LCD_home(void) {
75 
76  LCD_send_command (0,0,0,0);
77  LCD_send_command (0,0,1,0);
78 }
79 
80 
81 //************************************************************
82 // shift_LCD Implementation
83 //************************************************************
84 void LCD_shift (unsigned char shift, unsigned char number_of_shift) {
85 
86  unsigned char i;
87 
88  for (i=0; i < number_of_shift; i++) {
89  LCD_send_command (0,0,0,1);
90  LCD_send_command (1,shift,0,0);
91  }
92 }
93 
94 
95 //************************************************************
96 // shift_cursor_LCD Implementation
97 //************************************************************
98 void LCD_shift_cursor (unsigned char shift, unsigned char number_of_shift){
99 
100  unsigned char i;
101 
102  for (i=0; i < number_of_shift; i++) {
103  LCD_send_command (0,0,0,1);
104  LCD_send_command (0,shift,0,0);
105  }
106 }
107 
108 //************************************************************
109 // goto_line_LCD Implementation
110 //************************************************************
111 void LCD_goto_line (unsigned char line) {
112 
113 switch(line) {
114 
115  case 1: LCD_send_command(1,0,0,0);
116  LCD_send_command(0,0,0,0);
117  break;
118 
119  case 2: LCD_send_command(1,1,0,0);
120  LCD_send_command(0,0,0,0);
121  break;
122 
123  case 3: LCD_send_command(1,0,0,1);
124  LCD_send_command(0,1,0,0);
125  break;
126 
127  case 4: LCD_send_command(1,1,0,1);
128  LCD_send_command(0,1,0,0);
129  }
130 }
131 
132 
133 //************************************************************
134 // goto_xy_LCD Implementation
135 //************************************************************
136 void LCD_goto_xy (unsigned char x, unsigned char y){
137 
138  LCD_goto_line (y);
139  LCD_shift_cursor (RIGHT, x-1);
140 }
141 
142 //************************************************************
143 // write_char_LCD Implementation
144 //************************************************************
145 void LCD_write_char (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  LCD_send_command (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  LCD_send_command (D3,D2,D1,D0);
166 
167  LCD_RS = 0;
168 }
169 
170 //************************************************************
171 // write_message_LCD Implementation
172 //************************************************************
173 #ifndef __XC8
174 void LCD_write_message (const rom unsigned char *buffer) {
175 #endif
176 
177 #ifdef __XC8
178 void LCD_write_message (const unsigned char *buffer) {
179 #endif
180  // Write data to LCD up to null
181  while(*buffer) {
182 
183  // Write character to LCD
184  LCD_write_char(*buffer);
185  // Increment buffer
186  buffer++;
187  }
188 }
189 
190 
191 //************************************************************
192 // write_string_LCD Implementation
193 //************************************************************
194 void LCD_write_string (unsigned char *buffer) {
195 
196  // Write data to LCD up to null
197  while(*buffer){
198 
199  // Write character to LCD
200  LCD_write_char(*buffer);
201  // Increment buffer
202  buffer++;
203  }
204 }
205 
206 
207 //************************************************************
208 // write_integer_LCD Implementation
209 //************************************************************
210 void LCD_write_integer (signed int value, unsigned char number_of_digits, unsigned char zero_cleaning){
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 
239  if (zero_cleaning == ZERO_CLEANING_ON) {
240  convertedInt[index-1] = ' ';
241  } else {
242  convertedInt[index-1] = '0';
243  }
244 
245  }
246  }
247  }
248 
249  LCD_write_string (convertedInt);
250 
251 }
252 
253 
254 //************************************************************
255 // clear_LCD Implementation
256 //************************************************************
257 void LCD_clear (void){
258 
259  LCD_send_command (0,0,0,0);
260  LCD_send_command (0,0,0,1);
261 }
262 
263 //************************************************************
264 // cursor_LCD Implementation
265 //************************************************************
266 void LCD_cursor (unsigned char active, unsigned char blinking) {
267 
268  LCD_send_command (0,0,0,0);
269  LCD_send_command (1,1,active,blinking);
270 }
271 
272 //************************************************************
273 // back_light_LCD Implementation
274 //************************************************************
275 void LCD_backlight (unsigned char active) {
276 
277  LCD_LED = active;
278 }
279 
280 //************************************************************
281 // initialize_LCD Implementation
282 //************************************************************
283 void LCD_initialize (unsigned char quartz_frequency) {
284 
285  delay_set_quartz (quartz_frequency);
286 
287  LCD_RS = 0x00;
288  LCD_E = 0x00;
289  LCD_RW = 0x00;
290 
291  delay_ms (100);
292  LCD_send_command (0,0,1,1);
293  delay_ms (100);
294  LCD_send_command (0,0,1,1);
295  delay_ms (10);
296  LCD_send_command (0,0,1,1);
297 
298  LCD_send_command (0,0,1,0);
299 
300  LCD_send_command (0,0,1,0);
301  LCD_send_command (1,0,0,0);
302 
303  LCD_send_command (0,0,0,0);
304  LCD_send_command (1,0,0,0);
305 
306  LCD_send_command (0,0,0,0);
307  LCD_send_command (0,0,0,1);
308 
309  LCD_send_command (0,0,0,0);
310  LCD_send_command (0,1,1,0);
311 
312  LCD_clear ();
313 
314  LCD_cursor (0,0);
315 
316 }
317