Timing without pausing processing?
RogerN
Posts: 19
In most programs I want to write, I have tasks I want to do every pass through the loop and some tasks I only want after a preset period of time. If I were to make a timer I would do something like add my time increment to the current time and execute when the current time >= stored time + time increment.
In a PIC they have an internal timer that you can use but I don't know what to do in a BS2(PX). One idea is to have an external timer chip that you read every pass through the loop, but I'm hoping to not need an external timer.
The timers in the examples I've seen usually hold up the processor for the required time and then execute the timer done code. That's fine if you are only performing 1 task, but I need my loop to run continuous and other code to execute when it's time to, not pause my program while timing. Any solutions for this? Can the BS2PX run a program in slot 2 to increment a timer and then go back to the loop in program slot 1?
Thanks!
RogerN
In a PIC they have an internal timer that you can use but I don't know what to do in a BS2(PX). One idea is to have an external timer chip that you read every pass through the loop, but I'm hoping to not need an external timer.
The timers in the examples I've seen usually hold up the processor for the required time and then execute the timer done code. That's fine if you are only performing 1 task, but I need my loop to run continuous and other code to execute when it's time to, not pause my program while timing. Any solutions for this? Can the BS2PX run a program in slot 2 to increment a timer and then go back to the loop in program slot 1?
Thanks!
RogerN
Comments
All of the Stamps are what's called "single threaded". They can only do one thing at a time. Although the Stamps are based on the PIC or the SX microprocessors, the internal timer is used internally by the PBasic interpreter for timing various statements and is not available for any user timing. You would have to use some kind of external timer to do what you want. For simple, imprecise timing, the Stamps can approximate the time by using PAUSE statements and by accounting for the execution time of individual statements in a small loop. If your timing is on the order of seconds or tenths of a second, this technique works well. For more precise timing, you'd need to use an external timer or switch to something like the Propeller which can do very accurate timing easily.
A blinking light example of what I'm typing about: (Not in a real programming language or syntax) Start: Turn LED on, wait 1/2 second, turn LED OFF, wait 1/2 second, Goto Start. That would give 1 blink per second with 1/2 second on/off time. Additional code could only execute once per second.
Another way of doing the same thing without making the processor wait: Start: Timer 1 timing 1/2 Second, IF Timer 1 Not Done then led ON else led OFF, IF Timer 1 Done then Timer 2 timing 1/2 Second, IF Timer 2 Done then Reset Timer 1, Goto Start. This way additional code can be added before Goto Start and would be executed at the rate of the loop speed. The timers when enabled would add 1/2 second to the current time (once when first enabled) and set a Done bit when the current time >= the timers set time.
Maybe what I would need is something like a serial RTC that times in perhaps milliseconds (or at least better than 1 second increments).
1) The Stamps are relatively slow. A BS2 has execution times (for simple interpretive codes) on the order of 150-200us. More complex statements take on the order of 1ms. A BS2px is quite a bit faster, but still relatively slow as microcontrollers go.
2) You could provide a fast RTC using a standard 32KHz crystal and a CD4040 12 bit counter. You'd get pulses at 125ms, 62.5ms, and other submultiples of 2 to 1/32768 second.
If my final program loop can execute in less than 20 milliseconds I can wait for my 50 Hz clock to go low before "goto loop_start". Then a loop counter would give me 50 counts per second, that I use for timing. I'm thinking I will need to send a servo a certain number of pulses to move or turn at a certain speed for a specified length of time.
Thanks for the information, I was hoping for some timer register I could read but I can work around it with an oscillator and counter.
RogerN
RogerN