CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Save parameter to FLASH in 16f1508

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
edi



Joined: 22 Dec 2003
Posts: 82

View user's profile Send private message

Save parameter to FLASH in 16f1508
PostPosted: Thu Mar 10, 2016 3:55 pm     Reply with quote

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: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Mar 10, 2016 8:58 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Mar 10, 2016 11:27 pm     Reply with quote

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: 19553

View user's profile Send private message

PostPosted: Fri Mar 11, 2016 2:34 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Mar 11, 2016 2:36 am     Reply with quote

Many thanks.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Fri Mar 11, 2016 4:51 pm     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group