View previous topic :: View next topic |
Author |
Message |
edi
Joined: 22 Dec 2003 Posts: 82
|
Save parameter to FLASH in 16f1508 |
Posted: Thu Mar 10, 2016 3:55 pm |
|
|
What is the simplest way to write 1 byte (int8) to the flash of the pic 16f1508?
What is the way to read this int8?
And what I need to do if I want to re-program this single byte with a new value. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Thu Mar 10, 2016 8:58 pm |
|
|
First you should download and read the datasheet, specifically the chapter concerning 'flash program memory control'. In there you will read how the PIC can read and write to the 'flash'. You need to understand you cannot simply 'read/write' a single byte of data. There is a process of unlocking the PICs flash memory, then some 'math' to access the actual data. As well you have to access an entire 'row' of data (32, 14 bit words). Now if you want to store one byte of data ,you must read in a 'row' of 32 into RAM, edit the one byte you want, then write the entire row to flash. You also have to be aware of how many times you can write to flash, again that info is in the data sheets, and it varies from PIC to PIC family.
CCS does supply 'drivers' or 'functions' which will do all of the above, 'behind the scenes' for you so check with the CCS Manual. As well, they supply example programs.
The more you read about the PIC the better the programmer you will become.
Jay |
|
|
edi
Joined: 22 Dec 2003 Posts: 82
|
|
Posted: Thu Mar 10, 2016 11:27 pm |
|
|
Thanks for your reply.
Does the CCS commands do all the job behind? Including read all the row before (read/modify/write).
Do I need to use more commands beside:
write_program_memory
read_program_memory
Is there a need to erase between writes different data or this is also done behind?
Thanks. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Fri Mar 11, 2016 2:34 am |
|
|
Classic 'read the manual':
"Writes count bytes to program memory from dataptr to address. This function is most effective when count is a multiple of FLASH_WRITE_SIZE. Whenever this function is about to write to a location that is a multiple of FLASH_ERASE_SIZE then an erase is performed on the whole block."
The CCS 'write_program_memory' function will erase a row, if you write to the first byte of a row. Not otherwise. So if you want an 'eeprom' type behaviour (ability to set a single byte), you have to read the whole row, change the byte you want, and write the row back.
There is a supplied driver. "virtual_eeprom.c", which for chips with larger pages (>32 bytes), generates an emulation of an EEPROM using the last two pages in memory. However this is designed for chips which have pages like 1KB in size, and is relatively bulky. It uses an approach similar to the MicroChip example of this, with alternate bytes containing data, and the others used as 'flags' to determine if the cell is in vacant or not, and if in use hold the address of the eeprom location this is emulating. This avoids having to erase more than absolutely necessary (if there is still an 'erased' pair of bytes in the page, when asked to write, it overwrites the old flag to '00', and writes the new value and address to the already erased location - only when the page gets full, does it need to erase). |
|
|
edi
Joined: 22 Dec 2003 Posts: 82
|
|
Posted: Fri Mar 11, 2016 2:36 am |
|
|
Many thanks. |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Fri Mar 11, 2016 4:51 pm |
|
|
I had the same need for EEPROM that you did a while ago when using the 16F1509. My requirement was to store a number of int16 words for calibration retrieval. The hardware SPI pins were already committed so a bit-bang was the answer.
If you don't need 64 x 16bits there is a driver that is easy to use in the CCS library for 8bit mode too. I'd suggest external flash if you need to rewrite a lot - as I did. Driver is here:
http://www.ccsinfo.com/forum/viewtopic.php?p=203415 |
|
|
|