-
IF-THEN issue
I have a simple program that jumps to a few subroutines, looks for a high on a pin, sends out data to a digital pot and goes back to the main loop.
I'm having an issue that if the pin is high, it should turn on another pin to light an indicator LED. This isnt happening but the data is being sent out to the pot. If I strip away all code except that to turn on the LED, the LED will turn on.
PORTB.7 is the input pin, PORTB.6 is the output to the LED.
The sub routine(s) that are causing issues are below (there are 5 subroutines, all identical except the POT0 value is different.)
Code:
POWER1:
IF PORTB.7 = 1 THEN
PORTB.6 = 1 AND POT0 = 25
ELSE
PORTB.6 = 0 AND POT0 = 0
ENDIF
LOW CS
PAUSE 1
SHIFTOUT SDO,SDK,5, [POT0\16]
HIGH CS
PAUSE 1
RETURN
-
Re: IF-THEN issue
Your use of AND is unusual.
IF PORTB.7 = 1 THEN
PORTB.6 = 1 AND POT0 = 25
ELSE
PORTB.6 = 0 AND POT0 = 0
ENDIF
I would use
IF PORTB.7 = 1 THEN
PORTB.6 = 1
POT0 = 25
ELSE
PORTB.6 = 0
POT0 = 0
ENDIF
-
Re: IF-THEN issue
That creates a problem because if the pin drops lows after its exited the routine, the LED and POT0 value do not change.
I suppose if I add an IF THEN check in the main program loop to see if the pin is low and turn off the led and send 0 to the POT, that should fix that issue.
-
Re: IF-THEN issue
Not a problem because
IF PORTB.7 = 1 THEN 'If portb.7 is 1
PORTB.6 = 1 'set portb.6 to 1
POT0 = 25 'set poto to 25
ELSE 'If portb.7 is any value but 1
PORTB.6 = 0 ' set portb.6 to 0
POT0 = 0 'set poto to 0
ENDIF
how do you interpret this bit of code?
-
Re: IF-THEN issue
Jump into the subrountine, run that code once, jump out, if the pin turns low, the led stays on because the IF THEN code isnt being run. Therefore there has to be IF THEN code in the main program loop to detect if PORTB.7 = 0 and turn off the LED and send 0 to the POT.
-
Re: IF-THEN issue
Very interesting. In your main code you have an IF statement to detect PORTB.7 =1 and another IF statement to detect PORTB.7=0.
or you could just call the subroutine from the main program as it does both checks.