C18 LaurTec Library  2.5
Open Source C Library for PIC18 Microcontrollers
LaurTec_c18_libraries_v_2.5/src/LCD_44780_I2C.c
Go to the documentation of this file.
00001 /**********************************************************************************************
00002 
00003 Author : Mauro Laurenti
00004 Version : 1.0
00005 Date : 09/03/2012
00006  
00007 CopyRight 2006-2012 all rights are reserved
00008 
00009 ********************************************************
00010 SOFTWARE LICENSE AGREEMENT
00011 ********************************************************
00012 
00013 The usage of the supplied software imply the acceptance of the following license.
00014 
00015 The software supplied herewith by Mauro Laurenti (the Author) 
00016 is intended for use solely and exclusively on Microchip PIC Microcontroller (registered mark).  
00017 The software is owned by the Author, and is protected under applicable copyright laws. 
00018 All rights are reserved. 
00019 Any use in violation of the foregoing restrictions may subject the 
00020 user to criminal sanctions under applicable laws (Italian or International ones), as well as to 
00021 civil liability for the breach of the terms and conditions of this license. 
00022 Commercial use is forbidden without a written acknowledgment with the Author.
00023 Personal or educational use is allowed if the application containing the following 
00024 software doesn't aim to commercial use or monetary earning of any kind.    
00025 
00026 THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES, 
00027 WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED 
00028 TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
00029 PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE AUTHOR SHALL NOT, 
00030 IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR 
00031 CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
00032 
00033 **********************************************************************************************/
00034                                                                         
00035 
00036 #include "LCD_44780_I2C.h"
00037 #include "PCF8574.h"
00038 #include "delay.h"
00039 
00040 unsigned char data_buffer = 0;
00041 unsigned char PCF8574_address = 0;
00042 
00043 //************************************************************
00044 //                  Epulse  Implementation
00045 //************************************************************
00046 void Epulse (void) {
00047         data_buffer = data_buffer | LCD_E;
00048         PCF8574_write_data (PCF8574_address, data_buffer);
00049         delay_ms (1);
00050         data_buffer = data_buffer & ~LCD_E;
00051         PCF8574_write_data (PCF8574_address, data_buffer);
00052 }
00053 
00054 
00055 //************************************************************
00056 //                  SendCommand  Implementation
00057 //************************************************************
00058 void SendCommand (unsigned char data) {
00059 
00060 
00061         //clear the lower nible
00062         data_buffer = data_buffer & 0b11100000; 
00063 
00064         //write the data within the buffer
00065         data_buffer = data_buffer | data;
00066 
00067         PCF8574_write_data (PCF8574_address, data_buffer);
00068         Epulse ();
00069 }
00070 
00071 
00072 //************************************************************
00073 //                  Line2LCD  Implementation
00074 //************************************************************
00075 void Line2LCD(void) {
00076         
00077         SendCommand (0b00001100);       
00078         SendCommand (0b00000000);               
00079 }
00080 
00081 
00082 //************************************************************
00083 //                  HomeLCD  Implementation
00084 //************************************************************
00085 void HomeLCD(void) {
00086         
00087         SendCommand (0b00000000);       
00088         SendCommand (0b00000010);               
00089 }
00090 
00091 
00092 //************************************************************
00093 //                  ShiftLCD  Implementation
00094 //************************************************************
00095 void ShiftLCD(char shift, char number_of_shift) {
00096         
00097         char i;
00098         
00099         for (i=0; i < number_of_shift; i++) {   
00100                 SendCommand (0b00000001);       
00101                 SendCommand (0b00001000 | shift);       
00102         }       
00103 }
00104 
00105 
00106 //************************************************************
00107 //                  ShiftCursorLCD  Implementation
00108 //************************************************************
00109 void ShiftCursorLCD(char shift, char number_of_shift){
00110         
00111         char i;
00112         
00113         for (i=0; i < number_of_shift; i++) {
00114                 SendCommand (0b00000001);       
00115                 SendCommand (0b00000000 | shift);
00116         }               
00117 }
00118 
00119 //************************************************************
00120 //                  GotoLineLCD  Implementation
00121 //************************************************************
00122 void GotoLineLCD (char line) {
00123 
00124 switch(line) {
00125 
00126         case 1: SendCommand(0b00001000);
00127                         SendCommand(0b00000000);
00128                         break;
00129 
00130         case 2: SendCommand(0b00001100);
00131                         SendCommand(0b00000000);
00132                         break;
00133 
00134         case 3: SendCommand(0b00001001);
00135                         SendCommand(0b00000100);
00136                         break;
00137 
00138         case 4: SendCommand(0b00001101);
00139                         SendCommand(0b00000100);
00140         }
00141 }
00142 
00143 //************************************************************
00144 //                  WriteCharLCD  Implementation
00145 //************************************************************
00146 void WriteCharLCD (unsigned char value) {       
00147         
00148         unsigned char preliminary_buffer;
00149         
00150         data_buffer = data_buffer | LCD_RS;
00151         PCF8574_write_data (PCF8574_address, data_buffer);
00152 
00153         // Splitting of the first nibble
00154         preliminary_buffer = (value & 0xF0) >> 4;       
00155                                         
00156         SendCommand (preliminary_buffer);
00157                 
00158         // Splitting of the second nibble
00159         preliminary_buffer = (value & 0x0F);
00160                         
00161         SendCommand (preliminary_buffer);
00162                 
00163         data_buffer = data_buffer & ~LCD_RS;
00164         PCF8574_write_data (PCF8574_address, data_buffer);
00165 }
00166 
00167 //************************************************************
00168 //                  WriteStringLCD  Implementation
00169 //************************************************************
00170 void WriteStringLCD(const rom char *buffer) {
00171         
00172          // Write data to LCD up to null
00173     while(*buffer) {
00174             
00175             // Write character to LCD
00176             WriteCharLCD(*buffer);  
00177             // Increment buffer
00178             buffer++;              
00179    }
00180 }
00181 
00182 //************************************************************
00183 //                  WriteVarLCD  Implementation
00184 //************************************************************
00185 void WriteVarLCD(unsigned char *buffer) {
00186         
00187         // Write data to LCD up to null
00188         while(*buffer){
00189                 
00190                 // Write character to LCD
00191                 WriteCharLCD(*buffer); 
00192                 // Increment buffer
00193                 buffer++;               
00194         }
00195 }
00196 
00197 
00198 //************************************************************
00199 //                  WriteIntLCD  Implementation
00200 //************************************************************
00201 void WriteIntLCD(int value, char number_of_digits){
00202         
00203         // The array size is 5 plus end of string \0
00204         unsigned char convertedInt [6];
00205         
00206         // Index used to shift to the right the digit
00207         char index; 
00208         
00209         // Integer is converted to string
00210         itoa (value, (char*) convertedInt);     
00211         
00212         if (number_of_digits >0 ) {
00213                 
00214                 convertedInt[number_of_digits] = '\0';
00215                 
00216                 // Shift the digit to the right removing the empty one
00217                 while (!isdigit(convertedInt[number_of_digits-1])) {
00218                         
00219                         for (index = number_of_digits-1; index > 0; index--){
00220                                 convertedInt[index] = convertedInt[index-1];
00221                                 convertedInt[index-1] = ' ';
00222                         }                                                       
00223                 }       
00224         }
00225 
00226         WriteVarLCD (convertedInt);
00227         
00228 }       
00229 
00230 
00231 //************************************************************
00232 //                  ClearLCD  Implementation
00233 //************************************************************
00234 void ClearLCD (void){
00235         
00236         SendCommand (0b00000000);       
00237         SendCommand (0b00000001);               
00238 }
00239 
00240 //************************************************************
00241 //                  CursorLCD  Implementation
00242 //************************************************************
00243 void CursorLCD(char active,char blinking) {     
00244         
00245         SendCommand (0b00000000);       
00246         SendCommand (0b00001100 | active | blinking);                                                           
00247 }
00248 
00249 //************************************************************
00250 //                  CursorLCD  Implementation
00251 //************************************************************
00252 void BacklightLCD(char active) {
00253 
00254         //Clear the LED bit
00255         data_buffer = data_buffer & ~TURN_ON_LED;
00256 
00257         //write the data within the buffer
00258         data_buffer = data_buffer | active;
00259         PCF8574_write_data (PCF8574_address, data_buffer);      
00260 }
00261 
00262 //************************************************************
00263 //                  OpenLCD  Implementation
00264 //************************************************************
00265 void  OpenLCD(unsigned char quartz_frequency) { 
00266         
00267         setQuartz (quartz_frequency);
00268 
00269         PCF8574_address = PCF8574_ADDRESS_H | (PCF8574_ADDRESS_L << 1);
00270 
00271         data_buffer = 0;
00272         PCF8574_write_data (PCF8574_address, data_buffer);      
00273         
00274         delay_ms (100);
00275         SendCommand (0b00000011);
00276         delay_ms (5);   
00277         SendCommand (0b00000011);
00278         delay_ms (5);           
00279         SendCommand (0b00000011);
00280         delay_ms (5);           
00281         SendCommand (0b00000010);
00282         SendCommand (0b00000010);
00283         SendCommand (0b00001000);
00284         SendCommand (0b00000000);
00285         SendCommand (0b00001110);
00286         CursorLCD (TURN_OFF_CURSOR,BLINK_OFF);
00287         ClearLCD ();    
00288 }
00289 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines