C18 LaurTec Library  2.5
Open Source C Library for PIC18 Microcontrollers
LaurTec_c18_libraries_v_2.5/src/i2cEEPROM.c
Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003 Author : Mauro Laurenti
00004 Version : 1.0
00005 Date : 4/9/2006
00006  
00007 CopyRight 2006 all rights are reserved
00008 
00009 
00010 ********************************************************
00011 SOFTWARE LICENSE AGREEMENT
00012 ********************************************************
00013 
00014 The usage of the supplied software imply the acceptance of the following license.
00015 
00016 The software supplied herewith by Mauro Laurenti (the Author) 
00017 is intended for use solely and exclusively on Microchip PIC Microcontroller (registered mark).  
00018 The software is owned by the Author, and is protected under applicable copyright laws. 
00019 All rights are reserved. 
00020 Any use in violation of the foregoing restrictions may subject the 
00021 user to criminal sanctions under applicable laws (Italian or International ones), as well as to 
00022 civil liability for the breach of the terms and conditions of this license. 
00023 Commercial use is forbidden without a written acknowledgment with the Author.
00024 Personal or educational use is allowed if the application containing the following 
00025 software doesn't aim to commercial use or monetary earning of any kind.    
00026 
00027 THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES, 
00028 WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED 
00029 TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
00030 PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE AUTHOR SHALL NOT, 
00031 IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR 
00032 CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
00033 
00034 ********************************************************
00035 PURPOSES
00036 ********************************************************/
00037 
00038 #include "i2cEEPROM.h"
00039 
00040 
00041 //************************************************************
00042 //             write_I2C_EEPROM function implementation
00043 //************************************************************
00044 
00045 signed char write_I2C_EEPROM( unsigned char control, int address, unsigned char data ){
00046         
00047         unsigned char addressH;
00048         unsigned char addressL; 
00049         
00050         addressH = (unsigned char) ((address & 0xFF00) >> 8);
00051         addressL = (unsigned char) (address & 0x00FF);
00052         
00053 //*****************************
00054 // Start Condition and control
00055 // Byte are sent
00056 //*****************************         
00057 
00058         // Check if the module is idle
00059         IdleI2C();    
00060         // Initiate START condition                  
00061         StartI2C();                     
00062 
00063         // Wait until start condition is over 
00064         while (SSPCON2bits.SEN);  
00065         
00066         // Check if Bus collition happened   
00067         if (PIR2bits.BCLIF) {
00068           // Return with Bus Collision error
00069      return (-1);                 
00070    }
00071 
00072         // Write control byte - R/W bit should be 0
00073         if (WriteI2C(control)){
00074                 // Return with write Collision error            
00075       return (-3);             
00076      }
00077      
00078 //*****************************
00079 // Address High is sent
00080 //***************************** 
00081 
00082         // Check if the module is idle
00083         IdleI2C(); 
00084         
00085         // Check if ACK condition has been received                 
00086         if (!SSPCON2bits.ACKSTAT){ 
00087                 
00088                 // Write address high to the EEPROM
00089       if (WriteI2C(addressH)){
00090              // Return with write Collision error 
00091         return (-3);           
00092       }
00093         } else {
00094                 // Return with Not Ack error condition
00095         return (-2);               
00096      }
00097  
00098 //*****************************
00099 // Address Low is sent
00100 //*****************************
00101       
00102         // Check if the module is idle
00103         IdleI2C();   
00104         
00105         // Check if ACK condition has been received               
00106         if (!SSPCON2bits.ACKSTAT) {
00107                 
00108                 // Write address low to the EEPROM
00109                 if (WriteI2C(addressL)) {
00110                         // Return with write Collision error 
00111          return (-3);          
00112         }
00113    } else {
00114         // Return with Not Ack error condition
00115         return (-2);            
00116     }
00117 
00118 //*****************************
00119 // Data Byte is sent
00120 //*****************************   
00121  
00122         // Check if the module is idle
00123    IdleI2C(); 
00124        
00125    // Check if ACK condition has been received             
00126    if (!SSPCON2bits.ACKSTAT){
00127             
00128                 // Write data byte to the EEPROM           
00129       if (WriteI2C(data)) {
00130               // Return with write Collision error 
00131          return (-3);       
00132         }
00133       } else {
00134         // Return with Not Ack error condition
00135                 return (-2);            
00136       }
00137     
00138 //*****************************
00139 // Stop command is sent
00140 //*****************************
00141   
00142         // Check if the module is idle
00143         IdleI2C();  
00144         
00145         // Check if ACK condition has been received                       
00146         if (!SSPCON2bits.ACKSTAT) {
00147         
00148         // Send STOP condition          
00149         StopI2C();
00150          
00151         // Wait until stop condition is over                       
00152         while (SSPCON2bits.PEN);      
00153   
00154   } else {
00155                 // Return with Not Ack error condition
00156                 return (-2);           
00157    }
00158   
00159   // Test for bus collision
00160   if (PIR2bits.BCLIF){
00161          // Return with Bus Collision error 
00162     return (-1);                
00163   }
00164   
00165   // Return with no error
00166   return (1);                   
00167 }
00168 
00169 
00170 //************************************************************
00171 //         write_I2C_EEPROM_check function implementation
00172 //************************************************************
00173 
00174 signed char write_I2C_EEPROM_check( unsigned char control, int address, unsigned char data ) {
00175         
00176         // Store the returned error from the read and write function call
00177         signed char error;
00178         
00179         // Store the data read back from the EEPROM
00180         unsigned char dataReadback;
00181         
00182         error = write_I2C_EEPROM(control, address, data);
00183 
00184         if (error < 0)
00185                 return (error);
00186                 
00187         // wait for the write process to be completed
00188         delay_ms (6);
00189          
00190         error = read_I2C_EEPROM(control, address, &dataReadback);
00191         
00192         if (error < 0)
00193                 return (error);
00194         
00195         if (dataReadback == data)
00196                 return (1);
00197         else 
00198                 return(-4);
00199 }
00200 
00201 
00202 //************************************************************
00203 //           read_I2C_EEPROM function implementation
00204 //************************************************************
00205 
00206 signed char read_I2C_EEPROM( unsigned char control, int address, unsigned char *data) {
00207         
00208         unsigned char addressH;
00209         unsigned char addressL; 
00210         
00211         addressH = (unsigned char) ((address & 0xFF00) >> 8);
00212         addressL = (unsigned char) (address & 0x00FF);
00213 
00214 
00215 //*****************************
00216 // Start Condition and control
00217 // Byte are sent
00218 //*****************************
00219 
00220         // Check if the module is idle
00221         IdleI2C();    
00222         // Initiate START condition                  
00223         StartI2C(); 
00224         
00225         // Wait until start condition is over 
00226         while (SSPCON2bits.SEN);  
00227         
00228         // Check if Bus collition happened   
00229         if (PIR2bits.BCLIF) {
00230                 // Return with Bus Collision error 
00231                 return (-1);               
00232         }
00233         
00234         // Write Control Byte 
00235         if (WriteI2C(control)){
00236                  // Return with write collision error
00237       return (-3);             
00238         } 
00239 
00240 //*****************************
00241 // Address High is sent
00242 //***************************** 
00243 
00244         // Check if the module is idle
00245         IdleI2C();                    
00246         
00247         // Check if ACK condition has been received
00248         if (!SSPCON2bits.ACKSTAT) {
00249                 
00250                 // Write address high to the EEPROM
00251       if (WriteI2C(addressH)){
00252                 // Return with write collision error
00253                         return (-3);            
00254       }
00255     }else {
00256         // Return with Not Ack error condition
00257                 return (-2);          
00258     }
00259 
00260 //*****************************
00261 // Address Low is sent
00262 //***************************** 
00263         // Check if the module is idle
00264         IdleI2C(); 
00265         
00266         // Check if ACK condition has been received                   
00267         if (!SSPCON2bits.ACKSTAT) {
00268                 // Write address low to the EEPROM
00269       if (WriteI2C(addressL)) {
00270               // Return with write collision error 
00271                 return (-3);            
00272       }
00273    }else {
00274         // Return with Not Ack error condition
00275                 return (-2);          
00276     }
00277 
00278 
00279 //*****************************
00280 // Restart condition
00281 //*****************************         
00282         // Check if the module is idle
00283         IdleI2C(); 
00284           
00285         // Check if ACK condition has been received                 
00286         if (!SSPCON2bits.ACKSTAT){
00287                 
00288                 // Initiate I2C bus restart condition
00289                 RestartI2C();     
00290                 // Wait until re-start condition is over         
00291                 while (SSPCON2bits.RSEN);
00292        
00293       // Check if Bus collition happened   
00294                 if (PIR2bits.BCLIF) {
00295                         // Return with Bus Collision error 
00296          return (-1);          
00297       }
00298       
00299       // Write Control byte - This time R/W bit should be 1                    
00300                 if (WriteI2C(control+1)) {
00301                         // Return with write collision error
00302          return (-3);          
00303       }
00304 
00305 //*****************************
00306 // Data is Read
00307 //*****************************
00308 
00309                 // Check if the module is idle
00310                 IdleI2C();               
00311        
00312       // Check if ACK condition has been received  
00313                 if (!SSPCON2bits.ACKSTAT){
00314                         
00315                         // Enable master for 1 byte reception
00316                         SSPCON2bits.RCEN = 1;
00317                                
00318          // Check that receive sequence is over
00319          while (SSPCON2bits.RCEN);
00320          
00321          // Send not ACK condition 
00322          NotAckI2C(); 
00323             
00324          // Wait until ACK sequence is over           
00325          while (SSPCON2bits.ACKEN );
00326          
00327          // Send STOP condition
00328          StopI2C(); 
00329               
00330          // Wait until stop condition is over         
00331          while (SSPCON2bits.PEN); 
00332          
00333          // Check if Bus collition happened 
00334          if (PIR2bits.BCLIF) {
00335                  // return with Bus Collision error 
00336                                 return (-1);        
00337          }
00338          
00339                 } else {
00340                         // Return with Not Ack error
00341                 return (-2);          
00342                   }
00343 
00344         } else {
00345                 // Return with Not Ack error
00346                 return (-2);            
00347      }   
00348         
00349         // Data is read from the buffer 
00350         *data = SSPBUF;
00351  
00352         // No error occured  
00353         return (1);
00354 
00355 }
00356 
00357 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines