PASM waitpeq to measure the period of a frequency
StefanL38
Posts: 2,292
Hi,
I would like to know if the following code is measuring the period of a frequency with maximum possible precision.
The cog is stopped at the waitpeq-cmd until the specified bit matches the specified value.
If it does match - the time it takes to execute the command
move, firstpulse , cnt
is the same as with
move, secondpulse, cnt
so that after calculating the period-time in clockticks with
the period-time of the pulse is EXACTLY within the precision of clockticks (=12,5 nanoseconds at 80 MHz)?
If this is not exact- is there another way to get in measured with a resolution of one (or maybe 4 clockticks)?
best regards
Stefan
I would like to know if the following code is measuring the period of a frequency with maximum possible precision.
mov _InputStateM, #0 'load BitPattern ZERO WaitIRPinLow waitpeq _InputStateM, _IR_Recv_Pin2 'wait until specified bit "_IR_Recv_Pin" is ZERO 'which means pulsetrain starts mov _FirstPulse, cnt 'take snapshot of free running systemcounter mov _InputStateM, #8 'load bitpattern that should be matched WaitIRPinHigh waitpeq _InputStateM, _IR_Recv_Pin2 'wait until INA-registerbit specified in _IR_Recv_Pin2 'has the same state as in _InputStateM mov _InputStateM, #0 'load BitPattern ZERO WaitIRPinLow2 waitpeq _InputStateM, _IR_Recv_Pin2 'wait until specified bit "_IR_Recv_Pin" is ZERO mov _SecondPulse, cnt 'take another snapshot of free running systemcounter mov _Period, _SecondPulse #calculate difference of systemcounter-snapshots sub _Period, _FirstPulse 'calculate clockticks of pulsedistance (=carrier-frequency)I assume if the frequency is not near the maximum that could be measured this way.
The cog is stopped at the waitpeq-cmd until the specified bit matches the specified value.
If it does match - the time it takes to execute the command
move, firstpulse , cnt
is the same as with
move, secondpulse, cnt
so that after calculating the period-time in clockticks with
mov _Period, _SecondPulse #calculate difference of systemcounter-snapshots sub _Period, _FirstPulse 'calculate clockticks of pulsedistance (=carrier-
the period-time of the pulse is EXACTLY within the precision of clockticks (=12,5 nanoseconds at 80 MHz)?
If this is not exact- is there another way to get in measured with a resolution of one (or maybe 4 clockticks)?
best regards
Stefan
Comments
Here is a frequency counter I wrote in PropBasic. It is well commented and should help.
Bean
Are you sure you are trying to measure the period? Period of a frequency is 1/frequency. From the comments in your code it seems like you want to measure the time between two waveforms or pulse trains. Measuring frequency can be done with the counters. I'm pretty sure there's an example in the AN001 App Note. But if my suspicion is correct, that won't help you. Are you timing the arrival of consecutive pulse trains, or simply measuring the period of a consecutive pulses?
I think the solutions are very different- the latter being much easier.
Peter
I think one of the things you are asking is "what is the resolution/accuracy of waitpeq/waitpne" - yes its 1 system clock (12.5ns if 80MHz).
And the other thing "is this the most accurate way to measure frequency?" - to which the answer is a resounding NO....
The accurate way is to measure the time for _many_ periods and divide this by the number of periods timed. So if measuring 100kHz the above way the resolution of 12.5ns out of a period of 10us is 0.125%. Measuring 100,000 periods gives a precision of 0.00000125% (beyond the accuracy of your crystal!).