PIC18 LaurTec Library  3.1.1
Open Source C Library for PIC18 Microcontrollers based on C18 - XC8 Compilers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
intEEPROM.c
Go to the documentation of this file.
1 /*******************************************************************************
2 
3 Autore : Mauro Laurenti
4 Versione : 1.1
5 
6 Created on Date : 14/08/2007
7 Last update : 25/01/2013
8 
9 CopyRight 2006-2013 all rights are reserved
10 
11 ********************************************************
12 SOFTWARE LICENSE AGREEMENT
13 ********************************************************
14 
15 The usage of the supplied software imply the acceptance of the following license.
16 
17 The software supplied herewith by Mauro Laurenti (the Author) is intended for
18 use solely and exclusively on Microchip PIC Microcontroller (registered mark).
19 The software is owned by the Author, and is protected under applicable
20 copyright laws. All rights are reserved.
21 Any use in violation of the foregoing restrictions may subject the
22 user to criminal sanctions under applicable laws, as well as to civil liability
23 for the breach of the terms and conditions of this license.
24 Commercial use is forbidden without a written acknowledgment with the Author.
25 Personal or educational use is allowed if the application containing the
26 following software doesn't aim to commercial use or monetary earning of any kind.
27 
28 THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
29 WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
30 TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
31 PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE AUTHOR SHALL NOT,
32 IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
33 CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
34 
35 *******************************************************************************/
36 
37 #ifdef __XC8
38 #include <xc.h>
39 #endif
40 
41 #include "intEEPROM.h"
42 
43 
44 //************************************************************
45 // write_internal_EEPROM function implementation
46 //************************************************************
47 
48 unsigned char write_internal_EEPROM (unsigned char address, unsigned char data) {
49 
50  // Flag used to store the GIE value
51  unsigned char flagGIE = 0;
52 
53  // Flag used to store the GIEH value
54  unsigned char flagGIEH = 0;
55 
56  // Flag used to store the GIEL value
57  unsigned char flagGIEL = 0;
58 
59  // Set the address that will be written
60  EEADR = address;
61 
62  // Set the data that will be written
63  EEDATA = data;
64 
65  // EEPROM memory is pointed
66  EECON1bits.EEPGD = 0;
67 
68  #ifdef _PIC18
69  // EEPROM access enable
70  EECON1bits.CFGS = 0;
71  #endif
72 
73  // Enable write
74  EECON1bits.WREN = 0x01;
75 
76  // Check and store the Interrupt Status
77  if (INTCONbits.GIE == 1) {
78  INTCONbits.GIE = 0;
79  flagGIE = 1;
80  }
81 
82  #ifdef _PIC18
83  if (INTCONbits.GIEH == 1) {
84  INTCONbits.GIEH = 0;
85  flagGIEH = 1;
86  }
87 
88  if (INTCONbits.GIEL == 1) {
89  INTCONbits.GIEL = 0;
90  flagGIEL = 1;
91  }
92  #endif
93 
94 
95 
96  // Start the writing enabling sequence
97  EECON2 = 0x55;
98  EECON2 = 0xAA;
99 
100  // Initiate writing process
101  EECON1bits.WR = 0x01;
102 
103  // Wait the end of the writing process
104  while (EECON1bits.WR);
105 
106 
107  // Restore the previous interrupt status
108  if (flagGIE == 1) {
109  INTCONbits.GIE = 1;
110  }
111 
112 
113  #ifdef _PIC18
114  if (flagGIEH == 1) {
115  INTCONbits.GIEH = 1;
116  }
117 
118  if (flagGIEL == 1) {
119  INTCONbits.GIEL = 1;
120 
121  }
122  #endif
123 
124  // Disable the writing process
125  EECON1bits.WREN = 0x00;
126 
127 
128  // Check if the data has been properly written,
129  // a simple read back is done
130  if (read_internal_EEPROM (address) == data) {
131 
132  return (1);
133 
134  } else {
135 
136  return (0);
137  }
138 
139 }
140 
141 
142 //************************************************************
143 // read_internal_EEPROM Function Implementation
144 //************************************************************
145 
146 unsigned char read_internal_EEPROM (unsigned char address) {
147 
148  unsigned char data = 0;
149 
150  // Set the memory address that will be read
151  EEADR = address;
152 
153  // EEPROM memory is pointed
154  EECON1bits.EEPGD = 0;
155 
156  #ifdef _PIC18
157  // EEPROM access enable
158  EECON1bits.CFGS = 0;
159  #endif
160 
161  // Initiate reading
162  EECON1bits.RD = 0x01;
163 
164  // Data is read from the register
165  data = EEDATA;
166 
167  return (data);
168 
169 }
170