ARF
ARF

Reputation: 7694

PIC16: self-modify code

On a microchip PIC16 microcontroller I have a program of the sort:

off    = 2;
period = 10;

while (1) {
  if (counter == 1) {
    switch something on;
  }elseif (counter == off) {
    switch something off;
  }elseif (counter == period) {
    counter = 1;
  }
  counter++;
  pause;
}

In a communication module of my code (not shown) I would like to implement the possibility to modify the program code: to change the values assigned to "off" and "period".

Note: I do not want to make a variable assignment (which is lost on reset) but want to make a permanent change to the program stored on the device.

Can somebody point me in the right direction? Ideally I would like to implement this in C. What is the easiest way to do this? Should "off" and "period" be variables, pointers or constants for easy implementation?

Many thanks!

Upvotes: 0

Views: 334

Answers (1)

Nathan Wiebe
Nathan Wiebe

Reputation: 802

This is generally something you would do in Data EEPROM, if the micro has it (most 16F's do). Usually you would read from the EEPROM on boot and fill RAM variables with the values read, and use the variables from your code. Microchip has prebuilt code for the read and write sequence for data EEPROM. Also, if the part has no EEPROM, you could dedicate a particular page of program FLASH to data storage, but then you have to buffer and erase a whole page at a time, edit linker scripts, etc, which is a whole 'nother topic.

Upvotes: 3

Related Questions