Log in

View Full Version : How can I do this (reversing high & low states depending on deployment & user need)



HankMcSpank
- 16th May 2012, 23:24
Forgive the esoteric subject but it's hard to articulate what I'm trying to resolve.

I have a MOSFET under PIC control.....a mosfet gate is normally low but when a tactile switch is pressed, a PIC interrupt happens & a PIC output pin goes high & switches the MOSFET on - no worries thus far.

*but* occasionally, depending on the deployment (& I won't know in advance so it'll be down to the end user to establish) I will need the above scenario to reversed & this will then be the default .....therefore the MOSFET gate will normally be high & when a tactile switch is pressed an interrupt is triggered and a PIC output pin goes low & switches the MOSFET off.

So what I'm thinking here is when the user deploys & it doesn't want the default...that he can change it.

But what I don't want is a massive long program to cater for both scenarios...I'm thinking that if the user holds a switch prior to powering up, then the 'default' can be selected to the non default (in other words, a toggle).

You following this at the back?

in summary, this is what I'm trying to achieve....

factory_default...
mosfet_gate = low
when switched pressed mosfet_gate = high

user_selectable_new_default....
mosfet_gate = high
when switched pressed mosfet_gate = low

How do I tackle this in an efficient way?

pedja089
- 16th May 2012, 23:38
Try something like this:

IF factory_default THEN
StateOn=1
StateOff=0
ELSE
StateOn=0
StateOff=1
ENDIF

IF SwitchPressed THEN Gate=StateOn
IF SomeThingElse THEN Gate=StateOff

gadelhas
- 17th May 2012, 00:08
I probably do it like pedja089, but used the eeprom to save the setting!

Darrel Taylor
- 17th May 2012, 00:54
EE_Polarity DATA 0 ; Factory Default
SW1 VAR PORTB.0 ; Switch input pin
mosfet_gate VAR PORTB.1 ; mosfet output
Polarity VAR BIT

READ EE_Polarity, Polarity
IF !SW1 THEN WRITE EE_Polarity, !Polarity
READ EE_Polarity, Polarity

mosfet_gate = 1 ^ Polarity ; Turn OFF
OUTPUT mosfet_gate
;----------------------------------------------

mosfet_gate = 0 ^ Polarity ; Turn ON
mosfet_gate = 1 ^ Polarity ; Turn OFF

HankMcSpank
- 17th May 2012, 09:18
Thanks to everyone...embarrasingly simple in the end! (hey, it was late!)

Hi Darrel, I was with you up to this bit....



EE_Polarity DATA 0 ; Factory Default
mosfet_gate = 1 ^ Polarity ; Turn OFF
OUTPUT mosfet_gate
;----------------------------------------------

mosfet_gate = 0 ^ Polarity ; Turn ON
mosfet_gate = 1 ^ Polarity ; Turn OFF


Can anyone shed a little light on what's going on there (never used a ^ before!)

pedja089
- 17th May 2012, 10:14
That is XOR.
0^0=0
0^1=1
1^0=1
1^1=0
If Polarity is 1 or 0, depending on user settings. If Polarity is 0 then bit isn't inverted and result is same as:
mosfet_gate = 0 ; Turn ON
mosfet_gate = 1 ; Turn OFF
If Polarity is 1 then then bit are inverted so result is
mosfet_gate = 1 ; Turn ON
mosfet_gate = 0 ; Turn OFF