Eoin87
Joined: 20 Oct 2009 Posts: 18
|
PIC24F32KA302 EEPROM Issue [solved] |
Posted: Wed Oct 31, 2012 9:01 am |
|
|
Hi all.
I am having some issues with writing the internal EEPROM of my PIC24F32K3A02.
I tried using the built in CCS functions but they are not working for me. For this reason I followed the PIC datasheet and tried to create my own functions.
I am trying to erase the first location of EEPROM at 0x7FFE00 and write a word to this location.
Here the code:
Code: |
void eeprom_write(unsigned int32 address,unsigned int16 data) // writes one word
{
unsigned int y,high;
address=address+0x7ffe00;
high=make8(address,2);
#asm
mov high,w0
mov w0,0X32
mov address,w9
mov data,w4
TBLWTL w4,[w9]
mov #0x4004,w2
repeat #30 //dont understand this, but without NVMCON not set properly
nop
mov w2,nvmcon
disi #5
mov #0x55,w0
mov w0,0x766
mov #0xaa,w0
mov w0,0x766
bset nvmcon,15
nop
repeat #10
nop
#endasm
while(bit_Test(nvmcon,15));
output_toggle(led_green);
}
void eeprom_erase(unsigned int32 address,unsigned int16 block_size)
{
unsigned char high;
address=address+0x7ffe00;
high=make8(address,2);
#asm asis
mov block_size,w3 // move erase operation required
repeat #30
nop
mov w3,0x760
mov.b high,w0
mov w0,0X32 // tbplpag register
mov address,w0 // move address
TBLWTL w0,[w0]
disi #5
mov #0x55,w0
mov w0,0x766
mov #0xaa,w0
mov w0,0x766
bset 0x760,15
nop
repeat #10
nop
#endasm
while(bit_Test(nvmcon,15));
output_toggle(led_green);
}
unsigned int16 eeprom_read(unsigned int32 address)
{
unsigned int y,high;
address=address+0x7ffe00;
high=make8(address,2);
#asm
mov high,w0
mov w0,0X32
mov address,w9
TBLrdl [w9],w4
mov w4,high
#endasm
return high;
}
|
from main I call these function like this
Code: |
val2=0xa0a0;
output_high(led_red);
eeprom_erase(0,0x4058); // command for entire eprom
val=eeprom_read(0);
eeprom_write(0,val2); // write a single byte
output_low(led_red);
val=eeprom_read(0);
delay_ms(1000);
|
Any ideas why I always get 0 for EEPROM read?
The operations seem to be taking a few ms as expected when I measure the length of time the RED led is on for. My processor speed is default 8mhz FRC. |
|