Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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.

Latest threads

New Articles From Microcontroller Tips

Back
Top