Page 1 of 1

Writing/Reading EEPROM for PIC16f887

PostPosted: Mon Jul 27, 2020 3:23 am
by SamuelWong
Hi, I want to store a value to the EEPROM of a PIC16f887 mcu. The code attached below is the asm code to read from the EEPROM. What does banksel mean and how to write the command in C for xc8 compiler. I'm not asking for the code, I just don't understand what banksel is for.

Thanks in advance

Re: Writing/Reading EEPROM for PIC16f887

PostPosted: Mon Jul 27, 2020 3:42 am
by ric
The registers in a PIC16F887 are accessed via four "banks".
BANKSEL is a macro that generates the code required to select a particular bank.
(it actually generates two machine instructions on that PIC. Newer PICs can do it in one, but the macro hides the details.)
You don't need to worry about it in C, as the compiler generates all the code required to access any particular register.

However, you don't need to worry about this low level code at all in XC8. The compiler knows how to access EEPROM memory directly.
You just add a "__eeprom" qualifier when you define a variable, and the compiler will generate all the necessary code to access it correctly whenever you use that variable.

Re: Writing/Reading EEPROM for PIC16f887

PostPosted: Mon Jul 27, 2020 7:57 am
by SamuelWong
does that mean that I don't have to write the code to read and write to the EEPROM
Code: Select all
/***************************** EEPROM Functions *******************************/
void EEPROM_Write(uint8_t Address, uint8_t Data)
{
    EEADR = Address;
    EEDAT = Data;
    EECON1bits.EEPGD = 0;
    EECON1bits.WREN = 1;
   
    INTCONbits.GIE == 0;
    EECON2 = 0x55;         // Part Of Writing Mechanism..
    EECON2 = 0xAA;         // Part Of Writing Mechanism..
    EECON1bits.WR = 1;
    INTCONbits.GIE = 1;
   
    SLEEP();
    EECON1bits.WREN = 1;
    STATUSbits.RP0 = 0;
    STATUSbits.RP1 = 0; 
}
uint8_t EEPROM_Read(uint8_t Address)
{
    uint8_t Data;
    EEADR = Address;
    EECON1bits.EEPGD = 0;
    EECON1bits.RD = 1;
    Data = EEDAT;
    return Data;
}
/*************************** End EEPROM Functions *****************************/

Re: Writing/Reading EEPROM for PIC16f887

PostPosted: Mon Jul 27, 2020 8:01 am
by SamuelWong
if my variable name is duration,
How do I declare it?

int_eeprom duration;
or
int duration_eeprom;

Re: Writing/Reading EEPROM for PIC16f887

PostPosted: Mon Jul 27, 2020 8:08 am
by ric
SamuelWong wrote:does that mean that I don't have to write the code to read and write to the EEPROM

That is correct.

SamuelWong wrote:int_eeprom duration;
or
int duration_eeprom;

Neither
Code: Select all
int __eeprom duration;
//or
__eeprom int duration;

(the order of the qualifiers does not matter)
You could just resort to reading the XC8 User Manual, that does explain it and show examples...

Re: Writing/Reading EEPROM for PIC16f887

PostPosted: Mon Jul 27, 2020 10:21 am
by AussieSusan

Re: Writing/Reading EEPROM for PIC16f887

PostPosted: Mon Jul 27, 2020 2:24 pm
by SamuelWong
Do I have to declare
int__eeprom duration =0;
If I want the initial value to be 0?

Re: Writing/Reading EEPROM for PIC16f887

PostPosted: Mon Jul 27, 2020 9:02 pm
by ric
SamuelWong wrote:Do I have to declare
int__eeprom duration =0;
If I want the initial value to be 0?

Yes, the same as all variables.
The only difference is that that only affects the value after programming. If your code changes the value, then the new value will persist through power cycles of your device (which is the whole point of using EEPROM).

n.b. You have left out the space between "int" and "__eeprom". You can't do that, it is required!
Code: Select all
int __eeprom duration =0;

Re: Writing/Reading EEPROM for PIC16f887

PostPosted: Sat Aug 01, 2020 2:43 am
by ric
Is it all sorted now?