LTlib LaurTec Library  4.0.0 Beta
Open Source C Library for Microchip Microcontrollers based on XC8 Compiler
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
LCD_44780_I2C.c
Go to the documentation of this file.
1 /*******************************************************************************
2 
3 Author : Mauro Laurenti
4 Version : 1.5
5 Created on Date : 09/03/2012
6 Last update : 23/09/2015
7 
8 CopyRight 2006-2015 all rights are reserved
9 
10 ********************************************************
11 SOFTWARE LICENSE AGREEMENT
12 ********************************************************
13 
14 The usage of the supplied software imply the acceptance of the following license.
15 
16 The software supplied herewith by Mauro Laurenti (the Author) is intended for
17 use solely and exclusively on Microchip PIC Microcontroller (registered mark).
18 The software is owned by the Author, and is protected under applicable
19 copyright laws. All rights are reserved.
20 Any use in violation of the foregoing restrictions may subject the
21 user to criminal sanctions under applicable laws, as well as to civil liability
22 for the breach of the terms and conditions of this license.
23 Commercial use is forbidden without a written acknowledgement with the Author.
24 Personal or educational use is allowed if the application containing the
25 following software doesn't aim to commercial use or monetary earning of any kind.
26 
27 THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
28 WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
29 TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
30 PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE AUTHOR SHALL NOT,
31 IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
32 CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
33 
34 *******************************************************************************/
35 
36 #ifdef __XC8
37  #include <xc.h>
38  #include <stdlib.h>
39 #endif
40 
41 #include "LCD_44780_I2C.h"
42 
43 unsigned char data_buffer = 0;
44 unsigned char I2C_device_address = 0;
45 
46 //************************************************************
47 // LCD_enable_pulse Implementation
48 //************************************************************
49 void LCD_enable_pulse (void) {
51 
52  #ifdef PCF8574_GENERIC
54  #endif
55 
56  #ifdef MCP23008
58  #endif
59 
60 
61  delay_ms (1);
62 
64 
65  #ifdef PCF8574_GENERIC
67  #endif
68 
69  #ifdef MCP23008
71  #endif
72 }
73 
74 
75 //************************************************************
76 // LCD_send_command Implementation
77 //************************************************************
78 void LCD_send_command (unsigned char data) {
79 
80 
81  //clear the lower nible
82  data_buffer = data_buffer & 0b11100000;
83 
84  //write the data within the buffer
85  data_buffer = data_buffer | data;
86 
87  #ifdef PCF8574_GENERIC
89  #endif
90 
91  #ifdef MCP23008
93  #endif
94 
96 }
97 
98 
99 //************************************************************
100 // LCD_home Implementation
101 //************************************************************
102 void LCD_home (void) {
103 
104  LCD_send_command (0b00000000);
105  LCD_send_command (0b00000010);
106 }
107 
108 
109 //************************************************************
110 // LCD_shift Implementation
111 //************************************************************
112 void LCD_shift (unsigned char shift, unsigned char number_of_shift) {
113 
114  unsigned char i;
115 
116  for (i=0; i < number_of_shift; i++) {
117  LCD_send_command (0b00000001);
118  LCD_send_command (0b00001000 | shift);
119  }
120 }
121 
122 
123 //************************************************************
124 // LCD_shift_cursor Implementation
125 //************************************************************
126 void LCD_shift_cursor (unsigned char shift, unsigned char number_of_shift){
127 
128  unsigned char i;
129 
130  for (i=0; i < number_of_shift; i++) {
131  LCD_send_command (0b00000001);
132  LCD_send_command (0b00000000 | shift);
133  }
134 }
135 
136 //************************************************************
137 // LCD_goto_line Implementation
138 //************************************************************
139 void LCD_goto_line (unsigned char line) {
140 
141 switch(line) {
142 
143  case 1: LCD_send_command(0b00001000);
144  LCD_send_command(0b00000000);
145  break;
146 
147  case 2: LCD_send_command(0b00001100);
148  LCD_send_command(0b00000000);
149  break;
150 
151  case 3: LCD_send_command(0b00001001);
152  LCD_send_command(0b00000100);
153  break;
154 
155  case 4: LCD_send_command(0b00001101);
156  LCD_send_command(0b00000100);
157  }
158 }
159 
160 
161 //************************************************************
162 // LCD_goto_xy Implementation
163 //************************************************************
164 void LCD_goto_xy (unsigned char x, unsigned char y){
165 
166  LCD_goto_line (y);
167  LCD_shift_cursor (RIGHT, x-1);
168 }
169 
170 
171 //************************************************************
172 // LCD_write_char Implementation
173 //************************************************************
174 void LCD_write_char (unsigned char value) {
175 
176  unsigned char preliminary_buffer;
177 
179 
180  #ifdef PCF8574_GENERIC
182  #endif
183 
184  #ifdef MCP23008
186  #endif
187 
188  // Splitting of the first nibble
189  preliminary_buffer = (value & 0xF0) >> 4;
190 
191  LCD_send_command (preliminary_buffer);
192 
193  // Splitting of the second nibble
194  preliminary_buffer = (value & 0x0F);
195 
196  LCD_send_command (preliminary_buffer);
197 
199 
200  #ifdef PCF8574_GENERIC
202  #endif
203 
204  #ifdef MCP23008
206  #endif
207 }
208 
209 //************************************************************
210 // LCD_write_message Implementation
211 //************************************************************
212 #ifndef __XC8
213 void LCD_write_message (const rom unsigned char *buffer) {
214 #endif
215 
216 #ifdef __XC8
217 void LCD_write_message (const unsigned char *buffer) {
218 #endif
219 
220  // Write data to LCD up to null
221  while(*buffer) {
222 
223  // Write character to LCD
224  LCD_write_char (*buffer);
225  // Increment buffer
226  buffer++;
227  }
228 }
229 
230 //************************************************************
231 // LCD_write_string Implementation
232 //************************************************************
233 void LCD_write_string (unsigned char *buffer) {
234 
235  // Write data to LCD up to null
236  while(*buffer){
237 
238  // Write character to LCD
239  LCD_write_char (*buffer);
240  // Increment buffer
241  buffer++;
242  }
243 }
244 
245 
246 //************************************************************
247 // LCD_write_integer Implementation
248 //************************************************************
249 void LCD_write_integer (signed int value, unsigned char number_of_digits, unsigned char zero_cleaning){
250 
251  // The array size is 5 plus end of string \0
252  unsigned char convertedInt [6] = {0,0,0,0,0,0};
253 
254  // Index used to shift to the right the digit
255  unsigned char index;
256 
257  // Integer is converted to string
258  #ifndef __XC8
259  itoa (value, (unsigned char*) convertedInt);
260  #endif
261 
262  #ifdef __XC8
263  itoa ((unsigned char*) convertedInt, value,10);
264  #endif
265 
266  if (number_of_digits >0 ) {
267 
268  convertedInt[number_of_digits] = '\0';
269 
270  // Shift the digit to the right removing the empty one
271  while (!(convertedInt[number_of_digits-1] <= '9' && convertedInt[number_of_digits-1] >= '0')) {
272 
273  for (index = number_of_digits-1; index > 0; index--){
274  convertedInt[index] = convertedInt[index-1];
275 
276  if (zero_cleaning == ZERO_CLEANING_ON) {
277  convertedInt[index-1] = ' ';
278  } else {
279  convertedInt[index-1] = '0';
280  }
281  }
282  }
283  }
284 
285  LCD_write_string (convertedInt);
286 
287 }
288 
289 
290 //************************************************************
291 // LCD_clear Implementation
292 //************************************************************
293 void LCD_clear (void){
294 
295  LCD_send_command (0b00000000);
296  LCD_send_command (0b00000001);
297 }
298 
299 //************************************************************
300 // cursor_LCD Implementation
301 //************************************************************
302 void LCD_cursor (unsigned char active,unsigned char blinking) {
303 
304  LCD_send_command (0b00000000);
305  LCD_send_command (0b00001100 | active | blinking);
306 }
307 
308 //************************************************************
309 // LCD_backlight Implementation
310 //************************************************************
311 void LCD_backlight (unsigned char active) {
312 
313  //Clear the LED bit
315 
316  //write the data within the buffer
317  data_buffer = data_buffer | active;
318 
319  #ifdef PCF8574_GENERIC
321  #endif
322 
323  #ifdef MCP23008
325  #endif
326 }
327 
328 //************************************************************
329 // LCD_initialize Implementation
330 //************************************************************
331 void LCD_initialize (unsigned char quartz_frequency) {
332 
333  delay_set_quartz (quartz_frequency);
334 
336  data_buffer = 0;
337 
338  #ifdef PCF8574_GENERIC
339  PCF8574_initialize (quartz_frequency, LCD_BUS_DATA_RATE);
341  #endif
342 
343  #ifdef MCP23008
345  //Set the device with all ouptut pins
347  //Set all the output to 0
349  #endif
350 
351 
352  delay_ms (100);
353  LCD_send_command (0b00000011);
354  delay_ms (10);
355  LCD_send_command (0b00000011);
356  delay_ms (1);
357  LCD_send_command (0b00000011);
358 
359  LCD_send_command (0b00000010);
360 
361  LCD_send_command (0b00000010);
362  LCD_send_command (0b00001000);
363 
364  LCD_send_command (0b00000000);
365  LCD_send_command (0b00001000);
366 
367  LCD_send_command (0b00000000);
368  LCD_send_command (0b00000001);
369 
370  LCD_send_command (0b00000000);
371  LCD_send_command (0b00000110);
372 
374  LCD_clear ();
375 
376  delay_ms (10);
377 }
378