frequency counter

Discussion of issues related to writing ZBasic applications for targets other than ZX devices, i.e. generic targets.
Post Reply
rich
Posts: 81
Joined: 19 November 2015, 12:23 PM

frequency counter

Post by rich »

I would like to measure the frequency of an external digital input with the Zx328n.

My range is 100 Hz to 1 MHz.
I want it to be as accurate as possible.
The accuracy is the number one requirement.

If a small number of external chips are necessary, that would be ok.

If 'C' or asm needs to be written, that would be ok too.

I have searched the forum, and have not found what i need.

Can any one help with this?

Thank you
Richard
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: frequency counter

Post by dkinzer »

rich wrote:I would like to measure the frequency of an external digital input with the Zx328n. My range is 100 Hz to 1 MHz.d
Probably the simplest way to do this would be to use InputCaptureEx(). The tricky part will be selecting the clock rate that will work over the range you've indicated.

An alternate strategy would be to use the signal that you intend to measure as the external clock source for the 16-bit timer and then let the timer run for a fixed period of time. Given the accumulated timer count and the known time period you can calculate the frequency. With this strategy you would want to let the timer run for a small enough time such that the 16-bit count doesn't overflow at the highest frequency you expect but which is also large enough to get a large enough count to get the desired accuracy with the lowest frequency you intend to measure. This may be difficult to attain given that you have specified four orders of magnitude of range. It may be necessary to make the measurement using a multi-step process with diminishing accumulation duration until the timer doesn't overflow.
- Don Kinzer
rich
Posts: 81
Joined: 19 November 2015, 12:23 PM

Post by rich »

Don, thank for the info.

I have not checked but if the 'T0' for 'T1' pins go to a 16 bit counter i can do it.

I have to read up on the timers.

Thanks again.
Ricahrd
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

rich wrote:I have not checked but if the 'T0' for 'T1' pins go to a 16 bit counter[...]
T0 is the external clock input for Timer0. T1 is the external clock input for Timer1. T2 is the external clock input for Timer2. Timer1 is the 16-bit timer while the other two are 8-bit.

On the mega328P, D.5 is T1.

To time the duration of the frequency measurement, you may be able to use the GetElapsedMicroTime() routine. If that doesn't give you sufficient accuracy you may need to use Timer2 and a special interrupt service routine.
- Don Kinzer
rich
Posts: 81
Joined: 19 November 2015, 12:23 PM

Post by rich »

Thanks much

Richard
Post Reply