Frequency Divider

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

Moderators: Benj, Mods

Post Reply
mel
Posts: 9
Joined: Tue Dec 12, 2006 4:11 pm
Location: UK
Contact:

Frequency Divider

Post by mel »

I am using a PIC 16F 84A, and wanted to use it as a frequency divider, but am not sure how to do this within flowcode, or as to whether it is possible to do it.

For example: - I have a 555 timer chip configured in its astable mode, which generates an output squarewave frequency of 60 kHz (approximately). I want to send this to my PIC, and then divide it by, say, 14 within the PIC, to give me an output squarewave on one of the B ports of 4.2 kHz (approximately). I would be grateful for any feedback on this please.
Mel

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Post by Benj »

You can create a frequency devider in flowcode using just one variable.

Jump back to flowchart symbol

initialize your count variable to 0

while 1 block
{

while Input pin high{}

while Input pin low{}

count variable = count variable + 1

if count variable > count - 1 then change output pulse and jump back to flowchart symbol

}

User avatar
goldwingers
Posts: 118
Joined: Wed Sep 06, 2006 1:22 pm
Location: London
Been thanked: 1 time
Contact:

Hmmm

Post by goldwingers »

Benj,

Could you show this as a flowcode diagram, I am trying to develop two square wave outputs controlled by two ADC inputs, each would operate independantly, basically voltage controlled frequency oscillators... Can you help?

canipus
Posts: 31
Joined: Sun Nov 12, 2006 7:33 am
Contact:

Re: Hmmm

Post by canipus »

goldwingers wrote:Benj,

Could you show this as a flowcode diagram,
Assuming you're referring to the original request for frequency division that would be most helpful. I am also interested in the frequency division application and I'm trying to follow Benj's shorthand description but can't translate it into a Flowcode diagram.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Post by Benj »

Hello All, Ill try to make this as compatable with Flowcode as possible. Sorry I cant include images.

For the voltage controlled frequency oscillators you could use something like this.

Variable - Byte ADC_Value
Components - ADC Component - Connected to ADC channel
Single LED - Connected to output pin.

While 1
{
Call Component Macro - SampleADC
Call Component Macro - ReadAsByte -> ADC_Value
Output 1 to Single bit B0
Delay by ADC_Value ms
Output 0 to Single bit B0
Delay by ADC_Value ms
}

Using this method the frequency drops as the voltage increases. To invert this you could use a second variable and insert it after the ReadAsByte block. The second variable would be assigned:

INV_ADC_Value = 255 - ADC_Value

and would be used to control the delay as the ADC_Value was previously.
Last edited by Benj on Tue Jan 09, 2007 11:06 am, edited 1 time in total.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Post by Benj »

Now for the frequency devision.

Variable: Byte count_Var, Byte Num_Devisions, Byte output_Var

Components: Single switch connected to input bit
Single LED connected to output bit

Calculation: Num_Devisions = frequency devision eg. 8
Declare Connection Point reset_CP
Decision output_Var == 0
Yes: Calculation: output_Var = 1
No: Calculation: output_Var = 0
Output to single bit: output_Var
Calculation: count_Var = 0
while 1
{
Call Component Macro - Switches - Wait until High
Call Component Macro - Switches - Wait until Low
Calculation: count_Var = count_Var + 1
Decision count > Num_Devisions
Yes: Jump to connection point reset_CP
No: Carry on with while loop
}

Hope this helps with your coding. If your still having problems with it then I will create the flowchart and link it to the website.

User avatar
goldwingers
Posts: 118
Joined: Wed Sep 06, 2006 1:22 pm
Location: London
Been thanked: 1 time
Contact:

Further

Post by goldwingers »

Hi BenJ
Re the VCO's - I found your test file on outputting an audio sound last night and messed around with it, by adding the ADC, checking it as an Int which then gives 1013, nice round number for 0 - 10 volts, ( making it easier to do maths on also, I then created a calc to invert this so the frequency increased in line with voltage... So im half way there

2nd Part,
How do i make 2 VCO's on the same chip, I copied the above changes and set up another set of variables, this did work but only seemed for one vco to finish and the other begin, How can i have both running my prob is this;

My car is travelling at 30mph and the engine speed is 4000rpm, i need to initiate these frequencies from 2 VCO simultainiously, so for the first vco i need 30 hz, and the second 400ohz - HELP

canipus
Posts: 31
Joined: Sun Nov 12, 2006 7:33 am
Contact:

Post by canipus »

Benj wrote:Now for the frequency devision.

Variable: Byte count_Var, Byte Num_Devisions, Byte output_Var

Components: Single switch connected to input bit
Single LED connected to output bit

Calculation: Num_Devisions = frequency devision eg. 8
Declare Connection Point reset_CP
Decision output_Var == 0
Yes: Calculation: output_Var = 1
No: Calculation: output_Var = 0
Output to single bit: output_Var
Calculation: count_Var = 0
while 1
{
Call Component Macro - Switches - Wait until High
Call Component Macro - Switches - Wait until Low
Calculation: count_Var = count_Var + 1
Decision count > Num_Devisions
Yes: Jump to connection point reset_CP
No: Carry on with while loop
}

Hope this helps with your coding. If your still having problems with it then I will create the flowchart and link it to the website.
Many thanks Ben. It was the frequency divider that I needed. The ability to be able to take a low cost PIC and turn it into a dedicated frequency divider is just what I was looking for, especially with so many CMOS/TTL logic circuits becoming obsolete. I think I can follow what I need to do now.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Further

Post by Benj »

goldwingers wrote:i need to initiate these frequencies from 2 VCO simultainiously, so for the first vco i need 30 hz, and the second 400ohz - HELP
Goldwingers. There are several ways to achieve 2 VCO's simultainiously.

1) Use an interrupt to drive one of the VCO's. So as the processor's runtime is consumed with worrying about the first VCO the interrupts take care of the second. (Maybe quite tricky to code)

2) Use two microcontrollers and then pass the data to a third or just communicate between the 2 to obtain both of the desired outputs on the same chip.(fairly simple)

3) Use a timer driven interrupt to jump between the 2 control loops. This will grant you the use of both VCO's but again will be fairly difficult to code.

canipus
Posts: 31
Joined: Sun Nov 12, 2006 7:33 am
Contact:

Post by canipus »

canipus wrote:
Benj wrote:Now for the frequency devision.

Decision output_Var == 0

Hope this helps with your coding. If your still having problems with it then I will create the flowchart and link it to the website.
Ben

What does the double == sign stand for?

Canipus

User avatar
goldwingers
Posts: 118
Joined: Wed Sep 06, 2006 1:22 pm
Location: London
Been thanked: 1 time
Contact:

Final post from me

Post by goldwingers »

Ben,
Thanks so much will go the two chip route as the micro has other stuff to do other than the VCO's, Many thanks for your advice

Ian

canipus
Posts: 31
Joined: Sun Nov 12, 2006 7:33 am
Contact:

Post by canipus »

What does the double == sign stand for?

Ok answered my own Q - found the answer on the web under C Syntax re relational and assigned operators.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Post by Benj »

No problem Goldwingers. Let me know how you get on

The double == stands for is it equal to eg
Not sure if you need the double equals in Flowcode as I think the decision blocks do all the work for you.

Var = 1; //assigns 1 to Var
if (Var == 0) //returns a 1 if Var is 0 or returns a 0 if Var not a 0

a worked example of this is

if (Var == 0)
{
//Var = 0
some code;
}
else
{
//Var ! (not equal) 0
other code;
}

Post Reply