Shop OBEX P1 Docs P2 Docs Learn Events
Time Delta Measurement — Parallax Forums

Time Delta Measurement

PatrickPatrick Posts: 7
edited 2005-02-16 23:50 in BASIC Stamp
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

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-02-15 22:29
    I believe there is a command called PULSIN which measures pulse width, page 93 and 94 of "Understanding Signals" uses it (http://www.parallax.com/dl/docs/prod/sic/Signals.pdf·)
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-02-16 02:15
    To use PULSIN you'll need two readings: one for the pulse (assuming it's high), the other for the period in between. Add these together to get the period, then take the reciprocol to get the frequency.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • gtchiefgtchief Posts: 7
    edited 2005-02-16 14:24
    This is along the lines of something I am trying to do. I want to record a time each time a pin goes low(it will return high immediately after). Any ideas?
  • PatrickPatrick Posts: 7
    edited 2005-02-16 14:57
    The PULSIN command sounds great, but for my BS2SX, the max period it will wait is 52 ms.· That means that any RPM reading under 1154 RPM will not get read.· I am trying to think of a way around that, any ideas?


    Thanks
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-02-16 15:19
    Is there any way to increase the number of pulses per revolution? If not, you may need to use an external timing device -- but then that adds overhead and complexity to the project.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-02-16 16:59
    maybe an SXer can program an SX18 DIP ($1.79) or SX28 DIP ($4.80) for you to perform the function, using the internal oscillator of the SX and a single pullup resistor on the /MCLR pin, an SX can measure cycles that will capture any motor's RPM (capapable of capturing >100kHz), the only issue is the internal oscillator is +/- 8% tolerance, but for a $1.50, an external resonator will make the measurements very accurate. I am too busy to help you out right now, but perhaps a kind soul would be willing to do it for you (especially if they have already written code for such a function) for relatively cheap. The interface with the stamp could be I2C, SPI or·RS232 depending on your preference.

    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
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2005-02-16 17:23
    Steve Parkis did write a nice program for the SX that serves as a multifunction timer (one microsecond resolution to 71 hours):
    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
  • PatrickPatrick Posts: 7
    edited 2005-02-16 20:10
    This is lifted from one of the above links.· It is kind of a poor man's timer without any external hardware.· It sounds like a good idea, but how do you find out many iterations your stamp will do per second?· Use the COUNT function?··timer loop

    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.
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2005-02-16 23:50
    Hi Patrick,

    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:


    xlow VAR Word
    xhigh VAR Word
    Clow CON 65535           ' need to find right conversion value here
    CHIGH CON 65535         ' and here
    
    DO
      xlow=0 : xhigh=0
      DO : LOOP UNTIL in0=1  ' wait until high
      DO : LOOP UNTIL in0=0   ' wait until low
      DO : xlow=xlow+1 : LOOP UNTIL in0=1   ' count low time
      DO : xhigh=xhigh+1 : LOOP UNTIL in0=0   ' count high time
      DEBUG CR, DEC xlow, TAB, DEC xhigh   ' show results
      DEBUG CR, DEC xlow ** Clow, tab, DEC xhigh ** Chigh  ' show computed times
    LOOP
    



    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
Sign In or Register to comment.