| View previous topic :: View next topic |
| Author |
Message |
bschriek
Joined: 18 Dec 2007 Posts: 83
|
| Is there a better way to store calibration values? |
Posted: Mon Feb 09, 2026 9:25 am |
|
|
<16F15223.H> CCS compiler 5.121
I use the following code to read and write calibration values to the memory of my PIC's for years. Although it works fine can somebody tell me if a better solution is available for the modern pic's like the 16F15223?
//---------------Configuration-----------------//
#define HEF 0X0780
#ROM int16 HEF = {160,160}
struct twobytes
{
int8 l;
int8 h;
};
union prog_mem
{
int16 word;
struct twobytes b;
};
//-----------------Main loop-------------------//
struct {
union prog_mem I_cal_hef_24V;
union prog_mem U_cal_hef_24V;
}
//--------Read calibration values from memory--------//
read_program_memory(HEF,&values,2);
I_cal_24V = values.I_cal_hef_24V.b.l;
U_cal_24V = values.U_cal_hef_24V.b.l;
//--------Write calibration values to memory--------//
values.I_cal_hef_24V.b.l = I_sense_24V;
values.U_cal_hef_24V.b.l = U_sense_24V;
write_program_memory(HEF,&values,2);
Sorry for this question but I'm a hardware engineer....... |
|
 |
newguy
Joined: 24 Jun 2004 Posts: 1928
|
|
Posted: Mon Feb 09, 2026 5:56 pm |
|
|
Not really. Two things: the address of the high endurance flash should be available using the getenv feature so that you don't have to look it up yourself. Second, I always guard writes to memories that are either slow or can wear out (or both). I always read the contents of the memory first and only write if the value is different than what I intend to write.
Other than those minor nit picks, you're doing it right. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20032
|
|
Posted: Mon Feb 09, 2026 11:36 pm |
|
|
From the description, it sounds basically right. Obvious comments apply
about keeping the number of cycles down, but assuming this is rarely
done, the life of the HEF would be great.
There are 'caveat's' if you want to move later to chips like a 24F, since
on these the program memory is not contiguous as it is on the 18F.
Also you need to be aware that the processor will basically stop while
this write is being done. Could cause some problems.
Doing it as you do with the ROM declaration, is actually probably the
tidiest way of doing this.
Also obviously you have to be aware that the whole page gets erased
when you do this. so careful what else you store in this area. You
could expand the ROM statement with dummy bytes, to ensure that
nothing else gets put into the page. |
|
 |
|