Microcontroller › PIC › Using internal eeprom(256 bytes)
- This topic has 1 reply, 2 voices, and was last updated 11 years, 1 month ago by Anonymous.
-
AuthorPosts
-
August 28, 2013 at 10:41 am #2611akshatParticipant
I am trying to write data on internal eeprom but i am getting the result.
Help me…
Write code:-
volatile BYTE i;
volatile BYTE vDummy;
BYTE vSPIONSave;
BYTE SPICON1Save;
BYTE B,C;
BYTE* Dummy_ptr; //Akshat
BYTE D;
volatile BYTE Dummy;void Write(DWORD address,BYTE val)
{
vBytesInBuffer = 0;
EEPROMAddress = address;
EEPROMBuffer[vBytesInBuffer++] = val;
B = EEPROMBuffer[0];// Configure SPI
SPI_ON_BIT = 0;
EEPROM_SPICON1 = PROPER_SPICON1;
SPI_ON_BIT = 1;// Set the Write Enable latch
EEPROM_CS_IO = 0;
EEPROM_SSPBUF = OPCODE_WREN;
WaitForDataByte();
vDummy = EEPROM_SSPBUF;
EEPROM_CS_IO = 1;// Send WRITE opcode
EEPROM_CS_IO = 0;
EEPROM_SSPBUF = OPCODE_WRITE;
WaitForDataByte();
vDummy = EEPROM_SSPBUF;// Send address
//#if defined(USE_EEPROM_25LC1024)
//EEPROM_SSPBUF = ((DWORD_VAL*)&EEPROMAddress)->v[2];
//WaitForDataByte();
//vDummy = EEPROM_SSPBUF;
//#endif
EEPROM_SSPBUF = ((DWORD_VAL*)&EEPROMAddress)->v[1];
C = ((DWORD_VAL*)&address)->v[0]; //akshat
WaitForDataByte();
vDummy = EEPROM_SSPBUF;EEPROM_SSPBUF = ((DWORD_VAL*)&EEPROMAddress)->v[0];
C = ((DWORD_VAL*)&address)->v[0]; //akshat
Dummy_ptr = &C; //akshat
(*Dummy_ptr); //Akshat
WaitForDataByte();
Dummy = EEPROM_SSPBUF;
Dummy_ptr = &Dummy; //Akshat
D = (*Dummy_ptr); //Akshat
for(i = 0; i < vBytesInBuffer; i++)
{
// Send the byte to write
EEPROM_SSPBUF = EEPROMBuffer;
WaitForDataByte();
vDummy = EEPROM_SSPBUF;
}// Begin the write
EEPROM_CS_IO = 1;// Update write address and clear write cache
EEPROMAddress += vBytesInBuffer;
vBytesInBuffer = 0;// Restore SPI State
SPI_ON_BIT = 0;
EEPROM_SPICON1 = SPICON1Save;
SPI_ON_BIT = vSPIONSave;
}Read code:-
BYTE Read(DWORD address)
{
// Save the address and emptry the contents of our local buffer
EEPROMAddress = address;
vBytesInBuffer = 0;
XEEReadArray(EEPROMAddress, EEPROMBuffer, EEPROM_BUFFER_SIZE);
// Return a byte from our local buffer
return EEPROMBuffer[EEPROM_BUFFER_SIZE – vBytesInBuffer–];
}XEE_RESULT XEEReadArray(DWORD address, BYTE *buffer, WORD length)
{
volatile BYTE i;
volatile BYTE vDummy;
BYTE vSPIONSave;
BYTE SPICON1Save;
BYTE B,C;
volatile BYTE Dummy;
BYTE* Dummy_ptr; //Akshat
BYTE D;
// Save SPI state (clock speed)
SPICON1Save = EEPROM_SPICON1;
vSPIONSave = SPI_ON_BIT;// Configure SPI
SPI_ON_BIT = 0;
EEPROM_SPICON1 = PROPER_SPICON1;
SPI_ON_BIT = 1;EEPROM_CS_IO = 0;
// Send READ opcode
EEPROM_SSPBUF = OPCODE_READ;
WaitForDataByte();
Dummy = EEPROM_SSPBUF;//Send address
//EEPROM_SSPBUF = ((DWORD_VAL*)&address)->v[2];
//WaitForDataByte();
//Dummy = EEPROM_SSPBUF;EEPROM_SSPBUF = ((DWORD_VAL*)&address)->v[1];
C = ((DWORD_VAL*)&address)->v[0]; //akshat
WaitForDataByte();
Dummy = EEPROM_SSPBUF;EEPROM_SSPBUF = ((DWORD_VAL*)&address)->v[0];
C = ((DWORD_VAL*)&address)->v[0]; //akshat
Dummy_ptr = &C; //akshat
(*Dummy_ptr); //Akshat
WaitForDataByte();
Dummy = EEPROM_SSPBUF;
Dummy_ptr = &Dummy; //Akshat
D = (*Dummy_ptr); //Akshat
while(length–)
{
EEPROM_SSPBUF = 0;
WaitForDataByte();
Dummy = EEPROM_SSPBUF;
if(buffer != NULL)
*buffer++ = Dummy;
}EEPROM_CS_IO = 1;
// Restore SPI state
SPI_ON_BIT = 0;
EEPROM_SPICON1 = SPICON1Save;
SPI_ON_BIT = vSPIONSave;
return XEE_SUCCESS;
}i am writing and reading data using internal eeprom. I am not getting the results.
August 29, 2013 at 4:51 am #10384AnonymousGuestHi Akshat
You forgot to mention which microcontroller are you using!
-
AuthorPosts
- You must be logged in to reply to this topic.