Page 1 of 1

Frequency intervals

Posted: Mon Feb 05, 2007 12:37 pm
by Ganr
Hey :)

Im doing a projekt where I have an input pin where the input can be different frequencies for example:

2000hz
1500hz
1000hz
500 hz

How do I make flowcode do an action for each frequency?

I've figured its an interrupt function, but I have no idea how to count it for like Β½ a sec or way less

Any takers?

Posted: Mon Feb 05, 2007 3:05 pm
by Mark
You may be able to do this by polling a single input pin.

Check the pin (while pin is low) and when it changes
start a counter, ([while pin is high] holding [count = count + 1])

How far the counter has got for a single high cycle (for a 1:1 duty cycle signal) will be a measure of its frequency. (use another while loop to keep counting to next pin change if not 1:1 duty cycle and double the numbers below).

2000Hz count = 250us = x counts (guestimate x=1000 with 20M clock)
1000Hz count = 500us
500Hz count = 1000us
etc

Then use a decision tree, i.e. if Count > 250 and count < 1000 then frequency = (sort of) 1000Hz. You decide on the break points bteween frequencies. Counter will have to be an integer variable.

Either use a scope and a test loop to calibrate the programme or take apart the assembler code to see how long one count cycle is.

Hope this helps,

Mark

Posted: Tue Feb 06, 2007 11:02 am
by Benj
Hello

Another way to do it would be to start the TMR0 interrupt going and count the number of interrupts between input logic changes. Eg start the timer with TRM0 = 10 or something similar. You will need to know the Timer operating speed. You could even use the TOCKI input pin to increment the timer and have one variable counting interrupts and one variable counting the program cycles between. Eg how many interrupts have occured in 1000 program cycles. This would give a fairly accurate approximation of the input frequency.

Posted: Tue Feb 06, 2007 7:27 pm
by Mark
Yes, Ben's approach is neater.

Another variation would be to count clock inputs on Timer 1 input pin and use Timer 0 to interrupt and read Timer 1 and reset it. That way the programme would get a direct count of clock cycles per time interval, correctly selecting Timer 0 may even give a direct reading of cycles per second/64 or similar, which can be easily recalculated to cycles per second.

However, directly clocking in an input ignores issues of debouncing and spikes which are more easily tackled with a polling type method (as above).

Hope this helps,

Mark