Shop OBEX P1 Docs P2 Docs Learn Events
loop a specific time or break at pin high — Parallax Forums

loop a specific time or break at pin high

jeppexjeppex Posts: 2
edited 2010-12-30 10:15 in BASIC Stamp
Hi all,

Im new and have just build the Basic Stamp II OEM - The question is how to do a timed loop ie 30 sec which can be interruptet by a pin go high

Can I only do this using loop/pause or does Pbasic offer something that I can't find?

do loop for 30 sec
if pinx high then goto do_something_else
loop

Thanks in advance :smile:

Comments

  • stamptrolstamptrol Posts: 1,731
    edited 2010-12-28 04:20
    Welcome to the Forums.

    The short answer is that, yes, you can do exactly what you want. And, you can do it with any microprocessor.

    Overall it would make sense for you to do a bit of exploring and experimenting with the programming rather than looking for a packaged solution.

    Here's what you will end up with:

    The main loop will operate, calling a timing loop ( subroutine) each time it goes through.

    A subroutine will contain a short delay (say PAUSE 100). Therefore each time through the subroutine will take about 100 mSec. It then RETURNs to tthe main loop.

    Back in the main loop, you keep track of how many times you have called the subroutine. When you have called it about 300 times, you have timed about 30 seconds. You can also watch a pin state here.

    Cheers,
  • jeppexjeppex Posts: 2
    edited 2010-12-29 17:46
    Thank you Tom - I get your point and agree that a loop/timer routine looks like a fair solution. Thanks.

    On a note; How does PAUSE calculate the time? It could be looping also, but doesent a computer rely on timing from a xtal and if so would that pose an option to get some sort of time from the Basic Stamp itself?

    Thanks for all :)
    Jeppe
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2010-12-29 18:04
    On a note; How does PAUSE calculate the time? It could be looping also, but doesent a computer rely on timing from a xtal and if so would that pose an option to get some sort of time from the Basic Stamp itself?

    The short answer is no not very good time keeping from just using a Basic Stamp you could do it with just a basic stamp how ever what else you have in your routine will efect how much time the over all routine will take to run from top of your code to the bottom of your code you can adj the pause command
    time to get the right timing that you need a little more work is need on your end

    I hope this helps

    For example

    DO.....................................' The time it take to run from here
    LOW LED
    FOR Laps = 0 to 100
    IF IN0 = 1 THEN EXIT
    NEXT
    Your_loop = Your_loop + 1
    IF Your_loop = 300 THEN EXIT
    HIGH LED
    LOOP.....................................' Until it get to here you would need a stop watch to how long it would take to run from the led going on to going off to know long it takes to run one time and time it to see how it would take to run 300 times


    You could also use a DS1302 time chip as well here is a demo code for it the chip is very easy to use btw




    ' =========================================================================
    '
    ' File...... DS1302_Demo.bs2
    ' Purpose... Demonstrate The DS1302 Clock & RAM Functions
    ' Author.... Chris Savage -- Parallax, Inc.
    ' E-mail.... csavage@parallax.com
    ' Started...
    ' Updated... 10-09-2005
    '
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    '
    ' =========================================================================


    '
    [ Program Description ]

    ' This code demonstrates the use of the DS1302 RTC/RAM Chip with the BASIC
    ' Stamp 2 Microcontroller, making easy use of the 12 Hour & 24 Hour time
    ' modes, as well as accessing the RAM. The sample code will use the DEBUG
    ' window for input and output for simplicity. You can change the output
    ' to the device of your choice. For example, using a Parallax Serial LCD
    ' requires very few changes to the code to use it for output.
    ' A modified version of this code was adapted to display the date and time
    ' on a Parallax Serial LCD Display (#27976), also with backlight (#27977)
    ' and on Parallax Professional Development Board (#28138, coming soon!)
    ' using a MAX7219 8-Digit Display Driver.
    ' This code will work on all BS2 Stamp Modules.
    '
    ' The Parallax 2 X 16 Serial LCD Displays are availble at the following
    ' links:
    ' http://www.parallax.com/detail.asp?product_id=27976
    ' http://www.parallax.com/detail.asp?product_id=27977
    '
    ' Pin Assignments
    ' 0 DS1302 DataIO
    ' 1 DS1302 Clock
    ' 2 DS1302 Chip Select


    '
    [ Revision History ]



    '
    [ I/O Definitions ]

    DataIO PIN 4 ' DS1302.6
    Clock PIN 3 ' DS1302.7
    CS1302 PIN 5 ' DS1302.5


    '
    [ Constants ]

    WrSecs CON $80 ' Write Seconds
    RdSecs CON $81 ' Read Seconds

    WrMins CON $82 ' Write Minutes
    RdMins CON $83 ' Read Minutes

    WrHrs CON $84 ' Write Hours
    RdHrs CON $85 ' Read Hours

    CWPr CON $8E ' Write Protect Register

    WPr1 CON $80 ' Set Write Protect
    WPr0 CON $00 ' Clear Write Protect

    WrBurst CON $BE ' Write Burst Of Data
    RdBurst CON $BF ' Read Burst Of Data

    WrRam CON $C0 ' Write RAM Data
    RdRam CON $C1 ' Read RAM Data


    '
    [ Variables ]
    index VAR Byte ' Loop Counter
    reg VAR Byte ' Read/Write Address
    ioByte VAR Byte ' Data To/From DS1302

    secs VAR Byte ' Seconds
    secs01 VAR secs.LOWNIB
    secs10 VAR secs.HIGHNIB

    mins VAR Byte ' Minutes
    mins01 VAR mins.LOWNIB
    mins10 VAR mins.HIGHNIB

    hrs VAR Byte ' Hours
    hrs01 VAR hrs.LOWNIB
    hrs10 VAR hrs.HIGHNIB

    date VAR Byte
    month VAR Byte
    day VAR Nib ' Day
    year VAR Byte ' Year



    work VAR Byte ' Work Data


    '
    [ Initialization ]

    Init:
    reg = CWPr ' Initialize DS1302
    ioByte = WPr0 ' Clear Write Protect
    GOSUB RTC_Out ' Send Command


    '
    [ Program Code ]

    Start:
    mins = $00
    secs = $00

    GOSUB Set_Time

    DO

    GOSUB Get_Time
    DEBUG HOME, HEX2 mins, ":", HEX2 secs
    IF mins = $03 AND secs = $15 THEN EXIT
    LOOP

    DEBUG CR, CR, "3:15 Elapsed"

    STOP

    '
    [ Subroutines ]


    RTC_Out:
    HIGH CS1302 ' Select DS1302
    SHIFTOUT DataIO, Clock, LSBFIRST, [reg, ioByte]
    LOW CS1302 ' Deselect DS1302
    RETURN

    RTC_In:
    HIGH CS1302 ' Select DS1302
    SHIFTOUT DataIO, Clock, LSBFIRST, [reg]
    SHIFTIN DataIO, Clock, LSBPRE, [ioByte]
    LOW CS1302 ' Deselect DS1302
    RETURN

    Set_Time: ' DS1302 Burst Write
    HIGH CS1302 ' Select DS1302
    SHIFTOUT DataIO, Clock, LSBFIRST, [WrBurst]
    SHIFTOUT DataIO, Clock, LSBFIRST, [secs, mins, hrs,
    date, month, day, year, 0]
    LOW CS1302 ' Deselect DS1302
    RETURN

    Get_Time: ' DS1302 Burst Read
    HIGH CS1302 ' Select DS1302
    SHIFTOUT DataIO, Clock, LSBFIRST, [RdBurst]
    SHIFTIN DataIO, Clock, LSBPRE, [secs, mins, hrs, date, month, day, year]
    LOW CS1302 ' Deselect DS1302
    RETURN
  • damage31damage31 Posts: 21
    edited 2010-12-30 10:15
    sam_sam_sam, I just wanted to say thanks for the simplified 1302 code snippet...

    I was just thinking about doing exactly what you posted, but I wasn't sure just yet on where to start to begin simplifying the code from the DS1302_Demo.bs2 example to minimize it for my simple needs. You appear to have done that

    thanks much.
Sign In or Register to comment.