IF-THEN issue


Closed Thread
Results 1 to 6 of 6

Thread: IF-THEN issue

  1. #1
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305

    Default 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

  2. #2
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default 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

  3. #3
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305


    Did you find this post helpful? Yes | No

    Default 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.

  4. #4
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default 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?

  5. #5
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305


    Did you find this post helpful? Yes | No

    Default 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.

  6. #6
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default 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.

Similar Threads

  1. DS1307 issue
    By Scampy in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 30th October 2013, 13:54
  2. I2C issue
    By Charles Linquis in forum mel PIC BASIC Pro
    Replies: 16
    Last Post: - 18th October 2011, 22:53
  3. 12F675 - another issue
    By fratello in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 26th August 2011, 19:26
  4. Code Issue - select case or 'if' issue - not sure why
    By jamie_s in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 7th October 2007, 08:52
  5. 16F877A issue
    By DynamoBen in forum Off Topic
    Replies: 9
    Last Post: - 31st August 2007, 04:15

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