PDA

View Full Version : if-then statements



CrazyCooter
- 25th August 2007, 05:27
HI all i am have a challenging time with my code here.
am using 5 if -then statements to read the ports status in digital. My problem is i can read each port on its own but not the whole lot grouped together.

Pic 16f88 int osc 8MHz
PBP 2.47
Melabs U2 programmer

my code is as follows


OSCCON = %01111000 ' INTRC = 8MHz
'OSCON = %01101000 4 MHz
TRISA = %11111111
ANSEL = 0 'DISABLE ALL ADC
CMCON = 7 'DISABLE ALL ANALOG COMPARATOR
WHILE OSCCON.2=0:WEND 'STABILISE THE CLOCK
ADCON1 = 1
DEFINE OSC 8
PAUSE 1000
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 4 '4,5,6,7
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 3
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 2
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
pause 1000
lcdout $fe, 1
TRISA.0=1 'INPUT PIN PUMP ON OFF
TRISA.1=1 'INPUT PIN DALLAS 1 WIRE DS18S20
TRISA.2=1 'INPUT PIN WATER SENSOR
TRISA.6=1 'INPUT PIN TEMP BUTTON DOWN
TRISA.7=1 'INPUT PIN TEMP BUTTON UP
TRISA.3=0 'OUTPUT PIN na
TRISA.4=0 'OUTPUT PIN na
TRISA.5=0 'OUTPUT PIN na
TRISB.0=0 'OUTPUT PIN IGNITION
TRISB.1=0 'OUTPUT PIN GAS VALVE
low PORTB.0
low PORTB.1



MAIN:
lcdout $fe, $80+0, "PUMP off GAS off"
lcdout $fe, $c0+8, "WATER"
if PORTA.2 = 1 then GOTO waterok
if PORTA.2 = 0 then GOTO waterbad
if PORTA.6 = 1 then GOTO tempdown
if PORTA.7 = 1 then GOTO tempup
PAUSE 500
GOTO MAIN

waterok:
lcdout $fe, $c0+13, " OK"
goto main

waterbad:
lcdout $fe, $c0+13, "BAD"
goto main


tempdown:

lcdout $fe, $80+0, "TEMP DOWN "
goto main

tempup:

lcdout $fe, $80+0, "TEMP UP "
goto main
END

why cant i get this to work. it looks logical.
oh b4 i forget the MCLR is set as an input and the osc is set as INTRC with the u2 programmer

Thanks

crazy cooter

sinoteq
- 25th August 2007, 06:44
Look at what you have written and you will see why it does not work. Here is a short hint.

MAIN:
lcdout $fe, $80+0, "PUMP off GAS off"
lcdout $fe, $c0+8, "WATER"
if PORTA.2 = 1 then GOTO waterok
if PORTA.2 = 0 then GOTO waterbad
if PORTA.6 = 1 then GOTO tempdown
if PORTA.7 = 1 then GOTO tempup
PAUSE 500
GOTO MAIN

waterok:
lcdout $fe, $c0+13, " OK"
goto main

The problem is that you jump back to main all the time so in this case you will not do the other IF statements. This is why we have GOSUB in PBP, it jumps to a SUB and when you use RETURN you will jump back to the line after..... More or less in any case.

SO

MAIN:
lcdout $fe, $80+0, "PUMP off GAS off"
lcdout $fe, $c0+8, "WATER"
if PORTA.2 = 1 then GOSUB waterok '<------------------- JUMP if true
if PORTA.2 = 0 then GOSUB waterbad
if PORTA.6 = 1 then GOSUB tempdown
if PORTA.7 = 1 then GOSUB tempup
PAUSE 500
GOTO MAIN

waterok:
lcdout $fe, $c0+13, " OK"
RETURN '<------ to exit the SUB and return to MAIN



Get the idea??

M

Archangel
- 25th August 2007, 07:58
Hi C.C.,
this might be an opportunity to use select case.

I noticed you are using IF THEN to direct functions for each state or your PortA.2 inputs, why not use ELSE ?


main:
If PortA.2 = 0 then
gosub waterok
ELSE
Gosub Waterbad
if PORTA.6 = 1 then ' assumes you have pulldown resistor
GOSUB tempdown
if PORTA.7 = 1 then 'assumes you have pulldown resistor
GOSUB tempup
ENDIF
endif
endif
goto main
waterok:
lcdout $fe, $c0+13, " OK"
return

waterbad:
lcdout $FE,$c0+13, " NASTY"
return

tempdown:

lcdout $fe, $80+0, "TEMP DOWN "
RETURN

tempup:

lcdout $fe, $80+0, "TEMP UP "
RETURN
END

HTH
JS

Bruce
- 25th August 2007, 13:46
How about;


MAIN:
lcdout $fe, $80, "PUMP off GAS off"
lcdout $fe, $c0+8, "WATER"

if PORTA.2 = 1 then
lcdout $fe, $c0+13, " OK "
else
lcdout $fe, $c0+13, " BAD"
endif

if PORTA.6 = 1 then lcdout $fe, $80, "TEMP DOWN "
if PORTA.7 = 1 then lcdout $fe, $80, "TEMP UP "
PAUSE 500
GOTO MAIN
END
And maybe: if (PORTA.6=1) AND (PORTA.7=1) then lcdout $fe,$80," FAT FINGERS "..;o}

CrazyCooter
- 26th August 2007, 05:28
thanks everyone it works. i dont know what i was thinking when i wrote the code in the first place.

crazy cooter