Time Delta Measurement
Patrick
Posts: 7
What would be the best way to measure the time (in milliseconds or whatever) between pulses (high-low to high-low)?· I have a tachometer program that currently uses the COUNT function to calculate the RPM, but I would rather just measure the time between readings to calculate the RPM.
Thanks
Thanks
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas, TX· USA
Thanks
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas, TX· USA
My next slated project is interfacing an SX with the ATtiny13 which is a 8 pin dip microcontroller for <$2, the SX will program the AT13 via SPI then use it as a versatile expansion device communicating with the host via I2C and will be capable of doing 4 channels of PWM, 10-bit ADC, and pulse counting (both width and raw count), plus in-chip EEPROM storage. Each of the four channel's functions can be set via the I2C interface. While I will be creating the code to program the AT13 on an SX, it can also be programed via a stamp (I may attempt to write a stamp program to do it as well using SX/B, but I will be unable to test the BASIC code on a stamp itself). The only issue is, I do not expect to have the project completed until mid April, but I will post everything to the Projects forum when I'm done. If this project is not time critical you could wait until then.
Paul
Post Edited (Paul Baker) : 2/16/2005 5:03:39 PM GMT
home.earthlink.net/~parkiss/oldstuff/tm1summ.txt documentation
home.earthlink.net/~parkiss/oldstuff/tm1sxcod.src source code
Additional links to other timer chips from PHAnderson and Al Williams here,
www.emesystems.com/BS2speed.htm#longpulse
along with suggestions for timing events with native stamp routines.
-- Tracy
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
But you can time longer loops to about 1 millisecond of resolution without having to hook up any external chips. Here are two possible tight timing loops. The first keeps track of the time a pin stays in a low state, and the second measures the time a pin stays high.
' snippet measures the time P0 stays low. xc var word loop1: ' 1418 loops per second, 7.05E-4 seconds per loop xc=xc+1 branch in0,[noparse][[/noparse]loop1] ' count until in0 goes high
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
' snippet measures the time P0 stays high. xc var word loop2: ' 1426 loops per second, 7.013E-4 seconds per loop xc=xc+1 if in0 then loop2 ' count until in0 goes low debug dec xc**45960,cr ' ** converts the count to straight milliseconds
The conversion factor in the second program converts the arbitrary units of time, ticked off at 1426 per second, into milliseconds for display. The factor for use with the ** operator comes from 1000/1426 ~= 45690/65536. This logic is descibed elsewhere.
Be aware that the result depends slightly on the individual Stamp and on the temperature (at extremes), due to variation in the vibration frequency of the ceramic resonator that runs the Stamp. Moreover, the result also depends on details of any larger program that the above loops might be a part of, details you might not expect to have anything to do with it. The number of subroutine calls in the program and the exact variables used have a significant effect. In another test at another time using what I think was the same code, and using p1 instead of p12, I got 1516 interations per second instead of 1426. The message is that it is necessary to calibrate the loop in the final version of the program.
You need an external signal generator to provide a calibration signal. It can be a regular signal generator, or a clock chip with a signal output, or something derived (safely) from the 60 or 50 hz AC power line. Or, if you have the luxury of a second BASIC Stamp, you can use that as your signal generator,
' PBASIC 0.5hz signal generator
' take output from this 2nd stamp pin p0...
DO
HIGH 0:PAUSE 1000
LOW 0 : PAUSE 1000
LOOP
In any case, feed your test signal into the pin of the first stamp, running the following variation on the timer code:
The test program measures every other cycle, so that the time required to print out the DEBUG data does not figure in the result. That will print out the two timing parameters (counts per second). You just have to multiply them (using **) by a constantto rational units of milliseconds.
Suppose you get xlow=1234 and xhigh=1324. Then the constant factors you need to convert counts to milliseconds are, (from your calculator) 1000*65536/1234=53109, and 1000*65536/1324=49498
Clow CON 53109
Chigh CON 49498
Substitute those constants in the program, and the times should now come out at 1000 milliseconds high and 1000 milliseconds low. And you can now use the program as a tachometer with ~ 1 millisecond resolution. If you use a different calibration source, not 1000 milliseconds low and 1000 high, you will have to adjust the equations accordingly, but same idea.
I wrote snippet you quoted in the days before PBASIC 2.5 syntax. The loop using DO:LOOP syntax will run very slightly slower, but it is easier on the eyes.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com