LTlib LaurTec Library  4.0.3
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.6
5 Created on Date : 09/03/2012
6 Last update : 05/06/2016
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 data bits
83 
84  if (data & 0x01) {
86  }
87 
88  if (data & 0x02) {
90  }
91 
92  if (data & 0x04) {
94  }
95 
96  if (data & 0x08) {
98  }
99 
100 
101  #ifdef PCF8574_GENERIC
103  #endif
104 
105  #ifdef MCP23008
107  #endif
108 
109  LCD_enable_pulse ();
110 }
111 
112 
113 //************************************************************
114 // LCD_home Implementation
115 //************************************************************
116 void LCD_home (void) {
117 
118  LCD_send_command (0b00000000);
119  LCD_send_command (0b00000010);
120 }
121 
122 
123 //************************************************************
124 // LCD_shift Implementation
125 //************************************************************
126 void LCD_shift (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 (0b00001000 | shift);
133  }
134 }
135 
136 
137 //************************************************************
138 // LCD_shift_cursor Implementation
139 //************************************************************
140 void LCD_shift_cursor (unsigned char shift, unsigned char number_of_shift){
141 
142  unsigned char i;
143 
144  for (i=0; i < number_of_shift; i++) {
145  LCD_send_command (0b00000001);
146  LCD_send_command (0b00000000 | shift);
147  }
148 }
149 
150 //************************************************************
151 // LCD_goto_line Implementation
152 //************************************************************
153 void LCD_goto_line (unsigned char line) {
154 
155 switch(line) {
156 
157  case 1: LCD_send_command(0b00001000);
158  LCD_send_command(0b00000000);
159  break;
160 
161  case 2: LCD_send_command(0b00001100);
162  LCD_send_command(0b00000000);
163  break;
164 
165  case 3: LCD_send_command(0b00001001);
166  LCD_send_command(0b00000100);
167  break;
168 
169  case 4: LCD_send_command(0b00001101);
170  LCD_send_command(0b00000100);
171  }
172 }
173 
174 
175 //************************************************************
176 // LCD_goto_xy Implementation
177 //************************************************************
178 void LCD_goto_xy (unsigned char x, unsigned char y){
179 
180  LCD_goto_line (y);
181  LCD_shift_cursor (RIGHT, x-1);
182 }
183 
184 
185 //************************************************************
186 // LCD_write_char Implementation
187 //************************************************************
188 void LCD_write_char (unsigned char value) {
189 
190  unsigned char preliminary_buffer;
191 
193 
194  #ifdef PCF8574_GENERIC
196  #endif
197 
198  #ifdef MCP23008
200  #endif
201 
202  // Splitting of the first nibble
203  preliminary_buffer = (value & 0xF0) >> 4;
204 
205  LCD_send_command (preliminary_buffer);
206 
207  // Splitting of the second nibble
208  preliminary_buffer = (value & 0x0F);
209 
210  LCD_send_command (preliminary_buffer);
211 
213 
214  #ifdef PCF8574_GENERIC
216  #endif
217 
218  #ifdef MCP23008
220  #endif
221 }
222 
223 //************************************************************
224 // LCD_write_message Implementation
225 //************************************************************
226 #ifndef __XC8
227 void LCD_write_message (const rom unsigned char *buffer) {
228 #endif
229 
230 #ifdef __XC8
231 void LCD_write_message (const unsigned char *buffer) {
232 #endif
233 
234  // Write data to LCD up to null
235  while(*buffer) {
236 
237  // Write character to LCD
238  LCD_write_char (*buffer);
239  // Increment buffer
240  buffer++;
241  }
242 }
243 
244 //************************************************************
245 // LCD_write_string Implementation
246 //************************************************************
247 void LCD_write_string (unsigned char *buffer) {
248 
249  // Write data to LCD up to null
250  while(*buffer){
251 
252  // Write character to LCD
253  LCD_write_char (*buffer);
254  // Increment buffer
255  buffer++;
256  }
257 }
258 
259 
260 //************************************************************
261 // LCD_write_integer Implementation
262 //************************************************************
263 void LCD_write_integer (signed int value, unsigned char number_of_digits, unsigned char zero_cleaning){
264 
265  // The array size is 5 plus end of string \0
266  unsigned char convertedInt [6] = {0,0,0,0,0,0};
267 
268  // Index used to shift to the right the digit
269  unsigned char index;
270 
271  // Integer is converted to string
272  #ifndef __XC8
273  itoa (value, (unsigned char*) convertedInt);
274  #endif
275 
276  #ifdef __XC8
277  itoa ((unsigned char*) convertedInt, value,10);
278  #endif
279 
280  if (number_of_digits >0 ) {
281 
282  convertedInt[number_of_digits] = '\0';
283 
284  // Shift the digit to the right removing the empty one
285  while (!(convertedInt[number_of_digits-1] <= '9' && convertedInt[number_of_digits-1] >= '0')) {
286 
287  for (index = number_of_digits-1; index > 0; index--){
288  convertedInt[index] = convertedInt[index-1];
289 
290  if (zero_cleaning == ZERO_CLEANING_ON) {
291  convertedInt[index-1] = ' ';
292  } else {
293  convertedInt[index-1] = '0';
294  }
295  }
296  }
297  }
298 
299  LCD_write_string (convertedInt);
300 
301 }
302 
303 
304 //************************************************************
305 // LCD_clear Implementation
306 //************************************************************
307 void LCD_clear (void){
308 
309  LCD_send_command (0b00000000);
310  LCD_send_command (0b00000001);
311 }
312 
313 //************************************************************
314 // cursor_LCD Implementation
315 //************************************************************
316 void LCD_cursor (unsigned char active,unsigned char blinking) {
317 
318  LCD_send_command (0b00000000);
319  LCD_send_command (0b00001100 | active | blinking);
320 }
321 
322 //************************************************************
323 // LCD_backlight Implementation
324 //************************************************************
325 void LCD_backlight (unsigned char active) {
326 
327  //Clear the LED bit
329 
330  //write the data within the buffer
331  data_buffer = data_buffer | active;
332 
333  #ifdef PCF8574_GENERIC
335  #endif
336 
337  #ifdef MCP23008
339  #endif
340 }
341 
342 //************************************************************
343 // LCD_initialize Implementation
344 //************************************************************
345 void LCD_initialize (unsigned char quartz_frequency) {
346 
347  delay_set_quartz (quartz_frequency);
348 
349  data_buffer = 0;
350 
351  #ifdef PCF8574_GENERIC
355  #endif
356 
357  //The first initialization is done at lower speed
358  //After the initialization the bus speet is set to LCD_BUS_DATA_RATE
359  #ifdef MCP23008
361 
362  MCP2300x_initialize (50);
363  //Set the device with all ouptut pins
365  //Set all the output to 0
367  #endif
368 
369 
370  delay_ms (100);
371  LCD_send_command (0b00000011);
372  delay_ms (10);
373  LCD_send_command (0b00000011);
374  delay_ms (1);
375  LCD_send_command (0b00000011);
376 
377  LCD_send_command (0b00000010);
378 
379  LCD_send_command (0b00000010);
380  LCD_send_command (0b00001000);
381 
382  LCD_send_command (0b00000000);
383  LCD_send_command (0b00001000);
384 
385  LCD_send_command (0b00000000);
386  LCD_send_command (0b00000001);
387 
388  LCD_send_command (0b00000000);
389  LCD_send_command (0b00000110);
390 
392  LCD_clear ();
393 
394  delay_ms (10);
395 
396  #ifdef MCP23008
398  //Set the device with all ouptut pins
400  //Set all the output to 0
402  #endif
403 
404 
405 }
406