- Published: Saturday, 04 April 2009
- Written by Digital DIY
Download the module: PWM2.bas
Other programs that use this module:
- Large LED Array & Brightness Control via PWM
- Digital to Analogue Conversion (DAC) via PWM
- Infrared (IR) Modulated UART
Variable Frequency PWM Example
The SetFreqByTable() function uses a constants table of PR2 and Timer2 Prescaler load values so the user can switch easily between optimum frequencies and duty cycle resolution. A small utility program has been created to simplify the creation of this array of constants which can be downloaded from HERE. A screenshot of this utility is provided below.
Here is example code that uses some of the newly added functions. This example uses an 18F2550 @ 48MHz. Potentiometers are connected to AN2 and AN3 which create the variable duty-cycle for PWM1 and PWM2 outputs. A pushbutton connected to GND and PORTB.0 is used to toggle between PWM frequencies of 11.75KHz and 5.8KHz which have resolutions of 1024 and 512, respectively. These frequencies were determined from a constants table created with the PWM_Writer Utility. The EasyPIC3 PIC development board was used for this test.