Writing/Reading EEPROM for PIC16f887

(instructions, reset, WDT, specifications...) PIC12F6xx, PIC16Fxxx, PIC16F6x, PIC16F7x

Writing/Reading EEPROM for PIC16f887

Postby SamuelWong » Mon Jul 27, 2020 3:23 am

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
Attachments
115909645_669101190616498_2766561172354488850_n (1).png
115909645_669101190616498_2766561172354488850_n (1).png (18.53 KiB) Viewed 4313 times
SamuelWong
 
Posts: 11
Joined: Wed Jul 22, 2020 6:20 am

Re: Writing/Reading EEPROM for PIC16f887

Postby ric » Mon Jul 27, 2020 3:42 am

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.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: Writing/Reading EEPROM for PIC16f887

Postby SamuelWong » Mon Jul 27, 2020 7:57 am

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 *****************************/
SamuelWong
 
Posts: 11
Joined: Wed Jul 22, 2020 6:20 am

Re: Writing/Reading EEPROM for PIC16f887

Postby SamuelWong » Mon Jul 27, 2020 8:01 am

if my variable name is duration,
How do I declare it?

int_eeprom duration;
or
int duration_eeprom;
SamuelWong
 
Posts: 11
Joined: Wed Jul 22, 2020 6:20 am

Re: Writing/Reading EEPROM for PIC16f887

Postby ric » Mon Jul 27, 2020 8:08 am

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...
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: Writing/Reading EEPROM for PIC16f887

Postby AussieSusan » Mon Jul 27, 2020 10:21 am

AussieSusan
Verified identity
 
Posts: 173
Joined: Mon Jun 16, 2014 4:45 am
PIC experience: Experienced Hobbyist

Re: Writing/Reading EEPROM for PIC16f887

Postby SamuelWong » Mon Jul 27, 2020 2:24 pm

Do I have to declare
int__eeprom duration =0;
If I want the initial value to be 0?
SamuelWong
 
Posts: 11
Joined: Wed Jul 22, 2020 6:20 am

Re: Writing/Reading EEPROM for PIC16f887

Postby ric » Mon Jul 27, 2020 9:02 pm

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;
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: Writing/Reading EEPROM for PIC16f887

Postby ric » Sat Aug 01, 2020 2:43 am

Is it all sorted now?
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products


Return to 14-Bit Core

Who is online

Users browsing this forum: No registered users and 5 guests

cron