Shop OBEX P1 Docs P2 Docs Learn Events
One Shot Circuit Done With Software — Parallax Forums

One Shot Circuit Done With Software

ArchiverArchiver Posts: 46,084
edited 2001-05-28 14:42 in General Discussion
Hi Gang:

Got an application going that is 96% complete. The subject is the missing
4%. What I am doing in a subroutine is looking at the status of a
pushbutton. On a 0 - 1 transition, I have the subroutine branch to another
subroutine, otherwise it goes back to the main program and life goes on.

What is presently happening is that when I keep the button pressed, the
program cycles and treats the continuously pressed condition as a 0-1
transition. I have used the PULSIN and BUTTON commands in the subroutine to
try and eliminate the continuously pressed condition without any success.

What I really want the program to do is only execute this part of the
program on a 0-1 transition. The continuosly on condition and the 1-0
transitions would be ignored. I know that I can solve the problem by
inputting the switch into a 555 timer which would output a 10 - 100 msec
pulse to a stamp input (the timer is set up as a one-shot circuit). I could
then use that input pulse in the program but the software approach would be
more practical. I also suspect that someone has already gone through this
headache and solved it; this is also probably a problem that is so simple
that it has me stumped. The other parts of the program were a real bear to
get going but I was able to solve those problems. Thank goodness for the
DEBUG command!

Any help would be appreciated. Thanks and regards,

Gerry Shand
gwshand@c...
(780) 471-0470

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2001-05-22 15:39
    There are several ways to do this. If your routine takes long enough that
    you don't care about switch bounce, try something like this:

    btndown var bit
    btndown=0

    <blah>

    main:
    ' main loop
    if button=1 then btnup ' assume button is input bit 1=up
    ' button is down
    if btndown then btnnotdown
    btndown=1
    gosub mybuttonroutine
    if button=1 then btnup ' in case button already released
    goto btnnotdown

    btnup:
    btndown=0
    btnnotdown:
    << whatever is in main loop >>
    goto main


    I think that would do it.

    Regards,
    Al Williams
    AWC
    * Find out how to get NetPorter software FREE (for a limited time)
    http://www.al-williams.com/awce/netporter.htm


    >
    Original Message
    > From: Shand, Gerry W., Celanese/CA [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=cSBQmY5fLMs5vE4Ww5C8uTrzcyS68gIN6BxLnZHCdpGag1oG86qVfJQjMwkz5O-_PIxKK8OTgmoH-g]gwshand@c...[/url
    > Sent: Tuesday, May 22, 2001 8:47 AM
    > To: 'basicstamps@yahoogroups.com'
    > Subject: [noparse][[/noparse]basicstamps] One Shot Circuit Done With Software
    >
    >
    > Hi Gang:
    >
    > Got an application going that is 96% complete. The subject is the missing
    > 4%. What I am doing in a subroutine is looking at the status of a
    > pushbutton. On a 0 - 1 transition, I have the subroutine branch to another
    > subroutine, otherwise it goes back to the main program and life goes on.
    >
    > What is presently happening is that when I keep the button pressed, the
    > program cycles and treats the continuously pressed condition as a 0-1
    > transition. I have used the PULSIN and BUTTON commands in the
    > subroutine to
    > try and eliminate the continuously pressed condition without any success.
    >
    > What I really want the program to do is only execute this part of the
    > program on a 0-1 transition. The continuosly on condition and the 1-0
    > transitions would be ignored. I know that I can solve the problem by
    > inputting the switch into a 555 timer which would output a 10 - 100 msec
    > pulse to a stamp input (the timer is set up as a one-shot
    > circuit). I could
    > then use that input pulse in the program but the software
    > approach would be
    > more practical. I also suspect that someone has already gone through this
    > headache and solved it; this is also probably a problem that is so simple
    > that it has me stumped. The other parts of the program were a real bear to
    > get going but I was able to solve those problems. Thank goodness for the
    > DEBUG command!
    >
    > Any help would be appreciated. Thanks and regards,
    >
    > Gerry Shand
    > gwshand@c...
    > (780) 471-0470
    >
    >
    >
    >
    > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    >
  • ArchiverArchiver Posts: 46,084
    edited 2001-05-22 16:19
    Shand, Gerry W., wrote:
    >What I really want the program to do is only execute this part of the
    >program on a 0-1 transition. The continuosly on condition and the 1-0
    >transitions would be ignored.

    Hi Gary,

    Another way to do it, as a state machine, focuses the logic at one
    point in the program:

    bold var bit
    bnew var bit
    bx01 var bit

    main:
    bnew=in0 ' get state of button on P0
    bx01=bnew ^ bold & bnew ' detect 01 xtion
    bold=bnew ' update state variable
    if bx01 then dobuttondownstuff
    ' do other main stuff
    goto main

    dobuttondownstuff:
    ' whatever
    goto main


    >I have used the PULSIN and BUTTON commands in the subroutine to
    >try and eliminate the continuously pressed condition without any success.

    BUTTON command should also work for you (although I think most of us
    don't use BUTTON unless we want the autorepeat capability.) Maybe
    there was some error in your use of the syntax of the command.

    BUTTON 0,1,255,0,X,1,dobuttondownstuff ' do it on 01 edge

    Be sure the variable X is a byte and that the delay variable is set
    at 255 (debounce enabled). I'm not sure how you would use pulsin
    here.

    -- regards,
    Tracy Allen
    electronically monitored ecosystems
    mailto:tracy@e...
    http://www.emesystems.com
  • ArchiverArchiver Posts: 46,084
    edited 2001-05-27 19:53
    Hello everyone!

    This is literally my first day noodling with a stamp and so far it is
    a gas.

    I'm replying to this message because I'm trying to do something
    similar in a subroutine that takes the pin # as an argument. The idea
    is I can call it like this:

    Loop:
    ...do stuff...
    ' Check first button
    BtnPin = 15
    GOSUB PollButton
    IF BtnPressed THEN DoSomething
    ' Check next button
    BtnPin = 13
    GOSUB PollButton
    IF BtnPressed THEN DoSomethingElse
    ...do more stuff...
    GOTO Loop
    ...

    I want the subroutine to set BtnPressed to 0 or 1 if the button was
    pressed. If it was pressed, I want it to wait until the button is
    released before returning (the 0-1 thing).

    I figured I wanted to use BUTTON for the debounce capability. Here's
    what I have:

    PollButton
    ' See if a button was pressed, and if so, loop until it is
    ' released so that it is 'one event'. Note that this will
    ' consume as much time as it takes for the user to release
    ' the button, once pressed.
    ' NOTE: This works for 'pull-down' button - 'ON' = 0
    ' IN parameters:
    ' BtnPin = Pin# to poll
    ' OUT parameters:
    ' BtnPressed = 1 if pressed, 0 otherwise
    ' USES Variables:
    ' BtnWork
    BtnPressed = 0
    BtnWork = 0
    ' 'Down' state is 0 (pull-up), debounce but no autorepeat
    BUTTON BtnPin, 0, 255, 0, BtnWork, 1, ButtonPressed
    ' Button not pressed
    RETURN
    ButtonPressed:
    ' Button was pressed - loop until it is released
    BtnPressed = 1
    BtnWork = 0
    StillPressed:
    BUTTON BtnPin, 0, 255, 0, BtnWork, 1, StillPressed
    ' Button released
    RETURN

    But this returns right away and subsequent calls for the same pin#
    see the closed switch as another press.

    What am I doing wrong?

    Thanks!

    -Brian Smithson
  • ArchiverArchiver Posts: 46,084
    edited 2001-05-27 20:55
    Never mind!

    I figured it out. Here's what I ended up with:

    CheckButton:
    ' See if a button was pressed, and if so, loop until it is
    ' released so that it is 'one event'. Note that this will
    ' consume as much time as it takes for the user to release
    ' the button, once pressed.
    ' NOTE: This works for 'pull-down' button - 'ON' = 0
    ' Because that sort of circuit is recommended in Edwards'
    book.
    ' IN parameters:
    ' BtnPin = Pin# to poll
    ' OUT parameters:
    ' BtnPressed = 1 if pressed, 0 otherwise
    ' USES Variables:
    ' BtnWork

    BtnWork = 0
    ' 'Down' state is 0 (pull-up), debounce but no autorepeat
    BUTTON BtnPin, 0, 255, 0, BtnWork, 1, CheckButton_Pressed
    ' Button not pressed
    BtnPressed = 0
    RETURN

    CheckButton_Pressed:
    ' Button WAS pressed - loop until it is released
    IF INS.LOWBIT(BtnPin) = 0 THEN CheckButton_Pressed

    ' Button has been released
    BtnPressed = 1
    RETURN

    My problem was in trying to use BUTTON a 2nd time. The funky
    INS.LOWBIT(#) syntax lets me get at a port with a variable. Cool!

    -Brian

    --- In basicstamps@y..., bsmithson@a... wrote:
    > Hello everyone!
    >
    > This is literally my first day noodling with a stamp and so far it
    is
    > a gas.
    >
    > I'm replying to this message because I'm trying to do something
    > similar in a subroutine that takes the pin # as an argument. The
    idea
    > is I can call it like this:
    >
    > Loop:
    > ...do stuff...
    > ' Check first button
    > BtnPin = 15
    > GOSUB PollButton
    > IF BtnPressed THEN DoSomething
    > ' Check next button
    > BtnPin = 13
    > GOSUB PollButton
    > IF BtnPressed THEN DoSomethingElse
    > ...do more stuff...
    > GOTO Loop
    > ...
    >
    > I want the subroutine to set BtnPressed to 0 or 1 if the button was
    > pressed. If it was pressed, I want it to wait until the button is
    > released before returning (the 0-1 thing).
    >
    > I figured I wanted to use BUTTON for the debounce capability. Here's
    > what I have:
    >
    > PollButton
    > ' See if a button was pressed, and if so, loop until it is
    > ' released so that it is 'one event'. Note that this will
    > ' consume as much time as it takes for the user to release
    > ' the button, once pressed.
    > ' NOTE: This works for 'pull-down' button - 'ON' = 0
    > ' IN parameters:
    > ' BtnPin = Pin# to poll
    > ' OUT parameters:
    > ' BtnPressed = 1 if pressed, 0 otherwise
    > ' USES Variables:
    > ' BtnWork
    > BtnPressed = 0
    > BtnWork = 0
    > ' 'Down' state is 0 (pull-up), debounce but no autorepeat
    > BUTTON BtnPin, 0, 255, 0, BtnWork, 1, ButtonPressed
    > ' Button not pressed
    > RETURN
    > ButtonPressed:
    > ' Button was pressed - loop until it is released
    > BtnPressed = 1
    > BtnWork = 0
    > StillPressed:
    > BUTTON BtnPin, 0, 255, 0, BtnWork, 1, StillPressed
    > ' Button released
    > RETURN
    >
    > But this returns right away and subsequent calls for the same pin#
    > see the closed switch as another press.
    >
    > What am I doing wrong?
    >
    > Thanks!
    >
    > -Brian Smithson
  • ArchiverArchiver Posts: 46,084
    edited 2001-05-28 14:42
    Hi Gang:

    I finally got the code to work. It took some doing and a combination of two
    suggestions but I finally got it!

    For anyone who is interested, this is what I came up with (keeping in mind
    that there is always more than one way to make things work).

    dirs=%0000000000000001 'sets P0 as an output
    outs=%0000000000000000 'everything at zero to begin
    with

    set1 var bit 'variable declarations
    ready var bit
    n var word

    main:
    line1: if in1=0 then m1 'sets the circuit ready
    line2: if (in1=1 and ready=1) then m2 'starts the program running when the
    button is pressed
    line3: if set1=1 then m3 'starts timing
    line4: if n=20 then m4 'sets timing pulse range
    line5: out0=set1 'sets output high
    goto main

    m1: ready=1
    goto line2
    m2: set1=1
    ready=0
    goto line3
    m3: n=n=1
    pause 1
    goto line4
    m4: n=0 'stops the timing
    once n=setpoint (20 in this example)
    set1=0
    goto line5

    Notes:
    a. n = the amount of time in msec for the output pulse. In one of my
    programs, I was able to get the circuit to work down to n=2 and it would be
    tough to get this kind of precision in a 555 (from a repeatability
    viewpoint, especially with thermal drift and component aging).
    b. I tend to write my code like a PLC program because I do work with both
    components for a living and I usually end up converting PLC code to use in a
    stamp environment for dedicated embedded industrial controls. I make a nice
    living doing that.
    c. Murphy's law of programming: in solving the remaining 4% of the program,
    you usually have to go back and change about 40% of the original program (an
    iterative process).
    d. This program works with gosub commands as well (that was a real skull
    bender at first) if that's your preference.

    Have fun. Cheers and regards,

    Gerry Shand
    gwshand@c...
    res@c...
Sign In or Register to comment.