Whatever you declare as a variable in your code will be in the RAM of the PIC, and thus will disappear when you power it off. However, the PIC18F2580 has 256 bytes of EEPROM memory, which is non-volatile. You can store your phone numbers there each time they are modified, and load them at boot to your variables.
To read and write a byte to a given address of the EEPROM, you have to use the EECON1
, EECON2
, EEDATL
and EEADRL
registers (see chapter 8 of the datasheet), for example:
uint8_t read_eeprom(uint8_t addr)
{
EECON1 = 0;
EEADRL = addr;
EECON1bits.RD = 1;
return EEDATL;
}
void write_eeprom(uint8_t addr, uint8_t value)
{
EECON1 = 0;
EEADRL = addr; // address to write to
EEDATL = value; // value to write
EECON1bits.WREN = 1; // enable write
EECON2 = 0x55; // write unlock sequence
EECON2 = 0xAA;
EECON1bits.WR = 1; // do the actual write
EECON1bits.WREN = 0; // disable writing
while (EECON1bits.WR != 0); // wait for the writing to complete
EEIF = 0; // clear EEPROM interrupt flag
}
And then you can load your numbers with something like this:
void load_number(const uint8_t start_addr, char* n, const int len)
{
for (uint8_t i = 0; i < n; i++) {
n[i] = read_eeprom(start_addr + i);
}
}
load_number(0, callNumber1, 10);
load_number(10, callNumber2, 10);
// etc.
And same idea for saving. To note that this code is not using the EEPROM memory very efficiently, because storing ASCII digits in it wastes space. If you get constrained by the 256 bytes of EEPROM, you might for example store the numbers in BCD format, thus having two digits per byte.
Unlike RAM, nonvolatile memories as EEPROM and Flash have a limited number of write cycles. Although this number is generally very high (1 million cycles for the PIC18F2580 EEPROM), it is good practice not to write to nonvolatile memory unless this is necessary, i.e, the data has actually been modified and has to be stored.
If data has to be written periodically to a EEPROM or Flash memory, a wear-levelling algortithm might be helpful to evenly distribute the wear on the memory cells. This might be even more important with Flash-based nonvolatile memory, that often has less write cycles than EEPROM (e.g., the Flash memory of the PIC18F2580 has "only" 100k write cycles endurance).
The problem I am facing is these variables works as expected.
That's not your problem. \$\endgroup\$