Button help


Closed Thread
Results 1 to 22 of 22

Thread: Button help

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default

    I can't make it work with the BUTTON command.

    So I tried the other way and use the simple IF..THEN command.

    But again, I have the same unexpected result. The Led starts to blink when I press the button. As I release the button, it never "toggles" as I want it to do.

    Since this program behavior is the same in both cases (BUTTON and IF..THEN commands), I assume something is wrong with some register settings.

    I was thinking about a bouncing problem, but I'm not sure about that.

    Here is my code; where do I miss something?

    'PIC 16F88
    'Button is on PORTB.3 (Pin 9) Pressed = 0V
    'Led is on PORTB.4 (Pin 10)

    OSCCON = %01100000 'Internal RC set to 4MHZ
    TRISB = %00001000 'RB3 is an Input, all others are Outputs
    '----------------------------------------------------------------

    ' IF..THEN
    MAIN:
    IF PORTB.3 = 0 then SWITCH_LED
    goto main

    SWITCH_LED:
    pause 100 'Debounce
    toggle PORTB.4
    goto main

    end

    '----------------------------------------------------------------
    ' BUTTON

    'BVar var byte
    'BVar = 0
    '
    'MAIN:
    ' BUTTON 9, 0, 255, 255, BVar, 0, SWITCH_LED
    ' goto main
    '
    'SWITCH_LED:
    ' pause 100 'Debounce
    ' toggle PORTB.4
    ' goto main

    END
    Roger

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    MAIN:
    IF PORTB.3 = 0 then SWITCH_LED
    goto main

    SWITCH_LED:
    pause 100 'Debounce
    toggle PORTB.4
    goto main

    So what happens above when you keep your finger pressed on the button for longer than 100mS? Your LED will keep toggling every 100mS...

    Try this...

    MAIN:
    IF PORTB.3 = 0 then SWITCH_LED
    goto main

    SWITCH_LED:
    pause 100 'Debounce
    toggle PORTB.4
    While PortB.3=0:Wend ' Wait here for Button to be released
    goto main

  3. #3
    homebrewer's Avatar
    homebrewer Guest


    Did you find this post helpful? Yes | No

    Default

    ' Try CMCON=7 as mister_e has stated in reply#3
    ' now you can query input pins
    ' from this point it's up to you, how your led should work

    CMCON=7

    MAIN:
    IF PORTB.3 = 0 then SWITCH_LED
    goto main

    SWITCH_LED:
    pause 100 'Debounce
    toggle PORTB.4
    goto main

    end


    ' Regards
    ' Johann

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


    Did you find this post helpful? Yes | No

    Default

    CMCON has no effect on PortB on this PIC.

  5. #5
    homebrewer's Avatar
    homebrewer Guest


    Did you find this post helpful? Yes | No

    Default

    Hello Melanie,
    you are very right. I have looked into the datasheet after my reply.
    I was too overhasty - sorry!

  6. #6
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Appologies are unnescessary.

    flotulopex reported that his LED blinks when he pushes the button, that's because there's only a 100mS Pause in the loop and you toggle the LED-ON, and with your finger still on the button, 100mS later you toggle it OFF, and with your finger still on the button, the LED toggles again 100mS later... etc etc until the end of time.

    You need to ensure that once the LED toggles, it doesn't do so again until the Button is pressed anew. There are MANY, MANY ways of doing this. The example I posted simply waits for the user to remove his finger from the button before proceeding further.

  7. #7
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default

    Thank you Melanie,

    Actually, it still doesn't work.

    Each time I press the button, the Led lights up and as soon as I release the button, the Led switches off. At least, it doesn't blink anymore while the button is pressed.

    It just doesn't want to toggle!

    To make sure my button is correctly connected, is this schematic okay?
    > Vdd-R10k-PORTB.3 and the button (normally open) is derived from PORTB.3 to Vss.

    Since I can't make it work with this simple program, my last guess would be that my PIC is defect. I tried with other ports but there is no change. I shall receive another PIC tomorrow.

    By the way, is there any utility tool to check the PIC's health?
    Roger

Similar Threads

  1. Sony SIRC IR Issue
    By Ryan7777 in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 8th August 2015, 08:10
  2. 3 HPWM channels
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 4th April 2006, 02:43
  3. Code check -- button not working
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 2nd March 2006, 22:43
  4. Button Push within 3 second Window
    By Tissy in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 22nd December 2005, 10:06
  5. Button subfunction 16F628
    By Jųan in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 19th August 2005, 16:44

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