Aguijón Software Libraries  1.0
Documentation for the included Libraries
 All Files Functions Variables Typedefs Enumerations Enumerator Macros
EEPROM.c
Go to the documentation of this file.
1 /********************************************************************
2  FileName: EEPROM.c
3  Dependencies: See INCLUDES section
4  Hardware: Aguijón rev3.0
5  Complier: Microchip XC16(for PIC24), C30(for PIC24)
6  Company: Vinagrón Digital
7 
8  Software License Agreement:
9 
10  blah blah blah
11 
12 ********************************************************************
13  File Description:
14  Includes EEPROM control functions.
15 
16 
17  Change History:
18  Rev Description
19  ---- -----------------------------------------
20  1.0 Initial release
21 ********************************************************************/
22 
23 #include <p24FJ128GB106.h>
24 #include "i2c.h"
25 #include "init.h"
26 #include "definitions.h"
27 #include "EEPROM.h"
28 
29 
41 unsigned char read_EEPROM(unsigned char address)
42 {
43  unsigned char value=0;
44 
45  if(address > 0x7F){ return FALSE; }
46 
47  #ifdef I2C_HW
48 
49  I2C_Start();
51  I2CsendByte(address);
52  I2C_Stop();
53  I2C_Start();
55 
56  value = I2C_getByte(FALSE);
57 
58  NotAckI2C1();
59  I2C_Stop();
60  return value;
61 
62  #else
63 
64  I2C_Start();
66  I2CsendByte(address);
67  I2C_Stop();
68  I2C_Start();
70  SDA_TRIS = 1;
71  for(i=0 ; i<=7 ; i++){
72  value |= (char)(SDA_READ <<(7-i));
73  SCL = 0;
74  SCL = 1;
75  SCL = 0;
76  }
77  SDA_TRIS = 0;
78  I2C_Stop();
79  return value;
80 
81  #endif
82 }
83 
95 int write_EEPROM(unsigned char address, unsigned char to_write)
96 {
97  unsigned char write_check = 0;
98 
99  address &= EEPROM_SIZE; //con esto limitamos la direccion hasta 0x7F que es la locacion maxima
100 
101  I2C_Start();
103  I2CsendByte(address);
104  I2CsendByte(to_write);
105  I2C_Stop();
106 
107  write_check = read_EEPROM(address);
108 
109  if(write_check == to_write){
110  return TRUE;
111  }else{
112  return FALSE;
113  }
114 }
115 
127 int test_EEPROM(void)
128 {
129  //funcion que prueba la eeprom al escribirle valores random y despues regresandola a FF
130  int errors=0;
131  unsigned char index;
132  unsigned char values[10] = {0xAA, 0x88, 0x35, 0x64, 0x84, 0xFE, 0xBB, 0x66, 0x21, 0xCD};
133 
134  for(index=0 ; index < 10 ; index++){
135  while(!write_EEPROM((0x31+index), values[index]));
136 
137  if(read_EEPROM((0x31+index)) != values[index]){
138  errors++;
139  }
140  }
141 
142  for(index=0 ; index < 128 ; index++){
143  delayms(1);
144  while(!write_EEPROM(index, 0xFF));
145 
146  if(read_EEPROM(index) != 0xFF){
147  errors++;
148  }
149  }
150 
151  return errors;
152 }
153