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