LTlib LaurTec Library  4.0.1
Open Source C Library for Microchip Microcontrollers based on XC8 Compiler
 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 #endif
41 
42 #include "LCD_44780.h"
43 
44 
45 //************************************************************
46 // enable_pulse_LCD Implementation
47 //************************************************************
48 void LCD_enable_pulse (void) {
49 
50  LCD_E = 1;
51  delay_ms (1);
52  LCD_E = 0;
53  delay_ms (1);
54 }
55 
56 
57 //************************************************************
58 // send_command_LCD Implementation
59 //************************************************************
60 void LCD_send_command (unsigned char D3, unsigned char D2, unsigned char D1, unsigned char D0) {
61 
62  LCD_D0 = D0;
63  LCD_D1 = D1;
64  LCD_D2 = D2;
65  LCD_D3 = D3;
67 }
68 
69 
70 //************************************************************
71 // home_LCD Implementation
72 //************************************************************
73 void LCD_home(void) {
74 
75  LCD_send_command (0,0,0,0);
76  LCD_send_command (0,0,1,0);
77 }
78 
79 
80 //************************************************************
81 // shift_LCD Implementation
82 //************************************************************
83 void LCD_shift (unsigned char shift, unsigned char number_of_shift) {
84 
85  unsigned char i;
86 
87  for (i=0; i < number_of_shift; i++) {
88  LCD_send_command (0,0,0,1);
89  LCD_send_command (1,shift,0,0);
90  }
91 }
92 
93 
94 //************************************************************
95 // shift_cursor_LCD Implementation
96 //************************************************************
97 void LCD_shift_cursor (unsigned char shift, unsigned char number_of_shift){
98 
99  unsigned char i;
100 
101  for (i=0; i < number_of_shift; i++) {
102  LCD_send_command (0,0,0,1);
103  LCD_send_command (0,shift,0,0);
104  }
105 }
106 
107 //************************************************************
108 // goto_line_LCD Implementation
109 //************************************************************
110 void LCD_goto_line (unsigned char line) {
111 
112 switch(line) {
113 
114  case 1: LCD_send_command(1,0,0,0);
115  LCD_send_command(0,0,0,0);
116  break;
117 
118  case 2: LCD_send_command(1,1,0,0);
119  LCD_send_command(0,0,0,0);
120  break;
121 
122  case 3: LCD_send_command(1,0,0,1);
123  LCD_send_command(0,1,0,0);
124  break;
125 
126  case 4: LCD_send_command(1,1,0,1);
127  LCD_send_command(0,1,0,0);
128  }
129 }
130 
131 
132 //************************************************************
133 // goto_xy_LCD Implementation
134 //************************************************************
135 void LCD_goto_xy (unsigned char x, unsigned char y){
136 
137  LCD_goto_line (y);
138  LCD_shift_cursor (RIGHT, x-1);
139 }
140 
141 //************************************************************
142 // write_char_LCD Implementation
143 //************************************************************
144 void LCD_write_char (unsigned char value) {
145 
146  unsigned char D3,D2,D1,D0;
147 
148  LCD_RS = 1;
149 
150  // Splitting of the first nibble
151  D3 = (value & 0b10000000) >> 7;
152  D2 = (value & 0b01000000) >> 6;
153  D1 = (value & 0b00100000) >> 5;
154  D0 = (value & 0b00010000) >> 4;
155 
156  LCD_send_command (D3,D2,D1,D0);
157 
158  // Splitting of the second nibble
159  D3 = (value & 0b00001000) >> 3;
160  D2 = (value & 0b00000100) >> 2;
161  D1 = (value & 0b00000010) >> 1;
162  D0 = (value & 0b00000001);
163 
164  LCD_send_command (D3,D2,D1,D0);
165 
166  LCD_RS = 0;
167 }
168 
169 //************************************************************
170 // write_message_LCD Implementation
171 //************************************************************
172 #ifndef __XC8
173 void LCD_write_message (const rom unsigned char *buffer) {
174 #endif
175 
176 #ifdef __XC8
177 void LCD_write_message (const unsigned char *buffer) {
178 #endif
179  // Write data to LCD up to null
180  while(*buffer) {
181 
182  // Write character to LCD
183  LCD_write_char(*buffer);
184  // Increment buffer
185  buffer++;
186  }
187 }
188 
189 
190 //************************************************************
191 // write_string_LCD Implementation
192 //************************************************************
193 void LCD_write_string (unsigned char *buffer) {
194 
195  // Write data to LCD up to null
196  while(*buffer){
197 
198  // Write character to LCD
199  LCD_write_char(*buffer);
200  // Increment buffer
201  buffer++;
202  }
203 }
204 
205 
206 //************************************************************
207 // write_integer_LCD Implementation
208 //************************************************************
209 void LCD_write_integer (signed int value, unsigned char number_of_digits, unsigned char zero_cleaning){
210 
211  // The array size is 5 plus end of string \0
212  unsigned char convertedInt [6] = {0,0,0,0,0,0};
213 
214  // Index used to shift to the right the digit
215  unsigned char index;
216 
217  // Integer is converted to string
218  #ifndef __XC8
219  itoa (value, (unsigned char*) convertedInt);
220  #endif
221 
222  #ifdef __XC8
223  itoa ((unsigned char*) convertedInt, value,10);
224  #endif
225 
226 
227  if (number_of_digits >0 ) {
228 
229  convertedInt[number_of_digits] = '\0';
230 
231  // Shift the digit to the right removing the empty one
232  //while (!isdigit(convertedInt[number_of_digits-1]))
233  while (!(convertedInt[number_of_digits-1] <= '9' && convertedInt[number_of_digits-1] >= '0')){
234 
235  for (index = number_of_digits-1; index > 0; index--){
236  convertedInt[index] = convertedInt[index-1];
237 
238  if (zero_cleaning == ZERO_CLEANING_ON) {
239  convertedInt[index-1] = ' ';
240  } else {
241  convertedInt[index-1] = '0';
242  }
243 
244  }
245  }
246  }
247 
248  LCD_write_string (convertedInt);
249 
250 }
251 
252 
253 //************************************************************
254 // clear_LCD Implementation
255 //************************************************************
256 void LCD_clear (void){
257 
258  LCD_send_command (0,0,0,0);
259  LCD_send_command (0,0,0,1);
260 }
261 
262 //************************************************************
263 // cursor_LCD Implementation
264 //************************************************************
265 void LCD_cursor (unsigned char active, unsigned char blinking) {
266 
267  LCD_send_command (0,0,0,0);
268  LCD_send_command (1,1,active,blinking);
269 }
270 
271 //************************************************************
272 // back_light_LCD Implementation
273 //************************************************************
274 void LCD_backlight (unsigned char active) {
275 
276  LCD_LED = active;
277 }
278 
279 //************************************************************
280 // initialize_LCD Implementation
281 //************************************************************
282 void LCD_initialize (unsigned char quartz_frequency) {
283 
284  //IO pin initialization
293 
294 
295  delay_set_quartz (quartz_frequency);
296 
297  LCD_RS = 0x00;
298  LCD_E = 0x00;
299  LCD_RW = 0x00;
300 
301  delay_ms (100);
302  LCD_send_command (0,0,1,1);
303  delay_ms (100);
304  LCD_send_command (0,0,1,1);
305  delay_ms (10);
306  LCD_send_command (0,0,1,1);
307 
308  LCD_send_command (0,0,1,0);
309 
310  LCD_send_command (0,0,1,0);
311  LCD_send_command (1,0,0,0);
312 
313  LCD_send_command (0,0,0,0);
314  LCD_send_command (1,0,0,0);
315 
316  LCD_send_command (0,0,0,0);
317  LCD_send_command (0,0,0,1);
318 
319  LCD_send_command (0,0,0,0);
320  LCD_send_command (0,1,1,0);
321 
322  LCD_clear ();
323 
324  LCD_cursor (0,0);
325 
326 }
327