C18 LaurTec Library  2.5
Open Source C Library for PIC18 Microcontrollers
LaurTec_c18_libraries_v_2.5/src/PCF8574.c
Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003 Author : Mauro Laurenti
00004 Version : 1.0
00005 Date : 19/03/2011
00006  
00007 CopyRight 2006-2011 all rights are reserved
00008 
00009 
00010 
00011 ********************************************************
00012 SOFTWARE LICENSE AGREEMENT
00013 ********************************************************
00014 
00015 The usage of the supplied software imply the acceptance of the following license.
00016 
00017 The software supplied herewith by Mauro Laurenti (the Author) 
00018 is intended for use solely and exclusively on Microchip PIC Microcontroller (registered mark).  
00019 The software is owned by the Author, and is protected under applicable copyright laws. 
00020 All rights are reserved. 
00021 Any use in violation of the foregoing restrictions may subject the 
00022 user to criminal sanctions under applicable laws (Italian or International ones), as well as to 
00023 civil liability for the breach of the terms and conditions of this license. 
00024 Commercial use is forbidden without a written acknowledgment with the Author.
00025 Personal or educational use is allowed if the application containing the following 
00026 software doesn't aim to commercial use or monetary earning of any kind.    
00027 
00028 THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES, 
00029 WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED 
00030 TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
00031 PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE AUTHOR SHALL NOT, 
00032 IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR 
00033 CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
00034 
00035 ********************************************************
00036 PURPOSES
00037 ********************************************************/
00038 
00039 #include "PCF8574.h"
00040 
00041 
00042 //************************************************************
00043 //         PCF8574_write_data function implementation
00044 //************************************************************
00045 
00046 signed char PCF8574_write_data(unsigned char control, unsigned char data ){
00047         
00048         
00049 //*****************************
00050 // Start Condition and control
00051 // Byte are sent
00052 //*****************************         
00053 
00054         // Check if the module is idle
00055         IdleI2C();    
00056         // Initiate START condition                  
00057         StartI2C();                     
00058 
00059         // Wait until start condition is over 
00060         while (SSPCON2bits.SEN);  
00061         
00062         // Check if Bus collition happened   
00063         if (PIR2bits.BCLIF) {
00064           // Return with Bus Collision error
00065      return (-1);                 
00066    }
00067 
00068         // Write control byte - R/W bit should be 0
00069         if (WriteI2C(control)){
00070                 // Return with write Collision error            
00071       return (-3);             
00072      }
00073      
00074  
00075 //*****************************
00076 // Data Byte is sent
00077 //*****************************   
00078  
00079         // Check if the module is idle
00080    IdleI2C(); 
00081        
00082    // Check if ACK condition has been received             
00083    if (!SSPCON2bits.ACKSTAT){
00084             
00085                 // Write data byte to the data port        
00086       if (WriteI2C(data)) {
00087               // Return with write Collision error 
00088          return (-3);       
00089         }
00090       } else {
00091         // Return with Not Ack error condition
00092                 return (-2);            
00093       }
00094     
00095 //*****************************
00096 // Stop command is sent
00097 //*****************************
00098   
00099         // Check if the module is idle
00100         IdleI2C();  
00101         
00102         // Check if ACK condition has been received                       
00103         if (!SSPCON2bits.ACKSTAT) {
00104         
00105         // Send STOP condition          
00106         StopI2C();
00107          
00108         // Wait until stop condition is over                       
00109         while (SSPCON2bits.PEN);      
00110   
00111   } else {
00112                 // Return with Not Ack error condition
00113                 return (-2);           
00114    }
00115   
00116   // Test for bus collision
00117   if (PIR2bits.BCLIF){
00118          // Return with Bus Collision error 
00119     return (-1);                
00120   }
00121   
00122   // Return with no error
00123   return (1);                   
00124 }
00125 
00126 
00127 
00128 //************************************************************
00129 //           PCF8574_read_data function implementation
00130 //************************************************************
00131 
00132 signed char PCF8574_read_data(unsigned char control, unsigned char *data){
00133         
00134 
00135 //*****************************
00136 // Start Condition and control
00137 // Byte are sent
00138 //*****************************
00139 
00140         // Check if the module is idle
00141         IdleI2C();    
00142         // Initiate START condition                  
00143         StartI2C(); 
00144         
00145         // Wait until start condition is over 
00146         while (SSPCON2bits.SEN);  
00147         
00148         // Check if Bus collition happened   
00149         if (PIR2bits.BCLIF) {
00150                 // Return with Bus Collision error 
00151                 return (-1);               
00152         }
00153         
00154         // Write Control Byte 
00155         if (WriteI2C(control + 1)){
00156                  // Return with write collision error
00157       return (-3);             
00158         } 
00159 
00160 
00161 //*****************************
00162 // Data is Read
00163 //*****************************
00164 
00165                 // Check if the module is idle
00166                 IdleI2C();               
00167        
00168       // Check if ACK condition has been received  
00169                 if (!SSPCON2bits.ACKSTAT){
00170                         
00171                         // Enable master for 1 byte reception
00172                         SSPCON2bits.RCEN = 1;
00173                                
00174                 // Check that receive sequence is over
00175                 while (SSPCON2bits.RCEN);
00176          
00177                 // Send not ACK condition 
00178                  NotAckI2C(); 
00179             
00180                 // Wait until ACK sequence is over           
00181                 while (SSPCON2bits.ACKEN );
00182          
00183                 // Send STOP condition
00184                 StopI2C(); 
00185               
00186                 // Wait until stop condition is over         
00187                 while (SSPCON2bits.PEN); 
00188          
00189                 // Check if Bus collition happened 
00190                 if (PIR2bits.BCLIF) {
00191                         // return with Bus Collision error 
00192                                 return (-1);        
00193          }         
00194 
00195                 } else {
00196                 // Return with Not Ack error
00197                 return (-2);            
00198      }   
00199         
00200         // Data is read from the buffer 
00201         *data = SSPBUF;
00202  
00203         // No error occured  
00204         return (1);
00205 
00206 }
00207 
00208 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines