Shop OBEX P1 Docs P2 Docs Learn Events
Timing in a BS2 app — Parallax Forums

Timing in a BS2 app

ArchiverArchiver Posts: 46,084
edited 2003-10-14 20:07 in General Discussion
I'm involved in a project where we know there will be a lot of experimentation
and "on the fly" changes. Except for a few applications with a BS1 I'd consider
myself pretty new to PBASIC.

I have a BOE and a Stamp Modem board. The project involves taking some ADC
readings and watching how often a motor is run. Essentially what happens is I
need to count how many seconds the motor runs. This value is accumulated until
the temperature exceeds a certain value. Once that happens the modem board is
used to call a PC and transfer the data.

I come from a microcontroller background so all the ADC stuff, modem dialing,
etc. seems pretty straight forward. I'm sitting here this evening wondering how
to accumulate the motor run time?

There's an opto device that gives me a logic high when there's 120 VAC on the
input. I essentially need to poll this input to the Stamp and count the number
of seconds it's active. Too many years having timer interrupts and lack of
experience with PBASIC have me puzzled. At this point high resolution timing
isn't critical. If the motor runs for 78 seconds and I report it ran from 75 to
80 that's perfectly acceptable for proof of concept.

Suggestions?

Thanks,

Tim

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2003-10-14 05:32
    >I'm involved in a project where we know there will be a lot of
    >experimentation and "on the fly" changes. Except for a few
    >applications with a BS1 I'd consider myself pretty new to PBASIC.
    >
    >I have a BOE and a Stamp Modem board. The project involves taking
    >some ADC readings and watching how often a motor is run. Essentially
    >what happens is I need to count how many seconds the motor runs.
    >This value is accumulated until the temperature exceeds a certain
    >value. Once that happens the modem board is used to call a PC and
    >transfer the data.
    >
    >I come from a microcontroller background so all the ADC stuff, modem
    >dialing, etc. seems pretty straight forward. I'm sitting here this
    >evening wondering how to accumulate the motor run time?
    >
    >There's an opto device that gives me a logic high when there's 120
    >VAC on the input. I essentially need to poll this input to the Stamp
    >and count the number of seconds it's active. Too many years having
    >timer interrupts and lack of experience with PBASIC have me puzzled.
    >At this point high resolution timing isn't critical. If the motor
    >runs for 78 seconds and I report it ran from 75 to 80 that's
    >perfectly acceptable for proof of concept.
    >Suggestions?
    >Thanks,
    >Tim

    For the proof of concept (until springing for a real time clock
    chip), you can do something like the following

    DO
    DO
    ' read adc
    timer = timer + motorState ' <-- by calc to keep loop time
    state independent
    IF temperature>threshold THEN EXIT
    PAUSE 990 <--- adjust this value empirically to get
    good approximation
    LOOP
    ' stuff to do with modem etc. to relay timer seconds
    LOOP
  • ArchiverArchiver Posts: 46,084
    edited 2003-10-14 14:32
    Back in the "dark days" [noparse][[/noparse]being facetious] before everyone become so
    enamored with interrupts, programmers used to trace their code paths,
    counting cycles and could do accurate timing and measuring. I used to
    work in the irrigation industry building sprinkler timers, and this
    process is still used on small (four-bit processor!) models.

    Can it be done in the BASIC Stamp? Yes, in a way -- I did it for a
    product that I developed for my previous employer. Of course, you can't
    count cycles. What I did is use a spare pin as an indicator and a scope
    to measure the paths through my code. I was able to the 0.1 sec
    accuracy on a four-channel alarm device. In the micro world, 100
    milliseconds is forever, but in the *real* world it far exceeded the
    requirements of that application.

    PBASIC is designed well for state-machine applications. The framework
    for most of my apps looks like this:

    Main:
    DO
    GOSUB Real_Important_Task
    ON state GOSUB Task1, Task2, Task3
    LOOP

    In my alarm application I padded the routines such that any state would
    cause the "Main" loop to run at 100 milliseconds. My "Real Important
    Task" was monitoring inputs -- I simply added the value of the input,
    then multiplied, like this...

    counter1 = (counter1 + C1in) * C1in

    If the input was one the counter was incremented; if zero the counter
    was cleared. The next step in my system checked counts and directed the
    proper task (by setting the variable state).

    I hope that some of this helps you with your application.

    -- Jon Williams
    -- Applications Engineer, Parallax
    -- Dallas Office



    Original Message
    From: Tim McDonough [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=BKvnc1sxAx9X3gCIB8QcpOz3Fmo0q2jOEmZfBRXBxok6afkYSRtwzP1l-4hAazcFtLRh1SD9UiqNyg]tim@m...[/url
    Sent: Monday, October 13, 2003 6:59 PM
    To: basicstamps@yahoogroups.com
    Subject: [noparse][[/noparse]basicstamps] Timing in a BS2 app


    I'm involved in a project where we know there will be a lot of
    experimentation and "on the fly" changes. Except for a few applications
    with a BS1 I'd consider myself pretty new to PBASIC.

    I have a BOE and a Stamp Modem board. The project involves taking some
    ADC readings and watching how often a motor is run. Essentially what
    happens is I need to count how many seconds the motor runs. This value
    is accumulated until the temperature exceeds a certain value. Once that
    happens the modem board is used to call a PC and transfer the data.

    I come from a microcontroller background so all the ADC stuff, modem
    dialing, etc. seems pretty straight forward. I'm sitting here this
    evening wondering how to accumulate the motor run time?

    There's an opto device that gives me a logic high when there's 120 VAC
    on the input. I essentially need to poll this input to the Stamp and
    count the number of seconds it's active. Too many years having timer
    interrupts and lack of experience with PBASIC have me puzzled. At this
    point high resolution timing isn't critical. If the motor runs for 78
    seconds and I report it ran from 75 to 80 that's perfectly acceptable
    for proof of concept.

    Suggestions?

    Thanks,

    Tim



    To UNSUBSCRIBE, just send mail to:
    basicstamps-unsubscribe@yahoogroups.com
    from the same email address that you subscribed. Text in the Subject
    and Body of the message will be ignored.


    Your use of Yahoo! Groups is subject to
    http://docs.yahoo.com/info/terms/




    This message has been scanned by WebShield. Please report SPAM to
    abuse@p....
  • ArchiverArchiver Posts: 46,084
    edited 2003-10-14 14:41
    Just a follow-up:

    When using the input update or clear thing that I describe below, it's
    probably faster to do it with the & operator:

    counter1 = (counter1 + C1in) & C1in

    -- Jon Williams
    -- Parallax


    Original Message
    From: Jon Williams
    Sent: Tuesday, October 14, 2003 8:33 AM
    To: basicstamps@yahoogroups.com
    Subject: RE: [noparse][[/noparse]basicstamps] Timing in a BS2 app


    Back in the "dark days" [noparse][[/noparse]being facetious] before everyone become so
    enamored with interrupts, programmers used to trace their code paths,
    counting cycles and could do accurate timing and measuring. I used to
    work in the irrigation industry building sprinkler timers, and this
    process is still used on small (four-bit processor!) models.

    Can it be done in the BASIC Stamp? Yes, in a way -- I did it for a
    product that I developed for my previous employer. Of course, you can't
    count cycles. What I did is use a spare pin as an indicator and a scope
    to measure the paths through my code. I was able to the 0.1 sec
    accuracy on a four-channel alarm device. In the micro world, 100
    milliseconds is forever, but in the *real* world it far exceeded the
    requirements of that application.

    PBASIC is designed well for state-machine applications. The framework
    for most of my apps looks like this:

    Main:
    DO
    GOSUB Real_Important_Task
    ON state GOSUB Task1, Task2, Task3
    LOOP

    In my alarm application I padded the routines such that any state would
    cause the "Main" loop to run at 100 milliseconds. My "Real Important
    Task" was monitoring inputs -- I simply added the value of the input,
    then multiplied, like this...

    counter1 = (counter1 + C1in) * C1in

    If the input was one the counter was incremented; if zero the counter
    was cleared. The next step in my system checked counts and directed the
    proper task (by setting the variable state).

    I hope that some of this helps you with your application.

    -- Jon Williams
    -- Applications Engineer, Parallax
    -- Dallas Office



    Original Message
    From: Tim McDonough [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=nbW9R0u5r31BzozglNhVRrmHAeXfBTaX2FNnly8WAnJA-tImqKJeBh0Rhlh4lr6--YVefM8hT_s]tim@m...[/url
    Sent: Monday, October 13, 2003 6:59 PM
    To: basicstamps@yahoogroups.com
    Subject: [noparse][[/noparse]basicstamps] Timing in a BS2 app


    I'm involved in a project where we know there will be a lot of
    experimentation and "on the fly" changes. Except for a few applications
    with a BS1 I'd consider myself pretty new to PBASIC.

    I have a BOE and a Stamp Modem board. The project involves taking some
    ADC readings and watching how often a motor is run. Essentially what
    happens is I need to count how many seconds the motor runs. This value
    is accumulated until the temperature exceeds a certain value. Once that
    happens the modem board is used to call a PC and transfer the data.

    I come from a microcontroller background so all the ADC stuff, modem
    dialing, etc. seems pretty straight forward. I'm sitting here this
    evening wondering how to accumulate the motor run time?

    There's an opto device that gives me a logic high when there's 120 VAC
    on the input. I essentially need to poll this input to the Stamp and
    count the number of seconds it's active. Too many years having timer
    interrupts and lack of experience with PBASIC have me puzzled. At this
    point high resolution timing isn't critical. If the motor runs for 78
    seconds and I report it ran from 75 to 80 that's perfectly acceptable
    for proof of concept.

    Suggestions?

    Thanks,

    Tim



    To UNSUBSCRIBE, just send mail to:
    basicstamps-unsubscribe@yahoogroups.com
    from the same email address that you subscribed. Text in the Subject
    and Body of the message will be ignored.


    Your use of Yahoo! Groups is subject to
    http://docs.yahoo.com/info/terms/




    This message has been scanned by WebShield. Please report SPAM to
    abuse@p....


    To UNSUBSCRIBE, just send mail to:
    basicstamps-unsubscribe@yahoogroups.com
    from the same email address that you subscribed. Text in the Subject
    and Body of the message will be ignored.


    Your use of Yahoo! Groups is subject to
    http://docs.yahoo.com/info/terms/




    This message has been scanned by WebShield. Please report SPAM to
    abuse@p....
  • ArchiverArchiver Posts: 46,084
    edited 2003-10-14 18:05
    On Tue, 14 Oct 2003 06:32:51 -0700, Jon Williams wrote:
    >·Back in the "dark days" [noparse][[/noparse]being facetious] before everyone become so
    >·enamored with interrupts, programmers used to trace their code
    >·paths, counting cycles and could do accurate timing and measuring.
    >·I used to work in the irrigation industry building sprinkler
    >·timers, and this process is still used on small (four-bit processor!
    >·) models.

    I'll date myself here, the cobwebs are clearing away and I'm remembering a lot
    of those techniques from jobs past.

    >·In my alarm application I padded the routines such that any state
    >·would cause the "Main" loop to run at 100 milliseconds. ·My "Real
    >·Important Task" was monitoring inputs -- I simply added the value
    >·of the input, then multiplied, like this...
    >
    >·counter1 = (counter1 + C1in) * C1in
    >
    >
    >·If the input was one the counter was incremented; if zero the
    >·counter was cleared. ·The next step in my system checked counts and
    >·directed the proper task (by setting the variable state).
    >
    >·I hope that some of this helps you with your application.

    Several good suggestions from list members. At this stage I like your technique
    best as I won't need an external counter or clock for what I need to accomplish.

    Thank you Jon!

    Tim
  • ArchiverArchiver Posts: 46,084
    edited 2003-10-14 18:58
    Hi Jon,

    just a minor correction to the follow up, which would be,

    counter1 = (counter1 + C1in) & -C1in

    C1in is a bit and counter1 is probably a word or a byte. -1=$ffff is
    the mask to maintain the counter and 0 will reset it. (Note: unary
    "-" takes precedence over binary "&" in PBASIC)

    While in machine language execution time, negation-followed-by-AND
    would be far faster than full multiplication, on the Stamp BS2, each
    math operator takes about 140 microseconds of interpreter
    fetch+execute time, so the two operator sequence &- might be slower
    than the single *.

    -- Tracy


    >Just a follow-up:
    >
    >When using the input update or clear thing that I describe below, it's
    >probably faster to do it with the & operator:
    >
    > counter1 = (counter1 + C1in) & C1in
    >
    >-- Jon Williams
    >-- Parallax
    >
    >
    >
    Original Message
    >From: Jon Williams
    >Sent: Tuesday, October 14, 2003 8:33 AM
    >To: basicstamps@yahoogroups.com
    >Subject: RE: [noparse][[/noparse]basicstamps] Timing in a BS2 app
    >
    >
    >Back in the "dark days" [noparse][[/noparse]being facetious] before everyone become so
    >enamored with interrupts, programmers used to trace their code paths,
    >counting cycles and could do accurate timing and measuring. I used to
    >work in the irrigation industry building sprinkler timers, and this
    >process is still used on small (four-bit processor!) models.
    >
    >Can it be done in the BASIC Stamp? Yes, in a way -- I did it for a
    >product that I developed for my previous employer. Of course, you can't
    >count cycles. What I did is use a spare pin as an indicator and a scope
    >to measure the paths through my code. I was able to the 0.1 sec
    >accuracy on a four-channel alarm device. In the micro world, 100
    >milliseconds is forever, but in the *real* world it far exceeded the
    >requirements of that application.
    >
    >PBASIC is designed well for state-machine applications. The framework
    >for most of my apps looks like this:
    >
    >Main:
    > DO
    > GOSUB Real_Important_Task
    > ON state GOSUB Task1, Task2, Task3
    > LOOP
    >
    >In my alarm application I padded the routines such that any state would
    >cause the "Main" loop to run at 100 milliseconds. My "Real Important
    >Task" was monitoring inputs -- I simply added the value of the input,
    >then multiplied, like this...
    >
    > counter1 = (counter1 + C1in) * C1in
    >
    >If the input was one the counter was incremented; if zero the counter
    >was cleared. The next step in my system checked counts and directed the
    >proper task (by setting the variable state).
    >
    >I hope that some of this helps you with your application.
    >
    >-- Jon Williams
    >-- Applications Engineer, Parallax
    >-- Dallas Office
    >
    >
    >
    >
    Original Message
    >From: Tim McDonough [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=PAu-XoP3H5pyl06eNJ9UBIuQ_3B2gAYCmevM1c1OScuZMSuezsFUNPm-1z5szMtlVhhbxM8czA]tim@m...[/url
    >Sent: Monday, October 13, 2003 6:59 PM
    >To: basicstamps@yahoogroups.com
    >Subject: [noparse][[/noparse]basicstamps] Timing in a BS2 app
    >
    >
    >I'm involved in a project where we know there will be a lot of
    >experimentation and "on the fly" changes. Except for a few applications
    >with a BS1 I'd consider myself pretty new to PBASIC.
    >
    >I have a BOE and a Stamp Modem board. The project involves taking some
    >ADC readings and watching how often a motor is run. Essentially what
    >happens is I need to count how many seconds the motor runs. This value
    >is accumulated until the temperature exceeds a certain value. Once that
    >happens the modem board is used to call a PC and transfer the data.
    >
    >I come from a microcontroller background so all the ADC stuff, modem
    >dialing, etc. seems pretty straight forward. I'm sitting here this
    >evening wondering how to accumulate the motor run time?
    >
    >There's an opto device that gives me a logic high when there's 120 VAC
    >on the input. I essentially need to poll this input to the Stamp and
    >count the number of seconds it's active. Too many years having timer
    >interrupts and lack of experience with PBASIC have me puzzled. At this
    >point high resolution timing isn't critical. If the motor runs for 78
    >seconds and I report it ran from 75 to 80 that's perfectly acceptable
    >for proof of concept.
    >
    >Suggestions?
    >
    >Thanks,
    >
    >Tim
    >
    >
    >
    >To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    >from the same email address that you subscribed. Text in the Subject
    >and Body of the message will be ignored.
    >
    >
    >Your use of Yahoo! Groups is subject to
    >http://docs.yahoo.com/info/terms/
    >
    >
    >
    >
    >This message has been scanned by WebShield. Please report SPAM to
    >abuse@p....
    >
    >
    >To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    >from the same email address that you subscribed. Text in the Subject
    >and Body of the message will be ignored.
    >
    >
    >Your use of Yahoo! Groups is subject to
    >http://docs.yahoo.com/info/terms/
    >
    >
    >
    >
    >This message has been scanned by WebShield. Please report SPAM to
    >abuse@p....
    >
    >
    >To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    >from the same email address that you subscribed. Text in the
    >Subject and Body of the message will be ignored.
    >
    >
    >Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
  • ArchiverArchiver Posts: 46,084
    edited 2003-10-14 20:07
    Thanks for the catch, Tracy. I should have left well-enough alone....

    -- Jon

    Original Message
    From: Tracy Allen [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=6pX5yPNut1sDdXXDjPMJfsKKMwZEnuacrmPHB3GvbwC62_RdStLHpEuoAUBCI3VpRghRDR9HcKqt2w]tracy@e...[/url
    Sent: Tuesday, October 14, 2003 12:58 PM
    To: basicstamps@yahoogroups.com
    Subject: RE: [noparse][[/noparse]basicstamps] Timing in a BS2 app


    Hi Jon,

    just a minor correction to the follow up, which would be,

    counter1 = (counter1 + C1in) & -C1in

    C1in is a bit and counter1 is probably a word or a byte. -1=$ffff is
    the mask to maintain the counter and 0 will reset it. (Note: unary
    "-" takes precedence over binary "&" in PBASIC)

    While in machine language execution time, negation-followed-by-AND
    would be far faster than full multiplication, on the Stamp BS2, each
    math operator takes about 140 microseconds of interpreter
    fetch+execute time, so the two operator sequence &- might be slower
    than the single *.

    -- Tracy


    >Just a follow-up:
    >
    >When using the input update or clear thing that I describe below, it's
    >probably faster to do it with the & operator:
    >
    > counter1 = (counter1 + C1in) & C1in
    >
    >-- Jon Williams
    >-- Parallax
    >
    >
    >
    Original Message
    >From: Jon Williams
    >Sent: Tuesday, October 14, 2003 8:33 AM
    >To: basicstamps@yahoogroups.com
    >Subject: RE: [noparse][[/noparse]basicstamps] Timing in a BS2 app
    >
    >
    >Back in the "dark days" [noparse][[/noparse]being facetious] before everyone become so
    >enamored with interrupts, programmers used to trace their code paths,
    >counting cycles and could do accurate timing and measuring. I used to
    >work in the irrigation industry building sprinkler timers, and this
    >process is still used on small (four-bit processor!) models.
    >
    >Can it be done in the BASIC Stamp? Yes, in a way -- I did it for a
    >product that I developed for my previous employer. Of course, you
    can't
    >count cycles. What I did is use a spare pin as an indicator and a
    scope
    >to measure the paths through my code. I was able to the 0.1 sec
    >accuracy on a four-channel alarm device. In the micro world, 100
    >milliseconds is forever, but in the *real* world it far exceeded the
    >requirements of that application.
    >
    >PBASIC is designed well for state-machine applications. The framework
    >for most of my apps looks like this:
    >
    >Main:
    > DO
    > GOSUB Real_Important_Task
    > ON state GOSUB Task1, Task2, Task3
    > LOOP
    >
    >In my alarm application I padded the routines such that any state would
    >cause the "Main" loop to run at 100 milliseconds. My "Real Important
    >Task" was monitoring inputs -- I simply added the value of the input,
    >then multiplied, like this...
    >
    > counter1 = (counter1 + C1in) * C1in
    >
    >If the input was one the counter was incremented; if zero the counter
    >was cleared. The next step in my system checked counts and directed
    the
    >proper task (by setting the variable state).
    >
    >I hope that some of this helps you with your application.
    >
    >-- Jon Williams
    >-- Applications Engineer, Parallax
    >-- Dallas Office
    >
    >
    >
    >
    Original Message
    >From: Tim McDonough [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=Ut4RYH9wFLUFLaw-10WaUp3GcndK3hEqOJwK74yzS4hXAOnSALIWdUg9Up4zbwTuR-a9sLwr18xMvw]tim@m...[/url
    >Sent: Monday, October 13, 2003 6:59 PM
    >To: basicstamps@yahoogroups.com
    >Subject: [noparse][[/noparse]basicstamps] Timing in a BS2 app
    >
    >
    >I'm involved in a project where we know there will be a lot of
    >experimentation and "on the fly" changes. Except for a few applications
    >with a BS1 I'd consider myself pretty new to PBASIC.
    >
    >I have a BOE and a Stamp Modem board. The project involves taking some
    >ADC readings and watching how often a motor is run. Essentially what
    >happens is I need to count how many seconds the motor runs. This value
    >is accumulated until the temperature exceeds a certain value. Once that
    >happens the modem board is used to call a PC and transfer the data.
    >
    >I come from a microcontroller background so all the ADC stuff, modem
    >dialing, etc. seems pretty straight forward. I'm sitting here this
    >evening wondering how to accumulate the motor run time?
    >
    >There's an opto device that gives me a logic high when there's 120 VAC
    >on the input. I essentially need to poll this input to the Stamp and
    >count the number of seconds it's active. Too many years having timer
    >interrupts and lack of experience with PBASIC have me puzzled. At this
    >point high resolution timing isn't critical. If the motor runs for 78
    >seconds and I report it ran from 75 to 80 that's perfectly acceptable
    >for proof of concept.
    >
    >Suggestions?
    >
    >Thanks,
    >
    >Tim
    >
    >
    >
    >To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    >from the same email address that you subscribed. Text in the Subject
    >and Body of the message will be ignored.
    >
    >
    >Your use of Yahoo! Groups is subject to
    >http://docs.yahoo.com/info/terms/
    >
    >
    >
    >
    >This message has been scanned by WebShield. Please report SPAM to
    >abuse@p....
    >
    >
    >To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    >from the same email address that you subscribed. Text in the Subject
    >and Body of the message will be ignored.
    >
    >
    >Your use of Yahoo! Groups is subject to
    >http://docs.yahoo.com/info/terms/
    >
    >
    >
    >
    >This message has been scanned by WebShield. Please report SPAM to
    >abuse@p....
    >
    >
    >To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    >from the same email address that you subscribed. Text in the
    >Subject and Body of the message will be ignored.
    >
    >
    >Your use of Yahoo! Groups is subject to
    http://docs.yahoo.com/info/terms/


    To UNSUBSCRIBE, just send mail to:
    basicstamps-unsubscribe@yahoogroups.com
    from the same email address that you subscribed. Text in the Subject
    and Body of the message will be ignored.


    Your use of Yahoo! Groups is subject to
    http://docs.yahoo.com/info/terms/




    This message has been scanned by WebShield. Please report SPAM to
    abuse@p....
Sign In or Register to comment.