PDA

View Full Version : IF-THEN issue



jmgelba
- 12th February 2014, 20:05
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.)





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

EarlyBird2
- 12th February 2014, 21:00
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

jmgelba
- 12th February 2014, 21:29
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.

EarlyBird2
- 12th February 2014, 21:41
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?

jmgelba
- 12th February 2014, 21:56
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.

EarlyBird2
- 12th February 2014, 22:09
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.