Linear frequency control

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Post Reply

Please rate this article out of 5: (1 being the worst and 5 being the best)

1
0
No votes
2
0
No votes
3
0
No votes
4
0
No votes
5
3
100%
 
Total votes: 3

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:

Linear frequency control

Post by Benj »

It is relatively easy to provide linear control of the duration of a timed event using most microcontrollers. A simple variable can be used to control the number of cycles of a timing loop, or can be loaded into the compare registers provided by some of the more advanced timer modules. In either case the timed period, t, will be proportional to the value of the variable.

Linear control of frequency, f, can be more complicated unless more specialized hardware is available. As t=1/f, the obvious solution would seem to be to perform the division calculation to derive the required time period from the supplied frequency value. Flowcode V4 can support the floating-point calculations that would usually be required by this method, but there is a much quicker and simpler approach that can be used in many applications.
sa1.JPG
sa1.JPG (8.94 KiB) Viewed 9220 times
Phase accumulator:

The phase accumulator method uses the control variable to represent a phase increment value. The phase increments are continuously added to the phase accumulator (a larger variable used to hold a running total) at fixed time periods. Small values cause the accumulator value to increment slowly, large values cause it to increment proportionally faster, causing certain events to occur at a linearly controlled frequency.

The accumulator value can be used in a number of ways to generate an output.

1. Compare
On each increment, the value in the accumulator can be compared with a target value. When the target value is matched, or exceeded, the required operation is performed, and the accumulator value decremented by the target value to initialize the next cycle.

2. Address generation
The most significant bits of the accumulator are used directly as the index value for an array containing waveform data. On each accumulator increment, the indexed array data is accessed and used to generate almost any arbitrary audio waveform, control sequence, etc., with the required linear frequency control.

Example:

The example program linked to this article uses the phase accumulator technique to allow linear, analog, control of the speed of a stepper motor. The program is written in Flowcode AVR V3 to allow compatibility with all Flowcode V3/4 options. The V4 stepper component is not used and is replaced with a simple array of phase pattern data. The program can be loaded or imported directly into other V3 or V4 versions of Flowcode (PIC, AVR, ARM). Some adjustment of interrupt configuration might be required when importing to the alternative device types.

Note: The 'SampleADC' macro will be ignored in Flowcode V4 and can be removed.

The analog input is read as an 8-bit conversion, which is reduced to 7-bits to provide the 1% full scale resolution required by this application. Analog sampling and phase accumulator updates are triggered by a Timer0 overflow interrupt.

The phase accumulator is an int variable. This 16-bit, signed variable has been limited to 14-bit values to prevent problems with negative number representations.

The three most significant active bits of the phase accumulator are used as the index to an array of eight stepper motor phase output values (half-step mode) that are written to the selected port each time the phase accumulator is updated. In most cases the same value will be written for several successive updates until the relevant bits of the phase accumulator change.

Calculations:

The timer interrupt frequency is based on: 20MHz Xtal; 1:8 prescaler; 256 timer overflow count:

Timer interrupt frequency = 20000000Hz / 8 / 256 = 9765.625Hz

At the maximum input value (maximum frequency) the phase accumulator will be incremented by 255 on each interrupt. The phase accumulator will overflow after 129 interrupts

Maximum accumulator overflow frequency = 9765.625Hz / 129 = 75.7Hz

Due to the stepper phase patterns generated by the array, eight half-steps (four full-steps) will have been generated in this period.

The maximum stepper speed is 75.7Hz x 4 = 302.8 full-steps per second.

Alternative crystal frequencies, timer configurations, and sizes of variables, can allow alternative frequency ranges and adjustment resolutions to be achieved.
sa2.JPG
sa2.JPG (18.62 KiB) Viewed 9205 times
Other advantages of the phase accumulator method are:
Its ability to generate a zero frequency output without having to represent infinite time.
The filtering effect of the accumulator on noisy control signals.
StepperSpeed.fcf_avr
(5 KiB) Downloaded 714 times

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: Linear frequency control

Post by Benj »

Here is a v4 PIC version...
Attachments
StepperSpeed.fcf
(6.5 KiB) Downloaded 652 times

tulipman
Posts: 20
Joined: Sun Oct 10, 2010 10:40 pm
Has thanked: 3 times
Been thanked: 1 time
Contact:

Re: Linear frequency control

Post by tulipman »

Hi there,

As I have the EB-011 module, I wanted to try out the code

But in order to do that, I need some more information (goes for all projects in FC4):
Is there a way of put some project hardwaresetup info somewhere in the *.fcf file??
Like:
1) microcontroller used
2) settings of the switches on the EB-006
3) where the E-Blocks should be attached to the EB-006

Only with above mentioned info one is able to setup the hardware and run the program, IMHO.


Thanks in advance for your kind reply.

Best rgds

Robbert van Herksen
Soesterberg, NL



Benj wrote:It is relatively easy to provide linear control of the duration of a timed event using most microcontrollers. A simple variable can be used to control the number of cycles of a timing loop, or can be loaded into the compare registers provided by some of the more advanced timer modules. In either case the timed period, t, will be proportional to the value of the variable.

Linear control of frequency, f, can be more complicated unless more specialized hardware is available. As t=1/f, the obvious solution would seem to be to perform the division calculation to derive the required time period from the supplied frequency value. Flowcode V4 can support the floating-point calculations that would usually be required by this method, but there is a much quicker and simpler approach that can be used in many applications.
sa1.JPG
Phase accumulator:

The phase accumulator method uses the control variable to represent a phase increment value. The phase increments are continuously added to the phase accumulator (a larger variable used to hold a running total) at fixed time periods. Small values cause the accumulator value to increment slowly, large values cause it to increment proportionally faster, causing certain events to occur at a linearly controlled frequency.

The accumulator value can be used in a number of ways to generate an output.

1. Compare
On each increment, the value in the accumulator can be compared with a target value. When the target value is matched, or exceeded, the required operation is performed, and the accumulator value decremented by the target value to initialize the next cycle.

2. Address generation
The most significant bits of the accumulator are used directly as the index value for an array containing waveform data. On each accumulator increment, the indexed array data is accessed and used to generate almost any arbitrary audio waveform, control sequence, etc., with the required linear frequency control.

Example:

The example program linked to this article uses the phase accumulator technique to allow linear, analog, control of the speed of a stepper motor. The program is written in Flowcode AVR V3 to allow compatibility with all Flowcode V3/4 options. The V4 stepper component is not used and is replaced with a simple array of phase pattern data. The program can be loaded or imported directly into other V3 or V4 versions of Flowcode (PIC, AVR, ARM). Some adjustment of interrupt configuration might be required when importing to the alternative device types.

Note: The 'SampleADC' macro will be ignored in Flowcode V4 and can be removed.

The analog input is read as an 8-bit conversion, which is reduced to 7-bits to provide the 1% full scale resolution required by this application. Analog sampling and phase accumulator updates are triggered by a Timer0 overflow interrupt.

The phase accumulator is an int variable. This 16-bit, signed variable has been limited to 14-bit values to prevent problems with negative number representations.

The three most significant active bits of the phase accumulator are used as the index to an array of eight stepper motor phase output values (half-step mode) that are written to the selected port each time the phase accumulator is updated. In most cases the same value will be written for several successive updates until the relevant bits of the phase accumulator change.

Calculations:

The timer interrupt frequency is based on: 20MHz Xtal; 1:8 prescaler; 256 timer overflow count:

Timer interrupt frequency = 20000000Hz / 8 / 256 = 9765.625Hz

At the maximum input value (maximum frequency) the phase accumulator will be incremented by 255 on each interrupt. The phase accumulator will overflow after 129 interrupts

Maximum accumulator overflow frequency = 9765.625Hz / 129 = 75.7Hz

Due to the stepper phase patterns generated by the array, eight half-steps (four full-steps) will have been generated in this period.

The maximum stepper speed is 75.7Hz x 4 = 302.8 full-steps per second.

Alternative crystal frequencies, timer configurations, and sizes of variables, can allow alternative frequency ranges and adjustment resolutions to be achieved.
sa2.JPG
Other advantages of the phase accumulator method are:
Its ability to generate a zero frequency output without having to represent infinite time.
The filtering effect of the accumulator on noisy control signals.
StepperSpeed.fcf_avr

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: Linear frequency control

Post by Benj »

Hello,
But in order to do that, I need some more information (goes for all projects in FC4):
Is there a way of put some project hardwaresetup info somewhere in the *.fcf file??

1) microcontroller used - Look in the View -> Project Options menu. The target is shown here.
2) settings of the switches on the EB-006 - Look in the Chip -> Configure menu. The Config settings are shown here.
3) where the E-Blocks should be attached to the EB-006 -> Look in the component connections. The pins used by the component are shown here.

tulipman
Posts: 20
Joined: Sun Oct 10, 2010 10:40 pm
Has thanked: 3 times
Been thanked: 1 time
Contact:

Re: Linear frequency control

Post by tulipman »

Benj wrote:Hello,
But in order to do that, I need some more information (goes for all projects in FC4):
Is there a way of put some project hardwaresetup info somewhere in the *.fcf file??

1) microcontroller used - Look in the View -> Project Options menu. The target is shown here.
2) settings of the switches on the EB-006 - Look in the Chip -> Configure menu. The Config settings are shown here.
3) where the E-Blocks should be attached to the EB-006 -> Look in the component connections. The pins used by the component are shown here.

Hi Ben,

THANKS for the answers:

I could find my way to the solutions for answers 1 and 2,
but for your answer 3 I'm stuck a little.

You say: look in the components connections.

In the stepperspeed.fcf file there is currently only one component [ADC(1)], displayed as a potmeter in the the Panel area.
If I look at the connection of this component, is says that it's connected to ADC(1)
[BTW: in the flowchart I see a ADC(0), but when I load the StepperSpeed.fcf file, FC4 pops up a warning, saying that ADC(0) is not used anymore)
and my (dummie?) assumption is: there is no ADC(0) nescessary, but it's probably is still in the code to accomodate FC3 users???
As I have FC4, can I remove it??

Somewhere in the code I see that port D is used.
Should I therefore connect my EB-011 to port D??

The project is about steppermotors, but I don't see a stepper component in the panel.
If I run the sumulation in FC4, I don't see anything happening, as there is no stepper component in the panel area.
Should I maybe add a stepper component myself??

How do I connect a potmeter, to control the steppers?
Should I buy an E-Block for that?

I may appear like a dummie, but I'm still in a learning process.
Actually, I'll join a FC4 day-course on December 11, for some real hands-on action.
I very much look forward to that day!!!

Cheers

Robbert

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: Linear frequency control

Post by Benj »

Hello,
In the stepperspeed.fcf file there is currently only one component [ADC(1)], displayed as a potmeter in the the Panel area.
If I look at the connection of this component, is says that it's connected to ADC(1)
[BTW: in the flowchart I see a ADC(0), but when I load the StepperSpeed.fcf file, FC4 pops up a warning, saying that ADC(0) is not used anymore)
and my (dummie?) assumption is: there is no ADC(0) nescessary, but it's probably is still in the code to accomodate FC3 users???
As I have FC4, can I remove it??
Flowcode v4 will say that it no longer needs the sample component macro for the adc because the sample is now automatically called by the read functions. You can view the ADC pin connection by right clicking the component and selecting connections.
Somewhere in the code I see that port D is used.
Should I therefore connect my EB-011 to port D??
Yes I seem to remember that the example you are using controlled a motor on PortD using output icons.
The project is about steppermotors, but I don't see a stepper component in the panel.
If I run the sumulation in FC4, I don't see anything happening, as there is no stepper component in the panel area.
Should I maybe add a stepper component myself??
Yes you can instead add a stepper component and use the component macros rather then using the example which is using output icons and a state machine. There are also stepper motor examples as part of the main Flowcode 4 examples pack.
How do I connect a potmeter, to control the steppers?
Should I buy an E-Block for that?
The EB003 has a potentiometer onboard or you could simply connect one end of the pot to +5V, the other end to GND and the middle pin to a analogue capable pin (ANx on the chip diagram).
I may appear like a dummie, but I'm still in a learning process.
Actually, I'll join a FC4 day-course on December 11, for some real hands-on action.
I very much look forward to that day!!!
Great to hear it, I recently gave some training on Flowcode and everyone seemed to enjoy the experience very much.

Post Reply