简体   繁体   中英

How to save values to PIC18?

I would like to store some values to my PIC18 then retain those values even if the power is lost or the unit is reset. An example of the values I would like to save would some something like those 4 digits, 0100.

Any help would be appreciated!

Update : Would this be the way to go?

    unsigned char value;
    unsigned char DEEdata = 0x25;
    unsigned int  DEEaddr = 0x04;

    DataEEInit();
    dataEEFlags.val = 0;

    DataEEWrite(DEEdata,DEEaddr);
    value = DataEERead(DEEaddr);
    Nop();

In mikroc_for_PIC IDE simply you can use :

EEPROM_write(_Addres,char); char = EEPROM_Read(_Addres);

note that: after write it is best if you have delay about 20 ms.

EEPROM in PIC micro-controllers can save data for long period of time. PIC16F84 can store 64 bytes. It is not that much, but it can serve your purpose. A good description to how to write to and read from EEPROM in PIC16 is given in this link.

http://www.romux.com/tutorials/pic-tutorial/eeprom-data-memory

If you use HIGH-TECH as compiler you can simply write and read from EEPROM (which is the only way for keep a value after power lost except FARM ICs) with below function:

Write_b_eep
Read_b_eep

you can find these function from below directory if you install HIGH-TECH :

...\HI-TECH Software\PICC-18\9.80\sources\plib\EEP

It takes a few milli-seconds for the data to write into EE. If you look at https://en.wikipedia.org/wiki/EEPROM it explains the electrical and physical process of both erasing and writing and gives a time between 0.1 and 5mS to achieve this. Write or find a micro-second delay routine (hint the timers are good for this) Change your code to

unsigned char value;
unsigned char DEEdata = 0x25;
unsigned int  DEEaddr = 0x04;

DataEEInit();
dataEEFlags.val = 0;

DataEEWrite(DEEdata,DEEaddr);
wait_us(5000); // Wait 5mS.... data takes a while to burn into EE
value = DataEERead(DEEaddr); // now its available to read

The EEPROM write function, is a bit `fire and forget'. You command it to write and it gets on with it in the background. If you read too quickly after writing you are not guaranteed the value you might expect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM