; ---------------------------------------------------------------------------- ; ; Title: ; ; PP05 -- 10 MHz "picPET" PIC-based Precision Event Timer (edge,sync,tx/inv) ; ; Function: ; ; This program implements a precise time-stamping counter in a little old ; 8-pin PIC microcontroller. ; ; Given an accurate 10 MHz clock into pin2, periodic edge trigger events ; on pin5 are time-stamped to sub-microsecond resolution. The readings ; are output as plain ASCII messages at 19200 baud on pin3 to a host (PC) ; for display, logging, plotting, or statistical analysis. Single or average ; time interval (period), time error (phase), or pulse rate (frequency) ; measurements can be made from the raw data set. ; ; Typical uses may be to time individual mains frequency zero crossings, ; turns of a bicycle wheel, swings of a pendulum clock, rotations of an ; analog power meter, pulses from a digital kWh meter, engine tachometer ; signals, drips from a faucet, or human heart beats. The picPET can be ; used as a modest frequency counter (about 7 digits at one second). ; ; Each measurement is a 9-digit elapsed time and a 2-digit event count. ; Resolution is 100 ns, granularity is 400 ns, and accuracy is equal to ; the stability of the 10 MHz clock. The counter works for rates from ; as slow as one event a minute to as fast as 125 events per second. ; ; In this version: ; ; - GP0/pin7 (WPU) selects trigger edge. Ground for falling edge. ; Leave floating or set high for rising edge (default). ; ; - GP1/pin6 (WPU) allows optional 1PPS synchronization. Pull to ground ; and next event will reset time-stamp and event counters to zero. ; ; - GP2/pin5 is event input. ; ; - GP3/pin4 controls serial output polarity. Ground for normal "true" ; unipolar RS232. Set high for "inverted" serial (TTL, MAX232, FTDI). ; Do not leave this pin floating. ; ; - GP4/pin3 is serial output. ; ; - GP5/pin2 is clean precise digital 10 MHz clock input. ; ; - There is a 0.8 second delay after reset before Hello output. This ; gives host OS or USB/serial adapters time to settle on power-up. ; ; - Timestamp overhead (including serial transmit time) is 8 milliseconds. ; Thus the maximum data rate is about 125 samples per second. ; ; Diagram: ; ---__--- ; 5V (Vdd) +++++|1 8|===== Ground (Vss) ; 10 MHz input ---->|2 pP 7|<+--- Rise(1) Fall(0) ; RS232 output <----|3 05 6|<+--- Sync(0) ; RS232 invert o--->|4 5|<---- Event pulse input ; -------- ; Theory: ; ; A 6-digit BCD counter in a 10 kHz isochronous loop accumulates elapsed ; time in 100 us units. The 8-bit TMR1L register tracks cpu cycles (250) ; within that loop. The 8-bit TMR0 register counts pulses on pin5. At the ; point an edge-triggered interrupt occurs, a snapshot of these registers ; is made and later used to calculate the precise time-stamp of the event. ; ; The code is isochronous (all code paths take exactly the same number of ; cycles under all conditions). Elapsed time is kept by isochronous code ; execution rather than by hardware timers. ; ; There is no software jitter, hardware sampling jitter is 1 Tcy (400 ns) ; max, and the main loop is compensated for any interrupt time (including ; dummy cycles). Thus *no* cycles are ever lost and all measurements are ; as accurate as the time/frequency stability of the 10 MHz clock itself. ; ; Traditional interval timers measure *from* A *to* B, then C to D, then ; E to F, etc. By contrast a continuous time-stamping counter measures ; the time *of* A, of B, of C, D, E, and so on. It allows one to compute ; B-A, C-B, D-C, E-D, or E-C, C-A, or even E-A, etc. This flexibility, ; no time drift, and zero deadtime between measurements makes TSC (time ; stamping counters) more attractive than TIC (time interval counters) ; for some applications. ; ; Host software handles the 256 event (0x100 hex) counter wrap-around ; and 100 second (decimal) elapsed time counter wrap-around. Note that ; TS data can be converted into TI data if necessary. ; ; Blanking occurs while a time-stamp is output to the PC, however any ; events that happen to occur during this window are still counted. ; ; If fancy time-stamps are required (such as dates or local/UTC time), ; then host software combines its own less stable OS clock with the ; precise PIC time-stamps to create UTC absolute time-stamps. Note that ; for many applications differential seconds (interval) is sufficient. ; ; For more information see http://www.leapsecond.com/pic/ ; ; Version: ; ; 06-Oct-2009 Tom Van Baak (tvb) www.LeapSecond.com ; ; ---------------------------------------------------------------------------- PICPET_PID equ 0x05 ; picPET product id PICPET_VID equ 0x04 ; firmware version number