If - then statements


Closed Thread
Results 1 to 13 of 13

Hybrid View

  1. #1
    Join Date
    Jan 2006
    Location
    New Hampshire, USA
    Posts
    107

    Default If - then statements

    This is my second PIC program, and I am having a problem. The IF - THEN statements look OK to me, but the compiler doesn't like it.

    'device: 16F627A
    S3 var portb.1 ' S3 is the timer output
    S4 var portb.2 ' S4 determins whether short time or
    ' long time is measured. 0=short, 1=long
    T var byte

    green var portb.4 'Drives the green LED (physical pin 10)
    red var portb.5 'Drives the red led (physical pin 11)
    input s3
    input s4
    high green : high red 'LEDs are initally off
    loop:
    if s3=1 then time
    goto loop

    time:
    if S4=0 then t =500 : endif
    if s4 =1 then t = 5000 : endif
    pause t
    if t = 500 and s3 = 1 then bad : endif
    if t = 500 and s3 = 0 then good : endif
    * if t = 5000 and s3 = 0 then bad : endif
    if t = 5000 and s3 = 1 then good :endif
    stop ' should never get here!

    In the line with the *, the compiler says that "then" is missing. Why does it not see it? the compiler does not like any of the IF - THEN statements. I did not use "endif" originally but got an error " endif missing".

  2. #2
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    966


    Did you find this post helpful? Yes | No

    Default

    Where are the definitions of good and bad? Perhaps the compiler cannot see them. Is this the entire code you posted or just a fragment?

    Jerson

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Hi Russ,

    When using Inline IF statements, you don't need the ENDIF.

    It's either

    if S4=0 then t =500

    -or-

    if S4=0 then
        t =500
    endif

    <br>
    DT

  4. #4
    Join Date
    Jan 2006
    Location
    New Hampshire, USA
    Posts
    107


    Did you find this post helpful? Yes | No

    Default

    Thanks, Darrel, I removed the endif and now the compiler is happy and so am I. I guess PicBasic Pro Demo does not support "else" which got me off track in the first place.

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    I've never used the DEMO version of PicBasic Pro. So, I can't really say for sure, but, It doesn't seem like it would be much of a DEMO if it couldn't even do IF-ELSE-ENDIF's.

    Maybe there was a problem there too.

    This should work.
    Code:
    IF S4=0 then 
        t =500
    ELSE
        t =5000
    ENDIF
    This will Not.
    Code:
    IF S4=0 then t =500 : ELSE t = 5000 : ENDIF
    DT

  6. #6
    Join Date
    Jan 2006
    Location
    New Hampshire, USA
    Posts
    107


    Did you find this post helpful? Yes | No

    Default

    Thanks, Darrel. I have another question: My timing was way off so I measured the frequency at clkout, it was 2.28 mHz instead of 1.0. I can adjust T to compensate, but is there a way to reset the oscillator?

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. if-then statements
    By CrazyCooter in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 26th August 2007, 05:28
  3. Multiple IF-THEN statements
    By DavidK in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 20th June 2007, 18:28
  4. Proton Commands & statements
    By Lotondo in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 6th November 2006, 23:37
  5. time for PBP statements
    By fnovau in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 25th October 2006, 19:42

Members who have read this thread : 0

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