matt_hau said:
Does this code look better?
367hz is the frequency of the coil pulses at 11,000 RPM which works out to be about 37 pulses per 100uS.
No it's not.
367 Hz is the frequency of coil pulses at 11,000 rpm on a 4 cylinder, 4 stroke engine.
However, 367 Hz is 37 pulses in 100 ms, not 100 :mu:s
The programmes to measure frequency are quite a lot more involved than what you have written.
You need to count time and pulses simultaneously.
A PIC running at 4 MHz executes 1,000,000 steps per second, but it is almost impossible to use that fact if you are writing in C because you have no idea how many steps the compiler makes out of each line you write.
All PICs have timers, and that is what is used to count time, while the program counts the pulses.
The program needs to set a timer running. You then have a loop that checks if a new pulse has arrived, and if so increments a counter. The loop then checks if the timer has finished. If not, the program goes back to seeing if a pulse has arrived.
When the timer has finished, the counter value divided by the timer period is the frequency. If you arrange the timer scaling correctly, the counter value can be the answer you want directly.
The code then does whatever it needs with the counter value, like turn on the lights.
Then the counter is reset, then go back to where the timer is set running.
That is one method. It is possible to use either hardware timers or software for both pulses and time.
**broken link removed**
That uses a timer to count the pulses and the code to count the time
http://www.taylorriver.com/content/view/131/25/
That uses two timers, one to count pulses and the other to count time.