Alternate PWM Unit

This is a slightly modified library unit for taking advantage of the PICs hardware CCP module to do pulse width modulation.


All I've changed here (apart from reformatting the code to make it easier on the eyes) is to have added two compilation constants. These will make it easier to set things up from the main program. That is, there is no need to alter the unit itself when using it in various situations.

Compilation Constants

These are quite straightforward. The first establishes the frequency of the PWM waveform, while the other sets the desired mode. You can find what modes are available in your chip by referring to its data sheet. The mode is set by specifying the values of the CCP1CON bits. So, for example:

{$DEFINE PWM_FREQ 10000}                // 10 kHz 


The commands are pretty much self-explanatory:

procedure PWM_Init;

procedure PWM_Duty(aDuty : byte); 
Note that the specified duty cycle is 0..255, corresponding to 0%..100%.

procedure PWM_Start;

procedure PWM_Stop(aWaitLast : boolean);
The boolean flag indicates whether you want the PWM to stop at once, or wait for the current cycle to conclude before stopping.

Add It to Your Library Collection

And here's the new library unit, "PWM_CCP_alt.pas" for you to download:

Be sure to read over the source code for additional details on how to use it.

No comments:

Post a Comment