High Resolution Timer & Speed Calculator


Closed Thread
Results 1 to 5 of 5

Hybrid View

  1. #1
    Join Date
    Jan 2010
    Location
    PHILADELPHIA, PA - USA
    Posts
    34


    Did you find this post helpful? Yes | No

    Default ...Continued

    Here is the rest of the Code:

    Code:
    Main:
    
    IF T1CON.0 <> 0 And I = 0 THEN
      LCDOUT $FE,1
      LCDOUT $FE,$C0,"   MEASURING"
      I = 1
      GOTO LCDEND 
    ENDIF
    
    IF T1CON.0 <> 0 THEN LCDEND
    IF T1CON.0 = 0 AND J = 0 THEN
    
    '********** CONVERT COUNTS, OVERFLOWS to SECONDS **********
    @  MOVE?LP  _OVRFLO, _OVFLS64
    @  MATH_MUL  _OVFLS64, _BITS1664, _OVFLS64M
       COUNTS32 = (COUNTS-10)*2           ' 10 TICS START CORRECTION,  200 nS PER COUNT
    @  MOVE?LP  _COUNTS32, _COUNTS64
    @  MATH_ADD  _OVFLS64M, _COUNTS64, _TSEC64
    
    ' ********** CONVERT SECONDS TO MINUTES **********
    @  MATH_DIV  _TSEC64, _CONVSEC64, _TMIN64
    @  MOVE?PL  _TMIN64, _TMIN
    
    ' ********** GET INTEGER SECONDS **********
       CC32 = 10000000
    @  MOVE?LP  _CC32, _CC64
    @  MATH_MUL  _TMIN64, _CONVSEC64, _AA64
    @  MATH_SUB  _TSEC64, _AA64, _BB64
    @  MATH_DIV  _BB64, _CC64, _TSECINT64
    @  MOVE?PL  _TSECINT64, _TSECINT
    
    ' ********** GET DECIMAL SECONDS **********
    @  MATH_MUL  _TSECINT64, _CC64, _AA64
    @  MATH_SUB  _BB64, _AA64,  _TSECDEC64
    @  MOVE?PL  _TSECDEC64, _TSECDEC
    
    ' ********** CALCULATE SPEED (FPS) **********
    @  MATH_DIV  _DISTFT64M, _TSEC64, _FPS64
    
    ' ********** GET INTEGER FPS **********
       CC32 = 1000000
    @  MOVE?LP  _CC32, _CC64
    @  MATH_DIV  _FPS64, _CC64, _FPSINT64
    @  MOVE?PL  _FPSINT64, _FPSINT
    
    ' ********** GET DECIMAL FPS **********
    @  MATH_MUL  _FPSINT64, _CC64, _BB64
    @  MATH_SUB  _FPS64, _BB64,  _FPSDEC64
    @  MOVE?PL  _FPSDEC64, _FPSDEC
    
    ' ********** CONVERT FPS to MPH **********
       AA32 = 15
    @  MOVE?LP  _AA32, _AA64
       BB32 = 22
    @  MOVE?LP  _BB32, _BB64
    @  MATH_MUL  _AA64, _FPS64, _CC64
    @  MATH_DIV  _CC64, _BB64, _MPH64
    
    ' ********** GET INTEGER MPH **********
       AA32 = 1000000
    @  MOVE?LP  _AA32, _AA64
    @  MATH_DIV  _MPH64, _AA64, _MPHINT64
    @  MOVE?PL  _MPHINT64, _MPHINT
    
    ' ********** GET DECIMAL MPH **********
    @  MATH_MUL  _MPHINT64, _AA64, _BB64
    @  MATH_SUB  _MPH64, _BB64,  _MPHDEC64
    @  MOVE?PL  _MPHDEC64, _MPHDEC
    
    ' ********** DISPLAY RESULTS **********
    LCDOUT $FE,1
    LCDOUT $FE,$80," ",DEC2 TMIN,":",DEC2 TSECINT,".",DEC7 TSECDEC
    LCDOUT $FE,$90, DEC4 FPSINT,".",DEC6 FPSDEC," FPS"
    LCDOUT $FE,$D0, DEC4 MPHINT,".",DEC6 MPHDEC," MPH"
    
    DEBUG "OVRFLO: ",DEC10 OVRFLO,13,10
    DEBUG "COUNTS: ",DEC10 COUNTS,13,10,13,10
    DEBUG "TIME: ",DEC2 TMIN,":", DEC2 TSECINT,".",DEC7 TSECDEC,13,10
    DEBUG "SPEED: ",DEC4 FPSINT,".",DEC6 FPSDEC," FPS",13,10
    DEBUG "SPEED: ",DEC4 MPHINT,".",DEC6 MPHDEC," MPH",13,10,13,10
    
    J = 1
    ENDIF
    GOTO LCDEND
    
    LCDEND: 
      PAUSE 25
        @    INT_ENABLE  CCP1_INT     ; Start new capture
    GOTO Main
    
    '---[CCP1 - interrupt handler]------------------------------------------
    Capture:
      IF CCP1CON = %00000101 THEN      ' If rising edge capture then
         HIGH LED            ' Turn on LED to Indicate Measuring
         TMR1L = PRESET                ' Clear Timer1 counts with Preset (High)
         TMR1H = 0             ' Clear Timer1 counts (LOW)
         T1CON.0 = 1                   ' Turn Timer1 on at rising edge capture
         OVRFLO = 0                   ' Zero Over flow counts
         COUNTS = 0                   ' Zero remainder
         CCP1CON = %00000100           ' Switch to falling edge capture
         PIR1.0 = 0                    ' Clear Timer1 overflow flag before enable
       I = 0 : J = 0
         @    INT_ENABLE  TMR1_INT     ; Enable Timer 1 Interrupts 
         GOTO OVER_CCP                 ' Done, exit
      ENDIF
       
      IF CCP1CON = %00000100 THEN      ' If falling edge capture then
         T1CON.0 = 0                   ' Stop Timer1
         CCP1CON = %00000101           ' Switch back to rising edge capture
         @    INT_DISABLE  TMR1_INT    ; Disable Timer 1 Interrupts
         @    INT_DISABLE  CCP1_INT    ; Disable CCP1 Interrupts
         COUNTS.LowByte = TMR1L       ' Get remaining Timer1 counts on falling edge (Low Byte)
         COUNTS.HighByte = TMR1H      ' Get remaining Timer1 counts on falling edge (High Byte)
      LOW LED             ' Turn off LED 
      ENDIF
    
    OVER_CCP:
    @ INT_RETURN
    
    '---[TMR1 - interrupt handler]---------------------------------------------
    Timer1:
    OVRFLO = OVRFLO + 1
    @ INT_RETURN
    END
    It wouldn't all fit in a single post

    --Bob
    Wozzy-2010

  2. #2
    Join Date
    Jan 2010
    Location
    PHILADELPHIA, PA - USA
    Posts
    34


    Did you find this post helpful? Yes | No

    Default Notes, Limitations and Future Features

    Features:
    1) MAXIMUM TIME: 99 MIN, 99.9999999 SEC ...Really just a display limitation
    2) MINIMUM TIME: 0.0000522 SEC also a display limit at 9999.999998 MPH
    3) Time Resolution: 200 nS = 0.0000002 SEC
    4) Input single PULSE to Pin RC2
    5) Displays Measurement and resets awaiting next pulse.
    6) Data is sent out to 57600 N-8-1 Serial terminal on Pin RC6
    7) Status LED shows when pulse is being timed.

    Comments:
    This was an experiment in precision of the code.
    No oscillator is really this accurate.
    Probably pretty close for relative measurements.
    But Not for absolute measurements

    Currently the speed is based on fixed distance of the optical sensors.
    they are fixed and hard coded at 6.000 inches.
    The circuit I use to derive the pulse is here:
    http://www.picbasic.co.uk/forum/showthread.php?t=12614

    Future Plans:
    1) Add additional speed units (M/S, KMH)
    2) Add user menu to set gate distance and displayed units
    3) Add an oscillator correction factor after comparing it to bench-top timer.
    4) Write settings to Onboard EEPROM
    5) Try a 40 MHz crystal oscillator or other High accuracy clock
    6) Build a thermal oven for crystal oscillator ...

    Enjoy,
    --Bob
    Wozzy-2010

  3. #3
    Join Date
    Jan 2010
    Location
    PHILADELPHIA, PA - USA
    Posts
    34


    Did you find this post helpful? Yes | No

    Default Include Files:

    Please note that there are three include files in the above code which are required for it to operate.

    N-BIT_Math.pbp
    At its core is the Multibyte Arithmetic Assembly Library that was written by Alexander Avtanski.
    The Assembly Library was wrapped for PBP by Darrel Taylor [Version:1.3 Beta (JAN 07,2010)]
    This library is used to assemble the 64-Bit high precision math,
    You can find reference to it in this PB Forum Thread:
    http://www.picbasic.co.uk/forum/showthread.php?t=12433

    DT_INTS-18.bas & ReEnterPBP-18.bas
    DT_INTS-18.bas is Darrel Taylor's Base Interrupt System for PIC18F [Version:3.3 (MAR 24, 2008)]
    ReEnterPBP-18.bas is Darrel Taylor's PBP interrupts for PIC18F [Version:1.4 (MAR 24, 2008)]
    Both can be referenced at this PB Forum Thread:
    http://www.picbasic.co.uk/forum/showthread.php?t=3251
    Or directly from Darrel Taylor's web page here:
    http://darreltaylor.com/DT_INTS-18/home.html
    Please note that there are different versions for (PIC12F, 14F, & 16F) and for PIC18F.

    --Bob
    Wozzy-2010

  4. #4
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    WOZZY-2010, interesting project and well documented. Thank you for sharing it.

    Al.
    All progress began with an idea

Similar Threads

  1. SERIN MIDI out of Synch?
    By jncortes in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 9th June 2009, 20:08
  2. Old and beyond help ?
    By DavidFMarks in forum mel PIC BASIC Pro
    Replies: 46
    Last Post: - 11th December 2008, 15:23
  3. Replies: 14
    Last Post: - 26th September 2007, 05:41
  4. sample code for M25P32
    By Pedro Santos in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 9th January 2007, 02:37
  5. Serin and TMR0
    By capitano in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 17th May 2006, 14:58

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts