sivakumarmhn
Newbie level 1
flash_write pic
Hello !
I am using pic16f873a for a small acess control project. The ids of each persons are to be stored in flash memory of pic. But I have trouble with my code , the memory location once updated is not being rewritten when the function is called again with the same address.. would some one suggest me another code
my code goes like this:
void flash_write ( int address, char data )
{
unsigned char offset;
unsigned char buffer[4]= { 0,0,0,0}; // creates 4 word block for data
int dest_addr = address;
char i = 0;
offset = (unsigned char)(address & 0x03);
dest_addr &= 0xfffc; // initialise the dest_addr to point to the first address in the 4 word block
EEADR=(unsigned char)(dest_addr & 0xff); // load current word from destination
EEADRH=(unsigned char)((dest_addr>>8) & 0xff);
for( i= 0; i <= BLOCK_LAST_BYTE; i++ )
{
WREN=0;
EEPGD = 1; // Read and save whole block
RD=1;
asm("nop");
asm("nop");
buffer= ( EEDATA );
RD = 0;
EEADR++;
}
// DelayMs(20);
EEADR=(unsigned char)(dest_addr & 0xff); // load current word from destination
EEADRH=(unsigned char)((dest_addr>>8) & 0xff);
buffer[offset] = data;
for( i = 0; i <= BLOCK_LAST_BYTE; i++ )
{
EEDATA = buffer;
EEDATH = 0;
EEPGD = 1;
WREN = 1;
//GIE = ;
EECON2 = 0x55;
EECON2 = 0xAA;
WR = 1;
asm("nop");
asm("nop");
asm("nop");
WREN = 0;
WR = 0;
//GIE =
EEADR++;
}
while ( WRERR == 1 ){;}
while ( EEIF == 1 ){;}
}
Hello !
I am using pic16f873a for a small acess control project. The ids of each persons are to be stored in flash memory of pic. But I have trouble with my code , the memory location once updated is not being rewritten when the function is called again with the same address.. would some one suggest me another code
my code goes like this:
void flash_write ( int address, char data )
{
unsigned char offset;
unsigned char buffer[4]= { 0,0,0,0}; // creates 4 word block for data
int dest_addr = address;
char i = 0;
offset = (unsigned char)(address & 0x03);
dest_addr &= 0xfffc; // initialise the dest_addr to point to the first address in the 4 word block
EEADR=(unsigned char)(dest_addr & 0xff); // load current word from destination
EEADRH=(unsigned char)((dest_addr>>8) & 0xff);
for( i= 0; i <= BLOCK_LAST_BYTE; i++ )
{
WREN=0;
EEPGD = 1; // Read and save whole block
RD=1;
asm("nop");
asm("nop");
buffer= ( EEDATA );
RD = 0;
EEADR++;
}
// DelayMs(20);
EEADR=(unsigned char)(dest_addr & 0xff); // load current word from destination
EEADRH=(unsigned char)((dest_addr>>8) & 0xff);
buffer[offset] = data;
for( i = 0; i <= BLOCK_LAST_BYTE; i++ )
{
EEDATA = buffer;
EEDATH = 0;
EEPGD = 1;
WREN = 1;
//GIE = ;
EECON2 = 0x55;
EECON2 = 0xAA;
WR = 1;
asm("nop");
asm("nop");
asm("nop");
WREN = 0;
WR = 0;
//GIE =
EEADR++;
}
while ( WRERR == 1 ){;}
while ( EEIF == 1 ){;}
}