Shop OBEX P1 Docs P2 Docs Learn Events
Timing without pausing processing? — Parallax Forums

Timing without pausing processing?

RogerNRogerN Posts: 19
edited 2009-12-26 05:50 in BASIC Stamp
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

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-12-20 18:01
    No

    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.
  • RogerNRogerN Posts: 19
    edited 2009-12-20 21:26
    My programming experience went from C-64 to PC to PIC's and BS1, then onto PLC's. It was difficult to transition to PLC programming because of mostly bad examples I learned from with microprocessors and microcontrollers. I wanted to wait on condition and then process, with a PLC you check condition and process, no holding up the process to do the job. Holding up the process works if your processor is only doing one thing at a time but most of the time that isn't the case.

    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).
  • Mike GreenMike Green Posts: 23,101
    edited 2009-12-20 22:00
    Two thoughts ...

    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.
  • RogerNRogerN Posts: 19
    edited 2009-12-20 22:59
    I have multiple experiments & playing I want to do with this but what I bought was a Boe Bot and BS2px, along with some sensors. I would like to make a loop that runs at 50 cycles per second because that's just right for the servo updates. What I thought about is very similar to your suggestion, an oscillator and and some counters to divide to a frequency giving me a 50Hz signal (among others).
    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
  • MorteAzureMorteAzure Posts: 8
    edited 2009-12-22 18:31
    I used a 555 timer im one of my projects. The 555 timer was sensitive to voltage changes, and changed frequency in proportion to these changes, because every line of code or an new time variable ultimately changed my rc time constant circuit. I was trying to make a PWM injecter assembly that was sensitive to thermocouple outputs. Everytime the thermocouple gave a different value, my program modified the pulse and then the timing, SO I used the first high pulse off of the 555 timer to indicate go, and then I read the timing to the low pulse, which gave me my reading that was independent of the basic stamp's timing. I guess this was called an interrupt. My only problem was that the circuit was very succeptible to noise and power supply spikes, which made it unusable for heat control. Any Ideas on how to incorporate the 4-20 miliamp signal into the basic stamp?
  • RogerNRogerN Posts: 19
    edited 2009-12-26 05:50
    One way to read 4-20 mA would be to use a 250 Ohm resistor across the input of an A/D converter. The resistor will drop 1V at 4mA and 5V at 20mA, so 4-20mA becomes 1-5V at the A/D converter. I thought of using a 555 timer but am also considering a cheap PIC microcontroller, I saw some 8 pin PIC for less than $1 in Qty 1 at Digi-Key. That with a resonator or crystal should make a pretty good clock source and perhaps I could program other pins to do something useful.

    RogerN
Sign In or Register to comment.