if-then statements


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2006
    Location
    Melbourne, Australia
    Posts
    25

    Exclamation if-then statements

    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
    IF ITS STOCK IT WONT ROCK

  2. #2
    sinoteq's Avatar
    sinoteq Guest


    Did you find this post helpful? Yes | No

    Default

    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

  3. #3
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    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 ?
    Code:
    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
    Last edited by Archangel; - 25th August 2007 at 08:00.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  4. #4
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    How about;
    Code:
    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}
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  5. #5
    Join Date
    Nov 2006
    Location
    Melbourne, Australia
    Posts
    25


    Did you find this post helpful? Yes | No

    Thumbs up

    thanks everyone it works. i dont know what i was thinking when i wrote the code in the first place.

    crazy cooter
    IF ITS STOCK IT WONT ROCK

Similar Threads

  1. How much code space do PBP statements use.
    By Darrel Taylor in forum Code Examples
    Replies: 5
    Last Post: - 13th February 2009, 21:31
  2. Multiple IF-THEN statements
    By DavidK in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 20th June 2007, 18:28
  3. Proton Commands & statements
    By Lotondo in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 6th November 2006, 23:37
  4. time for PBP statements
    By fnovau in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 25th October 2006, 19:42
  5. If - then statements
    By Russ Kincaid in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 18th May 2006, 22:57

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts