can i save settings? with a pic mcu (pic16f88 & pic16f628a)

Thread Starter

gatoulisss

Joined Jan 23, 2015
69
hello everyone,
i need your help, i have a project with a pic mcu (pic16f88 & pic16f628a) which uses battery as power supplier, a reed switch is connected on my pic and makes several settings choices that work on the rest off the program.
my question is : is there any way to save this settings so if battery run out or disconnects my mcu will keep the settings i've already done before when i connect new battery?

thank you all very much!
 
Last edited by a moderator:

sagor

Joined Mar 10, 2019
903
Like Les says, write your settings to EEPROM and read them on powerup. Remember however, that the EEPROM has a limited erase/write count of 1,000,000 cycles. So, configure your program to write only when necessary, like after a change only.
If you wrote every second, the EEPROM would only last about 11.5 days. Hopefully the changes are not too frequent, but always do the math.
 

Thread Starter

gatoulisss

Joined Jan 23, 2015
69
Like Les says, write your settings to EEPROM and read them on powerup. Remember however, that the EEPROM has a limited erase/write count of 1,000,000 cycles. So, configure your program to write only when necessary, like after a change only.
If you wrote every second, the EEPROM would only last about 11.5 days. Hopefully the changes are not too frequent, but always do the math.
this is the part of my code that uses the reed switch, i want to save the hx value witch is the choice of the user
how i could do it? so when for example i replace battery the choice will be kept (im using mikro C )

void ep(){ //
delay_ms(50);
while (portb.f0==1) { // while reed switch is closed
a=a+1;
if (a==7){a=0;} //number of different choices
for (k=1; k<=a; k++){ //
sound_play(2600,80); //
delay_ms(200); //
hx=k;} // this is what i want to keep
delay_ms(1000);}
 

sagor

Joined Mar 10, 2019
903
I don't do "C", but how often is the reed switch closed/opened? If you open the switch, you could write hx to EEPROM at that point? No point saving it while it is changing every second or so, just save the last state when the reed is opened(?)
How you write to EEPROM is language dependent, usually there are library calls or published routines to do this.
 

djsfantasi

Joined Apr 11, 2010
9,156
One observation...

It appears that when a=0, the for loop won’t be executed. Is this what you want? Thus, hx will never be 0.
 

Thread Starter

gatoulisss

Joined Jan 23, 2015
69
I don't do "C", but how often is the reed switch closed/opened? If you open the switch, you could write hx to EEPROM at that point? No point saving it while it is changing every second or so, just save the last state when the reed is opened(?)
How you write to EEPROM is language dependent, usually there are library calls or published routines to do this.
the reed is very often used, the void ep though is not so much because you need at least 3sec having the reed closed so you can enter the void and then the choice of hx usually is not so often
so i need to save the result of the hx somehow
 

djsfantasi

Joined Apr 11, 2010
9,156
the reed is very often used, the void ep though is not so much because you need at least 3sec having the reed closed so you can enter the void and then the choice of hx usually is not so often
so i need to save the result of the hx somehow
The while loop exits when the Reed switch is opened. So, why don’t you save hx after the loop exits?
 

Thread Starter

gatoulisss

Joined Jan 23, 2015
69
The while loop exits when the Reed switch is opened. So, why don’t you save hx after the loop exits?
when the program exits loop it keeps the last hx value and everything is fine, the problem is that if for example i disconect and reconnect the battery then the programmed value of hx is lost
so i want to keep the hx value even after power reset and i dont know how to do it
 

djsfantasi

Joined Apr 11, 2010
9,156
You have to do the work and find out how to write and read from your microprocessors EEPROM, using your language.

Values in EEPROM remain, even if the power is turned off.

First, does your microprocessor include user accessible EEPROM?

Second, does your language have EEPROM functions?

in the Arduino world, there is a library that has to be included in your code. The link is to the Arduino reference so you can see how it’s used. The library includes functions to read and write.

In your problem, every time the program starts, it has to read the stored value for hx.

Then, as hx changes and is stable, you have to write the value to EEPROM. This is what I meant when I said to save the value of hx when the loop exits. It’s important to only write when hx is stable, because as others have pointed out, there is a limit to the number of times you can access EEPROM.

Are you catching my drift?
 

Thread Starter

gatoulisss

Joined Jan 23, 2015
69
You have to do the work and find out how to write and read from your microprocessors EEPROM, using your language.

Values in EEPROM remain, even if the power is turned off.

First, does your microprocessor include user accessible EEPROM?

Second, does your language have EEPROM functions?

in the Arduino world, there is a library that has to be included in your code. The link is to the Arduino reference so you can see how it’s used. The library includes functions to read and write.

In your problem, every time the program starts, it has to read the stored value for hx.

Then, as hx changes and is stable, you have to write the value to EEPROM. This is what I meant when I said to save the value of hx when the loop exits. It’s important to only write when hx is stable, because as others have pointed out, there is a limit to the number of times you can access EEPROM.

Are you catching my drift?
yes im catching you!
i have to find out how to use it, now i know that i have to use eeprom and when to do so i protect the limit off read/write it would be much help if i could find any similar tutorial but haven't find anything yet

thank you!
 

LesJones

Joined Jan 8, 2017
4,174
One way to reduce the number of writes to the EEPROM is to have a large capacitor on the supply rail that will power the microcontroller for long enough to write to the EEPROM. Have diode between the capacitor and the power source so that you can sense when the power is removed. (By using an input on the microcontroller that senses the voltage at the input side of the diode.) When power loss is detected it can interrupt the program and write the required data to EEPROM. I can post some code for a PIC16F628A in assembler that writes some configuration setting to EEPROM and reads them back at power up. Let me know if that would be any help to you.

Les.
 

JohnInTX

Joined Jun 26, 2012
4,787
The while loop exits when the Reed switch is opened. So, why don’t you save hx after the loop exits?
Works for me, too. Count up how often you actually modify the setup vs. the EEPROM write cycle limit. You probably don't have to worry about it if you just save once after selecting.
MikroC provides PIC library routines that provide basic access to the EEPROM. I'd start with those.

C:
unsigned short EEPROM_Read(unsigned int address); 
void EEPROM_Write(unsigned int address, unsigned short data);
 

djsfantasi

Joined Apr 11, 2010
9,156
Works for me, too. Count up how often you actually modify the setup vs. the EEPROM write cycle limit. You probably don't have to worry about it if you just save once after selecting.
MikroC provides PIC library routines that provide basic access to the EEPROM. I'd start with those.

C:
unsigned short EEPROM_Read(unsigned int address);
void EEPROM_Write(unsigned int address, unsigned short data);
The Arduino library also includes an EEPROM.update() function. It does a read and only performs a write if the current value is different from the stored value. Easy to implement in code with a simple if statement.
 

JohnInTX

Joined Jun 26, 2012
4,787
The Arduino library also includes an EEPROM.update() function. It does a read and only performs a write if the current value is different from the stored value. Easy to implement in code with a simple if statement.
Good point. I was going to suggest that as part of the whole approach but didn't to keep it simple.
 
Top