Help with code, button


Closed Thread
Results 1 to 12 of 12
  1. #1
    Join Date
    Jul 2007
    Posts
    48

    Default Help with code, button

    When I give PORTA.1 +5V, LED1 dosent shine..

    Whats wrong with my code?

    Code:
    LED1 var PORTB.1
    PRESS var PORTA.1
    
    loop:
    
    If press = 1 then       
                high led1   
                else
                low led1   
                endif      
                goto loop   
                end

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Jul 2007
    Posts
    48


    Did you find this post helpful? Yes | No

    Default

    I dont know its a PIC16F648A

  4. #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    According to the data sheet it has two comparators.
    The above links will explain why
    CMCON=7
    is needed.
    Dave
    Always wear safety glasses while programming.

  5. #5
    Join Date
    Jul 2007
    Posts
    48


    Did you find this post helpful? Yes | No

    Default

    Ok, thanks.

    But it still dosent work as I want it :/

    Code:
    @   device  pic16F648A, hs_osc, wdt_off, mclr_on, lvp_off, protect_off
    DEFINE OSC 20
    
    LED1 var PORTB.1
    PRESS var PORTA.1
    CMCON=7
    
    
    loop:
    
    If press=1 then       
                high led1  
                else
                low led1  
                endif      
                goto loop   
                end
    When i transfer it LED1 starts to shine directly even if PortA.1 doesnt have +5V

    :/

  6. #6
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Maybe the LED is backwards

    Do you have PORTA.1 tied to ground with a resistor (10K) so it is not floating?
    Dave
    Always wear safety glasses while programming.

  7. #7
    Join Date
    Jul 2007
    Posts
    48


    Did you find this post helpful? Yes | No

    Default

    Floating ?

  8. #8
    Join Date
    Jul 2007
    Posts
    48


    Did you find this post helpful? Yes | No

    Default

    Should it be like this?


  9. #9
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Yes, that is correct.
    Dave
    Always wear safety glasses while programming.

  10. #10
    Join Date
    Jul 2007
    Posts
    48


    Did you find this post helpful? Yes | No

    Default

    I added it and it worked!

    Thanks.

  11. #11
    Join Date
    Jul 2007
    Posts
    48


    Did you find this post helpful? Yes | No

    Default

    If I make the code like tis:

    Code:
    @   device  pic16F648A, hs_osc, wdt_off, mclr_on, lvp_off, protect_off
    DEFINE OSC 20
    
    LED1 var PORTB.1
    PRESS var PORTA.1
    CMCON=7
    
    
    loop:
    
    If press=0 then       
                high led1  
                else
                low led1  
                endif      
                goto loop   
                end
    Should it be like this?


  12. #12
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by xobx View Post
    If I make the code like tis:

    Code:
    @   device  pic16F648A, hs_osc, wdt_off, mclr_on, lvp_off, protect_off
    DEFINE OSC 20
    
    LED1 var PORTB.1
    PRESS var PORTA.1
    CMCON=7
    
    
    loop:
    
    If press=0 then       
                high led1  
                else
                low led1  
                endif      
                goto loop   
                end
    Should it be like this?

    No. The first way is what you want. When the switch is open the pin is "0"LOW. When the switch is closed the pin is "1"HIGH.

    If you are using push buttons, you will need to use either a normally open "NO" or a normally closed "NC".

    The resistor keeps the pin LOW (no float). High resistance is used to minimize the current through the switch (just a little short).

    Depending on how you look at it this is called a pull up or a pull down.
    Dave
    Always wear safety glasses while programming.

Similar Threads

  1. TSA5512/5511 code for PIC16F877/84
    By Marin in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 24th August 2013, 06:16
  2. How much code space do PBP statements use.
    By Darrel Taylor in forum Code Examples
    Replies: 5
    Last Post: - 13th February 2009, 21:31
  3. Loop with two motor and 2 sensors
    By MrRoboto in forum mel PIC BASIC
    Replies: 4
    Last Post: - 8th December 2008, 23:40
  4. 3 HPWM channels
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 4th April 2006, 02:43
  5. Code check -- button not working
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 2nd March 2006, 22:43

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