The standard parallel port "pulsing" setup in Mach3 uses a set "kernel speed" (25-100kHz) and this is the rate at which it decides to output a step pulse (or not). It is therefore also the maximum pulsing rate. The implementation details are specific to Mach3, but how would you determine an "even" pulsing pattern?

Let's rephrase our problem: How do you evenly take Y steps over X periods (or, determine which points in a 2 dimensional raster should be plotted in order to form a close approximation of a straight line between two given points). Bresenham's line algorithm is a way to achieve this.

At the start, we're already 1/2 way to crossing into the next Y position. For each increment in X, we move Y/X closer. Once we cross into the next Y position, we're another 1 away from the next crossing.

This could be expressed as:

var accumulator = 0.5; loop (X times) accumulator = accumulator + Y/X; if (accumulator >= 1) accumulator = accumulator - 1; //step!

We can eliminate the fractional 1/X & 0.5 and use only integer math by multiplying everything by 2X.

var accumulator = X; loop (X times) accumulator = accumulator + 2Y; if (accumulator >= 2X) accumulator = accumulator - 2X; //step!

We can also reduce calculation load by precalculating and storing 2Y & 2X.

This algorithm gives nice even pulse patterns. An example of 0 to a maximum of 10 pulses is shown below:

1 1234567890 0 .......... 1 ....X..... 2 ..X....X.. 3 .X..X...X. 4 .X.X..X.X. 5 X.X.X.X.X. 6 X.X.XX.X.X 7 X.XXX.XX.X 8 XX.XXXX.XX 9 XXXXX.XXXX 10 XXXXXXXXXX

The integer maths also makes this suitable for microcontrollers.

Below is an example of 3 & 7 (yellow top & blue bottom) pulses per 10 periods being generated by an AVR micro.

When we produce a continuous stream of these pulses, the pattern repeats (prior & next pattern shaded below)

So, how much processing power is consumed by this? I'm using a 256 cycle ring-buffer to precalulate 4 output channels, and inserting a 10ms delay when the buffer fills. To help measure calculation duration, I toggled a line (yellow) high during buffer re-fill. One of the output channels is display below (it's 25kHz signal is not decernable at this resolution).

## Add comment

biuquote