Frequency intervals

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Frequency intervals

Postby Ganr » Mon Feb 05, 2007 12:37 pm

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?
Ganr
 
Posts: 2
Joined: Mon Feb 05, 2007 12:12 pm
Has thanked: 0 time
Have thanks: 0 time

Advertisement

Postby Mark » Mon Feb 05, 2007 3:05 pm

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
Go with the Flow.
Mark
Flowcode v5 User
 
Posts: 209
Joined: Thu Oct 19, 2006 11:46 am
Location: Bakewell, UK
Has thanked: 20 times
Have thanks: 16 times

Postby Benj » Tue Feb 06, 2007 11:02 am

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.
User avatar
Benj
Matrix Staff
 
Posts: 8707
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix Multimedia Ltd
Has thanked: 1461 times
Have thanks: 1813 times

Postby Mark » Tue Feb 06, 2007 7:27 pm

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
Go with the Flow.
Mark
Flowcode v5 User
 
Posts: 209
Joined: Thu Oct 19, 2006 11:46 am
Location: Bakewell, UK
Has thanked: 20 times
Have thanks: 16 times


Return to Flowcode V2 & V3

Who is online

Users browsing this forum: No registered users and 1 guest