# Using Timer1 and CCP pins to Measure RPM (period)

Submitted by:

Glenn Clark
VersaTech Electronics

The programs and drawings shown below are taken from the examples section of the manual.

Measurement of RPM or the time between repetitive events is simple. In the last example, the time base could only be counted while the signal input was high and during the first cycle following the rising edge of that signal. By eliminating one of the AND gates, the time base is counted during the entire first cycle following arming. By taking the reciprocal of the time, we have the RPM. Doing the reciprocal requires a bit of mathematic manipulation, but nothing too hard for the TICkit. First, the revised circuit: The following program shows fixed point arithmetic used to scale the results for 1000 RPS (rotations per second). Realistically, we should slow our time base, but this shows how sensitive this method can be. We choose a scale where 1000000 represents the number 1.000000. When we divide 1 by the number of microseconds the result is the number of millions of events that took place in one second. Due to our scale, we can simply move our imagined decimal point to the right to see how many thousands of events took place per second.

Use the con_fmt() function detailed earlier to display the scaled results on the debug console. The result is shown in thousands of rotations per second with 2 decimal places of precision. Examine the code to see how this is done:

```DEF tic62_c
LIB fbasic.lib

GLOBAL word last_count 0
GLOBAL byte count_done 0b

FUNC none main
BEGIN
rs_param_set( debug_pin )
pin_low( pin_a2 )
tmr2_cont_set( tmr2_con_on )
tmr2_period_set( 4b )     ; set for a period of 1.0 us
ccp1_cont_set( ccp_pwm )
ccp1_reg_set( 2w )        ; set for approx 50% duty cycle

; time base is now operational
pin_low( pin_a3 )         ; reset trigger circuit
tmr1_reg_set( 0 )         ; clear timer1
pin_high( pin_a3 )        ; arm the trigger circuit

; pulse measurement circuit is now active
REP
IF <>( last_count, 0 )
IF ==( last_count, tmr1_count_get())
++( count_done )
ENDIF
ENDIF

; do whatever during the body of the loop.
; timing is not critical.
=( last_count, tmr1_reg_get())
UNTIL count_done

con_string( "Pulse Width = " )
con_out( last_count )
con_string( "us" )
=( rps_result, /( 1000000L, last_count ))
con_string( "Thousands of Rotations Per Second = " )
con_fmt( rps_result, "####.00X" )
REP
debug_on()
LOOP
ENDFUN```

Protean Logic Inc. Copyright 05/06/04         Top of Page