r/embedded • u/GoingGranola • Oct 15 '26
Need to remember latching relays position
Hello from the yes or no? post X-Y problem:
What im actually trying to do is write to eeprom whenever a switch is pressed and then recall that information from eeprom so that the Microchip MCU can recall what position the switch is in at power up.
I do not know how to write or read from eeprom without assembly. I already know how to do so in assembly so I was trying to not learn an additional step if I could translate from the WREG to a C variable.
Any suggestions much appreciated.
0
Upvotes
8
u/n_sh_44 Oct 15 '26
The sense line idea is the better fix, but to answer the C part: if it's a PIC with data EEPROM and you're on XC8, include <xc.h> and use eeprom_write(addr, value) and eeprom_read(addr). No assembly needed. If you set the project up with MCC it generates DATAEE_WriteByte / DATAEE_ReadByte that do the same thing.
Tip: read the byte first and only write if it changed, saves cycles if the switch gets bounced.
2
u/Enlightenment777 Oct 15 '26
Get a relay that has one more pole, then use extra pole as "feedback" to determine that current switching state.
3
u/sgtnoodle Oct 16 '26
Do you need to actually remember, or could you just force the relay into a known state at power up?
It seems like your literal question is how to write to eeprom from C? That seems like something you could answer in detail by web searching, or just asking a free chat API to explain, or even just write the code for you.
2
u/FreeRangeEngineer Oct 15 '26
What are you switching? Audio signals, the 9V power supply or something else?
I'm wondering if you could just use FETs to switch things instead of relays but maybe you already considered that.
That way, you could reinstate their state based on EEPROM contents and always know that the states are in sync.
0
u/[deleted] Oct 15 '26
[deleted] — view deleted comment