PDA

View Full Version : Run-Time Config



Darrel Taylor
- 18th June 2006, 06:00
How many times have you been told that CONFIG settings can only be changed by an ICSP programmer while downloading the program's code?    And how many times did you believe them?   Well, for me, it was every time, until I ran across a BIT that I didn't recognize in the datasheets.

CFGS: Flash Program/Data EEPROM or Configuration Select bit
    1 = Access Configuration registers
    0 = Access Flash program or data EEPROM memory

Once I saw that, I knew it could be done, it was just a matter of figuring out how.   The datasheet was really vague, and it simply said that it was similar to writing to Flash Memory. After a little trial and error, I found that it really is just that easy.   So maybe, you want to add a routine to verify that the chip has been programmed with the proper config settings for the program. Or maybe you need to change the settings according to the users input from a menu. Now you can.

This option is available on every 18F that I've looked at so far, so that means it's been around for years. And, no, it's not available on any 16F's that I've seen.<HR>
This program presents the following macros:
<b><pre>@ ReadConfig?CB _CONFIG3H, tempbyte</pre></b>
Read the contents of a CONFIG register and place the results in a PBP byte variable. The constant for the CONFIG location can be either, one of the pre-defined names from the MPASM .inc file such as _CONFIG1H or _CONFIG3H, or the address of the location like 300001h
<b><pre>@ ReadConfig?BB AddrByte, tempbyte</pre></b>
Read the contents of a CONFIG register using a byte offset from the start of the CONFIG address space. For instance, to read register 300001h, the AddrByte variable will have the value 1. The result will be placed in a PBP byte variable.
<b><pre>@ WriteConfig?CB _CONFIG1L, tempbyte</pre></b>
Write a byte to the CONFIG register designated by a constant
<b><pre>@ WriteConfig?BB AddrByte, tempbyte</pre></b>
Write a byte to the CONFIG register using an offset to the CONFIG adress space
<b><pre>@ WriteConfig?CC _CONFIG5L, 0Fh</pre></b>
Write a byte sized constant to the CONFIG register designated by a constant<HR>
Obviously there are some issues to think about before using these macro's. Such as, an oscillator must be running to be able to execute code. So trying to set the oscillator config at run-time will only work if you start off with a configuration that allows a system clock. Other changes in mid stream might put the CCP output on a pin that has a low impeadance signal on the input, causing damage. It's up to you to make sure these things don't happen. Which I guess is no different than if you're using ICSP.
<br>
http://www.darreltaylor.com/RTconfig/RTconfig.inc.txt
http://www.darreltaylor.com/RTconfig/TestConfig.pbp.txt

Art
- 1st February 2012, 17:26
Such as, an oscillator must be running to be able to execute code
LoL! Or trying to ignore MCLR while the chip is in reset! :D
Nice :)