Button help


Closed Thread
Results 1 to 22 of 22

Thread: Button help

Hybrid View

  1. #1
    darklight's Avatar
    darklight Guest

    Default Button help

    Hey people.

    I'm new to microcontrolers and picbasic.
    I was just wondering if anyone could help me with switchs.
    I can already output leds from portB.

    PIC = 16F627

    I'm using a programmer / test board (VELLEMAN VM111) to test buttons and leds.

    The leds = 6 on portB
    Buttons = 4 on portA

    Thanks for any help.

  2. #2
    blainecf's Avatar
    blainecf Guest


    Did you find this post helpful? Yes | No

    Default Checklist for buttons

    Make sure:

    1. ADCON1 = 7 so that your PORTA pins are digital

    2. Set the appropriate TRISA.x = %1 for Digital Input

    3. Wire a pull-up resistor (10K from V+ to each pin that will sense the switch)

    4. Wire each switch, one side ground, the other to the pin from step #3

    5. Set up a loop for polling the ports; High (%1) will be normal, and Low (%0) will show when the switch is held closed.

    If you're not used to digital signals, it seems weird to ground out a resistor thats hooked to V+, but just accept it.

    Regards,

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default MIIIP something's wrong :)

    ADCON1=7 have nothing usefull to do with the 16F627 but compilation error. There's no ADCs in the PIC16F627, just analog comparator.

    CMCON=7
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4
    homebrewer's Avatar
    homebrewer Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mister_e
    ADCON1=7 have nothing usefull to do with the 16F627 but compilation error. There's no ADCs in the PIC16F627, just analog comparator.

    CMCON=7
    I'm a beginner too. I had also problems to query PORTA. The tip with CMON=7 was the solution. Steve, thank you very much!
    _______________
    Regards
    Johann
    Last edited by homebrewer; - 22nd August 2006 at 00:00.

  5. #5
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Meeee Tooo

    Attention fellow newbies:
    Here's what I learned the hard way, twice!
    When your button code calls a LABEL,
    put that labeled code in the very next line after your button code,
    or you get nasty error codes assuring you that it is not really a label.

    I ran into this in a program with several sub routines and several buttons
    and the only way it would compile was if I put the subs directly after the button.

  6. #6
    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

  7. #7
    Join Date
    Jan 2008
    Location
    lincoln uk
    Posts
    20


    Did you find this post helpful? Yes | No

    Default polling portb

    On the side, could you tell me the correct syntax to poll portb in a for..next loop?
    viz
    FOR i = 0 to 7
    IF portb.[i]=0 THEN GOSUB alarm
    NEXT i
    This doesn't work and I can't find the answer in the book


    Quote Originally Posted by blainecf View Post
    Make sure:

    1. ADCON1 = 7 so that your PORTA pins are digital

    2. Set the appropriate TRISA.x = %1 for Digital Input

    3. Wire a pull-up resistor (10K from V+ to each pin that will sense the switch)

    4. Wire each switch, one side ground, the other to the pin from step #3

    5. Set up a loop for polling the ports; High (%1) will be normal, and Low (%0) will show when the switch is held closed.

    If you're not used to digital signals, it seems weird to ground out a resistor thats hooked to V+, but just accept it.

    Regards,

  8. #8
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by med2007 View Post
    On the side, could you tell me the correct syntax to poll portb in a for..next loop?
    viz
    FOR i = 0 to 7
    IF portb.[i]=0 THEN GOSUB alarm
    NEXT i
    This doesn't work and I can't find the answer in the book
    http://www.picbasic.co.uk/forum/showthread.php?t=544

  9. #9
    Join Date
    Jan 2008
    Location
    lincoln uk
    Posts
    20


    Did you find this post helpful? Yes | No

    Default polling portb

    Thanks for the link...so my syntax should be?

    FOR i = 0 to 7
    IF portb.0(i) = 0 THEN GOSUB alarm
    NEXT i

    I'll give it a whirl.
    Rgds
    MED2007

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