internal flash

Status
Not open for further replies.

Dr_Doggy

Well-Known Member
hey, i have been trying to r/w with the internal flash, bouncing between online samples and the datasheet i have so far gotten these subroutines: and the main is after it:



void writeFL(unsigned long addr, unsigned char data)
{ unsigned char cnt;
EECON1=0b10000100; //ensure
TBLPTRU = ((unsigned char)addr>>16),\
TBLPTRH = (unsigned char)(((unsigned int)addr)>>8),\
TBLPTRL = ((unsigned char)addr);

TABLAT = data;

INTCONbits.GIE=0; //No interrupts
EECON2 = 0x55; //required sequence #1
EECON2 = 0xAA; //#2
EECON1bits.WR = 1; //#3 = actual write
while(EECON1bits.WR == 1){}
EECON1bits.WREN = 0; //disable write to EEPROM
}

unsigned char readFL(unsigned long addr)
{
EECON1=0b10000000; //ensure CFGS=0
TBLPTRU = ((unsigned char)addr>>16),\
TBLPTRH = (unsigned char)(((unsigned int)addr)>>8),\
TBLPTRL = ((unsigned char)addr);
EECON1bits.RD = 1;
while(EECON1bits.RD){}
xxx = 1;
return(TABLAT);
}


void main (){
cnt3=0;
for(cnt4=1;cnt4<17;cnt4++){cnt3++;writeFL(cnt4,cnt3);}
for(cnt4=1;cnt4<17;cnt4++){DATi[cnt4] = readFL(cnt4);}
}

but when i go to run this, the whole array returns the last value of cnt3 written, so in this case DATi = 16,16,16,16,16,16,16...ect
changing it to this next thing would result in DATi = 14,14,14,14,14,14,14,14,...ect

void main (){
cnt3=0;
for(cnt4=1;cnt4<15;cnt4++){cnt3++;writeFL(cnt4,cnt3);}
for(cnt4=1;cnt4<17;cnt4++){DATi[cnt4] = readFL(cnt4);}
}

my best guess is that either i dont understand the TBLPTR or TABLAT, or i am casting the wrong variable type in the readFL return.?
 
To read flash you need the tblrd instructions.

Try,
Code:
unsigned char readFL(unsigned long addr){
    EECON1=0b10000000; //ensure CFGS=0
    TBLPTRU = ((unsigned char)(addr>>16)); // note extra brackets.
    TBLPTRH = (unsigned char)(addr>>8);
    TBLPTRL = ((unsigned char)addr);
    asm("\tTBLRD*");
    return(TABLAT);
}

Mike.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…