PIC18 LaurTec Library  3.1.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_I2C.c
Go to the documentation of this file.
1 /*******************************************************************************
2 
3 Author : Mauro Laurenti
4 Version : 1.3
5 Created on Date : 09/03/2012
6 Last update : 04/10/2013
7 
8 CopyRight 2006-2013 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 acknowledgment 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 
40  #ifndef _PIC18
41  #error "The LCD_44780_I2C Library supports only PIC18 devices"
42  #endif
43 #endif
44 
45 #include "LCD_44780_I2C.h"
46 #include "PCF8574.h"
47 #include "PCF8574.c"
48 #include "delay.h"
49 
50 unsigned char data_buffer = 0;
51 unsigned char PCF8574_address = 0;
52 
53 //************************************************************
54 // enable_pulse_LCD Implementation
55 //************************************************************
56 void enable_pulse_LCD (void) {
59  delay_ms (1);
62 }
63 
64 
65 //************************************************************
66 // send_command_LCD Implementation
67 //************************************************************
68 void send_command_LCD (unsigned char data) {
69 
70 
71  //clear the lower nible
72  data_buffer = data_buffer & 0b11100000;
73 
74  //write the data within the buffer
75  data_buffer = data_buffer | data;
76 
79 }
80 
81 
82 //************************************************************
83 // home_LCD Implementation
84 //************************************************************
85 void home_LCD(void) {
86 
87  send_command_LCD (0b00000000);
88  send_command_LCD (0b00000010);
89 }
90 
91 
92 //************************************************************
93 // shift_LCD Implementation
94 //************************************************************
95 void shift_LCD(unsigned char shift, unsigned char number_of_shift) {
96 
97  unsigned char i;
98 
99  for (i=0; i < number_of_shift; i++) {
100  send_command_LCD (0b00000001);
101  send_command_LCD (0b00001000 | shift);
102  }
103 }
104 
105 
106 //************************************************************
107 // shift_cursor_LCD Implementation
108 //************************************************************
109 void shift_cursor_LCD(unsigned char shift, unsigned char number_of_shift){
110 
111  unsigned char i;
112 
113  for (i=0; i < number_of_shift; i++) {
114  send_command_LCD (0b00000001);
115  send_command_LCD (0b00000000 | shift);
116  }
117 }
118 
119 //************************************************************
120 // goto_line_LCD Implementation
121 //************************************************************
122 void goto_line_LCD (unsigned char line) {
123 
124 switch(line) {
125 
126  case 1: send_command_LCD(0b00001000);
127  send_command_LCD(0b00000000);
128  break;
129 
130  case 2: send_command_LCD(0b00001100);
131  send_command_LCD(0b00000000);
132  break;
133 
134  case 3: send_command_LCD(0b00001001);
135  send_command_LCD(0b00000100);
136  break;
137 
138  case 4: send_command_LCD(0b00001101);
139  send_command_LCD(0b00000100);
140  }
141 }
142 
143 
144 //************************************************************
145 // goto_xy_LCD Implementation
146 //************************************************************
147 void goto_xy_LCD (unsigned char x, unsigned char y){
148 
149  goto_line_LCD (y);
150  shift_cursor_LCD (RIGHT, x-1);
151 }
152 
153 
154 //************************************************************
155 // write_char_LCD Implementation
156 //************************************************************
157 void write_char_LCD (unsigned char value) {
158 
159  unsigned char preliminary_buffer;
160 
163 
164  // Splitting of the first nibble
165  preliminary_buffer = (value & 0xF0) >> 4;
166 
167  send_command_LCD (preliminary_buffer);
168 
169  // Splitting of the second nibble
170  preliminary_buffer = (value & 0x0F);
171 
172  send_command_LCD (preliminary_buffer);
173 
176 }
177 
178 //************************************************************
179 // write_message_LCD Implementation
180 //************************************************************
181 #ifndef __XC8
182 void write_message_LCD(const rom unsigned char *buffer) {
183 #endif
184 
185 #ifdef __XC8
186 void write_message_LCD(const unsigned char *buffer) {
187 #endif
188 
189  // Write data to LCD up to null
190  while(*buffer) {
191 
192  // Write character to LCD
193  write_char_LCD(*buffer);
194  // Increment buffer
195  buffer++;
196  }
197 }
198 
199 //************************************************************
200 // write_string_LCD Implementation
201 //************************************************************
202 void write_string_LCD(unsigned char *buffer) {
203 
204  // Write data to LCD up to null
205  while(*buffer){
206 
207  // Write character to LCD
208  write_char_LCD(*buffer);
209  // Increment buffer
210  buffer++;
211  }
212 }
213 
214 
215 //************************************************************
216 // write_integer_LCD Implementation
217 //************************************************************
218 void write_integer_LCD(int value, unsigned char number_of_digits){
219 
220  // The array size is 5 plus end of string \0
221  unsigned char convertedInt [6];
222 
223  // Index used to shift to the right the digit
224  unsigned char index;
225 
226  // Integer is converted to string
227  #ifndef __XC8
228  itoa (value, (unsigned char*) convertedInt);
229  #endif
230 
231  #ifdef __XC8
232  itoa ((unsigned char*) convertedInt, value,10);
233  #endif
234 
235  if (number_of_digits >0 ) {
236 
237  convertedInt[number_of_digits] = '\0';
238 
239  // Shift the digit to the right removing the empty one
240  while (!(convertedInt[number_of_digits-1] <= '9' && convertedInt[number_of_digits-1] >= '0')) {
241 
242  for (index = number_of_digits-1; index > 0; index--){
243  convertedInt[index] = convertedInt[index-1];
244  convertedInt[index-1] = ' ';
245  }
246  }
247  }
248 
249  write_string_LCD (convertedInt);
250 
251 }
252 
253 
254 //************************************************************
255 // clear_LCD Implementation
256 //************************************************************
257 void clear_LCD (void){
258 
259  send_command_LCD (0b00000000);
260  send_command_LCD (0b00000001);
261 }
262 
263 //************************************************************
264 // cursor_LCD Implementation
265 //************************************************************
266 void cursor_LCD(unsigned char active,unsigned char blinking) {
267 
268  SendCommand (0b00000000);
269  SendCommand (0b00001100 | active | blinking);
270 }
271 
272 //************************************************************
273 // backlight_LCD Implementation
274 //************************************************************
275 void backlight_LCD(unsigned char active) {
276 
277  //Clear the LED bit
279 
280  //write the data within the buffer
281  data_buffer = data_buffer | active;
283 }
284 
285 //************************************************************
286 // initialize_LCD Implementation
287 //************************************************************
288 void initialize_LCD(unsigned unsigned char quartz_frequency) {
289 
290  setQuartz (quartz_frequency);
291 
293 
294  data_buffer = 0;
296 
297  delay_ms (100);
298  send_command_LCD (0b00000011);
299  delay_ms (10);
300  send_command_LCD (0b00000011);
301  delay_ms (1);
302  send_command_LCD (0b00000011);
303 
304  send_command_LCD (0b00000010);
305 
306  send_command_LCD (0b00000010);
307  send_command_LCD (0b00000000);
308 
309  send_command_LCD (0b00000000);
310  send_command_LCD (0b00001000);
311 
312  send_command_LCD (0b00000000);
313  send_command_LCD (0b00000001);
314 
315  send_command_LCD (0b00000000);
316  send_command_LCD (0b00000110);
317 
318 
320  ClearLCD ();
321 }
322