here is what I did to implement the idea mentioned above - on a different target but the gist should be the same
1. i configured a timer (TIMER5 in my case) to take input on a pulse train.
2. in the loop(), I periodically (=0.5sec) check the reading of that timer to calculate how many pulses have elapsed since the last read.
from that, I can calculate the pulse rate.
for debugging, I used another timer to generate a pulse train at a known frequency (80000 in my case). At a half second interval, I should be expecting 40,000 counts from pcGet().
Code: Select all
F_PHB= 0,004,000,000 ticks= 0,008,022,692 tmp0 = 0,000,000,002 PR2 = 0,000,000,049 pcGet= 0,000,040,004
F_PHB= 0,004,000,000 ticks= 0,012,023,092 tmp0 = 0,000,000,002 PR2 = 0,000,000,049 pcGet= 0,000,040,004
F_PHB= 0,004,000,000 ticks= 0,016,023,492 tmp0 = 0,000,000,002 PR2 = 0,000,000,049 pcGet= 0,000,040,004
F_PHB= 0,004,000,000 ticks= 0,020,023,892 tmp0 = 0,000,000,002 PR2 = 0,000,000,049 pcGet= 0,000,040,004
F_PHB= 0,004,000,000 ticks= 0,024,023,492 tmp0 = 0,000,000,004 PR2 = 0,000,000,049 pcGet= 0,000,039,990
F_PHB= 0,004,000,000 ticks= 0,028,023,892 tmp0 = 0,000,000,002 PR2 = 0,000,000,049 pcGet= 0,000,040,010
F_PHB= 0,004,000,000 ticks= 0,032,023,492 tmp0 = 0,000,000,004 PR2 = 0,000,000,049 pcGet= 0,000,039,990
F_PHB= 0,004,000,000 ticks= 0,036,023,892 tmp0 = 0,000,000,002 PR2 = 0,000,000,049 pcGet= 0,000,040,010
F_PHB= 0,004,000,000 ticks= 0,040,023,492 tmp0 = 0,000,000,004 PR2 = 0,000,000,049 pcGet= 0,000,039,990
F_PHB= 0,004,000,000 ticks= 0,044,023,892 tmp0 = 0,000,000,002 PR2 = 0,000,000,049 pcGet= 0,000,040,010
F_PHB= 0,004,000,000 ticks= 0,048,023,492 tmp0 = 0,000,000,004 PR2 = 0,000,000,049 pcGet= 0,000,039,990
F_PHB= 0,004,000,000 ticks= 0,052,023,892 tmp0 = 0,000,000,002 PR2 = 0,000,000,049 pcGet= 0,000,040,010
F_PHB= 0,004,000,000 ticks= 0,056,023,492 tmp0 = 0,000,000,004 PR2 = 0,000,000,049 pcGet= 0,000,039,990
F_PHB= 0,004,000,000 ticks= 0,060,023,892 tmp0 = 0,000,000,002 PR2 = 0,000,000,049 pcGet= 0,000,040,010
F_PHB= 0,004,000,000 ticks= 0,064,023,492 tmp0 = 0,000,000,004 PR2 = 0,000,000,049 pcGet= 0,000,039,990
F_PHB= 0,004,000,000 ticks= 0,068,023,892 tmp0 = 0,000,000,002 PR2 = 0,000,000,049 pcGet= 0,000,040,010
F_PHB= 0,004,000,000 ticks= 0,072,023,492 tmp0 = 0,000,000,004 PR2 = 0,000,000,049 pcGet= 0,000,039,990
F_PHB= 0,004,000,000 ticks= 0,076,023,892 tmp0 = 0,000,000,002 PR2 = 0,000,000,049 pcGet= 0,000,040,010
the jitter is caused by interrupts happening during the loop execution.
the pulse rate I'm running at is F_PHB / (PR2 + 1) = 80K. so at 0.5s intervals, I should be getting 40K, vs. 39.99 - 40.01K.
the code can count upto Mhz range, depending on the mcu's clock rate - it is basically a crude frequency meter. you can enhance it by measurign the gate time and to handle the overflow - so you can measure a wide input frequency range.