Still Struggling with IF THEN


Closed Thread
Results 1 to 12 of 12

Hybrid View

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

    Default Still Struggling with IF THEN

    I have an LED on GPIO.0, I want it to light when GPIO.1 is low. In this program the LED comes on for 1 second at the beginnig, goes off for 1/2 sec then comes on and stays on. It does not matter if GPIO.1 is high or low. How do I make it work only when GPIO.1 is low?
    rem device = 12F675

    high GPIO.0
    PAUSE 1000

    ANSEL = 0 'GPIO.0 TO GPIO.3 SET AS DIGITAL
    OPTION_REG = 0 'WEAK PULLUPS ENABLED
    GPIO = %00111110 'PORT GPIO.0 SET LOW INITIALLY
    WPU = %00110110 'SETS WEAK PULLUP ON DIGITAL INPUTS


    rem define variables
    time1 var byte: time2 var byte: time4 var byte: time8 var byte
    time var word
    LOW GPIO.0
    PAUSE 500
    IF NOT GPIO.1 THEN
    HIGH GPIO.0
    ELSE
    LOW GPIO.0
    ENDIF
    END

  2. #2
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    The first thing that appears to me to be missing is the config settings for the device

    @ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON

    I would also suggest that you use external pullup resistors of around 10K rather than rely on the internal WPU

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


    Did you find this post helpful? Yes | No

    Default

    Thanks. the internal pullup seems to be working OK. I set the configuration at programming time, I thought that was sufficient?

  4. #4


    Did you find this post helpful? Yes | No

    Default Just curious

    What if instead of

    IF NOT GPIO.1 THEN

    You use

    IF GPIO.1=0 THEN

    ? just curious to see if there's any difference



    picster

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by picster View Post
    What if instead of

    IF NOT GPIO.1 THEN

    You use

    IF GPIO.1=0 THEN

    ? just curious to see if there's any difference



    picster
    Yes, I tried that, no difference.

  6. #6
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    I always find it good practice to set the config bits in the code..

    The following turns on an LED on GPIO.0 when GPIO.1 is grounded (external 10K pullup) - Hope it helps
    Code:
    @ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON
    
    ANSEL = 0
    CMCON=7
    TRISIO = %101010
    
    LED    var GPIO.0
    
    main:
    IF GPIO.1=0 THEN
         LED=1
            ELSE
         LED=0
    ENDIF
    goto main:

  7. #7
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    It may not be the most structured of codes, but this also demonstrates the IF...Then...Else option

    Code:
    @ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON
    
    ANSEL=0                 
    CMCON=7                 
    TRISIO = %101010
    
    LED    var GPIO.0
    
    main:
    IF GPIO.1=0 THEN
    goto flash
    ELSE
    LED=0
    ENDIF
    goto main:
    
    Flash:
    LED=1
    If GPIO.1=1 then main:
    pause 100
    LED=0
    pause 100
    goto flash:
    I think your original problem was in setting the PIC up as digital and turning off the analogue functions, hence the ANSEL and CMCON settings

    Wow... I'm only a newbie and I've managed to pass on some advice... maybe my status should change from newbie to novice

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by malc-c View Post
    I always find it good practice to set the config bits in the code..

    The following turns on an LED on GPIO.0 when GPIO.1 is grounded (external 10K pullup) - Hope it helps
    Code:
    @ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON
    
    ANSEL = 0
    CMCON=7
    TRISIO = %101010
    
    LED    var GPIO.0
    
    main:
    IF GPIO.1=0 THEN
         LED=1
            ELSE
         LED=0
    ENDIF
    goto main:
    Thanks, that works, now if I can work it into my program! I think the thing that makes it work is CMCON=7. I tried that earlier but misspelled it so got an error.

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


    Did you find this post helpful? Yes | No

    Default

    The IF THEN statements compile OK if I comment out the TRISIO line. The error message is: Warning line 15: $111110 numeric overflow. TRISIO never gave me a problem before, what is wrong?

    rem device = 12F675
    CMCON = 7
    ANSEL = 0 'GPIO.0 TO GPIO.3 SET AS DIGITAL
    OPTION_REG = 0 'WEAK PULLUPS ENABLED
    TRISIO = $111110 'GPIO.0 SET AS OUTPUT, ALL OTHERS INPUT
    GPIO = %111110 'PORT GPIO.0 SET LOW INITIALLY
    WPU = %110110 'SETS WEAK PULLUP ON DIGITAL INPUTS
    rem define variables
    time1 var byte: time2 var byte: time4 var byte: time8 var byte
    time var word
    IF GPIO.1 = 0 THEN
    time1 = 1
    ELSE
    TIME1 = 0
    ENDIF
    END

Similar Threads

  1. calculator-like code entry with matrix keypad and display
    By Dennis in forum mel PIC BASIC Pro
    Replies: 35
    Last Post: - 16th December 2009, 22:58
  2. Old analog guy needs help with PIC16F57
    By yankee in forum mel PIC BASIC Pro
    Replies: 31
    Last Post: - 6th March 2009, 13:32
  3. Replies: 5
    Last Post: - 28th June 2006, 21:32

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