PIC18 LaurTec Library  3.3.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.5
5 Created on Date : 09/03/2012
6 Last update : 15/12/2014
7 
8 CopyRight 2006-2014 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 // LCD_enable_pulse Implementation
54 //************************************************************
55 void LCD_enable_pulse (void) {
58  delay_ms (1);
61 }
62 
63 
64 //************************************************************
65 // LCD_send_command Implementation
66 //************************************************************
67 void LCD_send_command (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 // LCD_home Implementation
83 //************************************************************
84 void LCD_home (void) {
85 
86  LCD_send_command (0b00000000);
87  LCD_send_command (0b00000010);
88 }
89 
90 
91 //************************************************************
92 // LCD_shift Implementation
93 //************************************************************
94 void LCD_shift (unsigned char shift, unsigned char number_of_shift) {
95 
96  unsigned char i;
97 
98  for (i=0; i < number_of_shift; i++) {
99  LCD_send_command (0b00000001);
100  LCD_send_command (0b00001000 | shift);
101  }
102 }
103 
104 
105 //************************************************************
106 // LCD_shift_cursor Implementation
107 //************************************************************
108 void LCD_shift_cursor (unsigned char shift, unsigned char number_of_shift){
109 
110  unsigned char i;
111 
112  for (i=0; i < number_of_shift; i++) {
113  LCD_send_command (0b00000001);
114  LCD_send_command (0b00000000 | shift);
115  }
116 }
117 
118 //************************************************************
119 // LCD_goto_line Implementation
120 //************************************************************
121 void LCD_goto_line (unsigned char line) {
122 
123 switch(line) {
124 
125  case 1: LCD_send_command(0b00001000);
126  LCD_send_command(0b00000000);
127  break;
128 
129  case 2: LCD_send_command(0b00001100);
130  LCD_send_command(0b00000000);
131  break;
132 
133  case 3: LCD_send_command(0b00001001);
134  LCD_send_command(0b00000100);
135  break;
136 
137  case 4: LCD_send_command(0b00001101);
138  LCD_send_command(0b00000100);
139  }
140 }
141 
142 
143 //************************************************************
144 // LCD_goto_xy Implementation
145 //************************************************************
146 void LCD_goto_xy (unsigned char x, unsigned char y){
147 
148  LCD_goto_line (y);
149  LCD_shift_cursor (RIGHT, x-1);
150 }
151 
152 
153 //************************************************************
154 // LCD_write_char Implementation
155 //************************************************************
156 void LCD_write_char (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  LCD_send_command (preliminary_buffer);
167 
168  // Splitting of the second nibble
169  preliminary_buffer = (value & 0x0F);
170 
171  LCD_send_command (preliminary_buffer);
172 
175 }
176 
177 //************************************************************
178 // LCD_write_message Implementation
179 //************************************************************
180 #ifndef __XC8
181 void LCD_write_message (const rom unsigned char *buffer) {
182 #endif
183 
184 #ifdef __XC8
185 void LCD_write_message (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  LCD_write_char (*buffer);
193  // Increment buffer
194  buffer++;
195  }
196 }
197 
198 //************************************************************
199 // LCD_write_string Implementation
200 //************************************************************
201 void LCD_write_string (unsigned char *buffer) {
202 
203  // Write data to LCD up to null
204  while(*buffer){
205 
206  // Write character to LCD
207  LCD_write_char (*buffer);
208  // Increment buffer
209  buffer++;
210  }
211 }
212 
213 
214 //************************************************************
215 // LCD_write_integer Implementation
216 //************************************************************
217 void LCD_write_integer (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  LCD_write_string (convertedInt);
249 
250 }
251 
252 
253 //************************************************************
254 // LCD_clear Implementation
255 //************************************************************
256 void LCD_clear (void){
257 
258  LCD_send_command (0b00000000);
259  LCD_send_command (0b00000001);
260 }
261 
262 //************************************************************
263 // cursor_LCD Implementation
264 //************************************************************
265 void LCD_cursor (unsigned char active,unsigned char blinking) {
266 
267  LCD_send_command (0b00000000);
268  LCD_send_command (0b00001100 | active | blinking);
269 }
270 
271 //************************************************************
272 // LCD_backlight Implementation
273 //************************************************************
274 void LCD_backlight (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 // LCD_initialize Implementation
286 //************************************************************
287 void LCD_initialize (unsigned unsigned char quartz_frequency) {
288 
289  delay_set_quartz (quartz_frequency);
290 
291  initialize_PCF8574 (quartz_frequency, LCD_BUS_DATA_RATE);
293 
294  data_buffer = 0;
296 
297  delay_ms (100);
298  LCD_send_command (0b00000011);
299  delay_ms (10);
300  LCD_send_command (0b00000011);
301  delay_ms (1);
302  LCD_send_command (0b00000011);
303 
304  LCD_send_command (0b00000010);
305 
306  LCD_send_command (0b00000010);
307  LCD_send_command (0b00001000);
308 
309  LCD_send_command (0b00000000);
310  LCD_send_command (0b00001000);
311 
312  LCD_send_command (0b00000000);
313  LCD_send_command (0b00000001);
314 
315  LCD_send_command (0b00000000);
316  LCD_send_command (0b00000110);
317 
318 
320  LCD_clear ();
321 }
322