C18 LaurTec Library  2.5
Open Source C Library for PIC18 Microcontrollers
LaurTec_c18_libraries_v_2.5/src/intEEPROM.c
Go to the documentation of this file.
00001 /*******************************************************
00002 ********************************************************
00003  
00004 Autore : Mauro Laurenti
00005 Versione : 1.0
00006 Data : 14/08/2007
00007  
00008 CopyRight 2007 all rights are reserved
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 This library is supposed for being used for read and 
00040 write the PIC microcontroller internal EEPROM 
00041  
00042 *******************************************************
00043 *******************************************************/
00044 
00045 
00046 #include "intEEPROM.h"
00047 
00048 
00049 //************************************************************
00050 //         write_Int_EEPROM function implementation
00051 //************************************************************
00052 
00053 char write_Int_EEPROM (unsigned char data, unsigned char address) {
00054         
00055         // Flag used to store the GIE value
00056         unsigned char flagGIE = 0;      
00057         
00058         // Flag used to store the GIEH value
00059         unsigned char flagGIEH = 0;     
00060         
00061         // Flag used to store the GIEL value
00062         unsigned char flagGIEL = 0; 
00063 
00064         // Set the address that will be written
00065         EEADR = address;        
00066         
00067         // Set the data that will be written
00068         EEDATA = data;          
00069         
00070         // EEPROM memory is pointed
00071         EECON1bits.EEPGD = 0; 
00072                 
00073         // EEPROM access enable
00074         EECON1bits.CFGS = 0;    
00075         
00076         // Enable write 
00077         EECON1bits.WREN = 0x01; 
00078 
00079         // Check and store the Interrupt Status
00080         if (INTCONbits.GIE == 1) {  
00081                         INTCONbits.GIE = 0; 
00082                         flagGIE = 1;
00083         }
00084         
00085         if (INTCONbits.GIEH == 1) {  
00086                         INTCONbits.GIEH = 0; 
00087                         flagGIEH = 1;
00088         }
00089         
00090         if (INTCONbits.GIEL == 1) {  
00091                         INTCONbits.GIEL = 0; 
00092                         flagGIEL = 1;
00093         }
00094         
00095         // Start the writing enabling sequence          
00096         EECON2 = 0x55;  
00097         EECON2 = 0xAA;
00098 
00099         // Initiate writing process
00100         EECON1bits.WR = 0x01;   
00101 
00102         // Wait the end of the writing process
00103         while (EECON1bits.WR);  
00104         
00105         
00106         // Restore the previous interrupt status
00107         if (flagGIE == 1) {
00108                 INTCONbits.GIE = 1; 
00109         }       
00110                 
00111         if (flagGIEH == 1) {
00112                 INTCONbits.GIEH = 1; 
00113         }
00114         
00115         if (flagGIEL == 1) {
00116                 INTCONbits.GIEL = 1; 
00117         
00118         }
00119         
00120         // Disable the writing process
00121         EECON1bits.WREN = 0x00; 
00122         
00123         
00124         // Check if the data has been properly written,
00125         // a simple read back is done
00126     if (read_Int_EEPROM (address) == data) {
00127                 
00128                 return (1);
00129                 
00130         } else {
00131                 
00132                 return (0);
00133         }
00134 
00135 }
00136 
00137 
00138 //************************************************************
00139 //           read_Int_EEPROM Function  Implementation
00140 //************************************************************
00141 
00142 unsigned char read_Int_EEPROM (unsigned char address) {
00143         
00144         unsigned char data = 0;
00145 
00146         // Set the memory address that will be read
00147         EEADR = address;        
00148         
00149         // EEPROM memory is pointed     
00150         EECON1bits.EEPGD = 0; 
00151         
00152         // EEPROM access enable
00153         EECON1bits.CFGS = 0;    
00154         
00155         // Initiate reading
00156         EECON1bits.RD = 0x01;   
00157         
00158         // Data is read from the register
00159         data = EEDATA;                  
00160         
00161         return (data);          
00162                 
00163 }
00164 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines