Hi,
Pitch = frequency, right? If so that is exactly what the above code is supposed to do. Though it could be tweaked to measure half a period instead of a full.
What kind of performance to expect (and how or even IF to do to it) depends on the dynamic range you want. Is it 110-1100Hz or 11 to 11000Hz?
Say you're running at 4Mhz, at 20Hz one half-period is 25ms or 25000 ticks - pretty good and you've got plenty of resolution as one tick equals a change in frequency of 20/25000=0.0008Hz ( I wonder if you can hear that? ).
But go up an order of magnitude, or two, and do the math at 2kHz. Now a half cycle is 1/2000/2=250us and the resolution drops conciderably. One tick now equals a change in frequency of 2000/250=8Hz. At 10kHz all you'll get is 50ticks per half cycle and the resolution has now dropped to 1 tick per 200Hz - that kind of sucks.
Run the PIC at 40MHz instead of 4 and get 10 times the resolution. But then the problem comes at the low end where the timer register will overflow. I guess that could be handled by counting the number of overflows though.
So, it depends on the range you want/need and what kind of resolution and accuracy you need. 138Hz probably, 138.5 perhaps, 138.59 maby, 138.591.... you get the idea.
There might be OTHER ways of doing it though - which I haven't thought about. But lets not mess this thread up, if you want to discuss this further may I kindly suggest we bring it to a new thread.
/Henrik.
Bookmarks