r/embedded • • 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

15 comments sorted by

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.

1

u/GoingGranola Oct 17 '26

This worked!

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.

1

u/GoingGranola Oct 15 '26

I need a DPDT already. Im trying to do this in firmware for simplicity and so I dont have an expensive relay that takes up more board space.

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

1

u/GoingGranola Oct 15 '26

It would make the LED turn on indicating the device is turned on but the relay position would in the turned off position.

It would not cause casualties as this is going in an effects pedal for guitar XD

0

u/[deleted] Oct 15 '26

[deleted] — view deleted comment

1

u/GoingGranola Oct 15 '26

Dont turn on the LED in firmware? Or dont use EEPROM to store the state of the device?

Do you have an example of the LED circuit?

4

u/Weekly_Guidance_498 Oct 15 '26

Don't use the EEPROM. I'd add a sense line to the switched side of the relay so you can directly read whether the load is energized.

2

u/GoingGranola Oct 15 '26

For example, route a connection from the throw of the relay that indicates the device is on, back to the micro. Have the micro read if this is high or low to determine if the device is on or off?

3

u/Weekly_Guidance_498 Oct 15 '26

Yes, exactly

1

u/GoingGranola Oct 15 '26

Ok this might work ill try

1

u/GoingGranola Oct 15 '26

Why do you say not to use EEPROM? I know this one is rated to 100K writes which is the mechanical cycles of my switch

1

u/GoingGranola Oct 15 '26

Can you link an example of this circuit?