button problems


Closed Thread
Results 1 to 4 of 4

Thread: button problems

  1. #1
    Join Date
    Oct 2005
    Posts
    14

    Unhappy button problems

    hello, i wrote a program for pic16F876 to do the following:

    press button RB7 and led RB1 wil go on and stay on no matter how long you push RB7.
    if you release RB7 the led RB1 will stay on,
    if you push the RB7 again, the led will go off again , no matter how long you push RB7

    This works perfectly standalone, but when i put it in a SUB and call it from the main, it doesn't work properly anymore,
    So push RB7 gosub flipflop, push RB6 gosub onoff (turn led B0 on).
    I used pulldown resistors, so that ain't the problem

    I'm puzzled:

    here is the working program:


    trisb=%11110000
    yy var byte
    yy=2
    portb=0

    start:


    if yy=2 and portb.7=0 then
    yy=10
    endif

    if yy=10 and portb.7=1 then
    yy=20
    portb.1=1
    endif

    if yy=20 and portb.7=0 then
    yy=30
    endif

    if yy=30 and portb.7=1 then
    yy=40
    portb.1=0
    endif

    if yy=40 and porta.7=0 then
    yy=2
    endif

    goto start


    '------------------------------

    and now the subroutine version (Can't get it to work!!)


    trisb=%11110000
    yy var byte
    yy=2
    portb=0
    start:

    if portb.7=1 then
    gosub flipflop
    endif

    if portb.6=1 then
    gosub onoff
    else
    portb.0=0
    endif

    goto start


    flipflop:
    if yy=2 and portb.7=0 then
    yy=10
    endif

    if yy=10 and portb.7=1 then
    yy=20
    portb.1=1
    endif

    if yy=20 and portb.7=0 then
    yy=30
    endif

    if yy=30 and portb.7=1 then
    yy=40
    portb.1=0
    endif

    if yy=40 and porta.7=0 then
    yy=2
    endif

    goto start

    onoff:
    portb.0=1
    goto start

    '===========================

    Who can help me out here ???

    greetz, Maus

  2. #2
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    I dont understand what all this stuff with yy is for.

    Why not use something like this:

    Code:
    if portb.7=1 then                      ' If button is pressed:
        toggle portb.1                     ' Toggle the LED
        loop: if portb.7=1 then goto loop  ' Wait until the button is released
    endif

  3. #3
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    anyway, the reason for your subroutine not working is becuase this statement will never be true: "if yy=2 and portb.7=0 then", becuase you only jump to the subroutine when portb.7=1.

    Instead of this:

    if portb.7=1 then
    gosub flipflop
    endif

    Try just this: "gosub flipflop"

    Also, there is no return from the subroutine... instead of "goto start", put "return"
    Last edited by Kamikaze47; - 22nd November 2005 at 10:10.

  4. #4
    Join Date
    Oct 2005
    Posts
    14


    Did you find this post helpful? Yes | No

    Default

    simple and short

    thanx, works like a charm


    greetz, Maus

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