FIGURE 5. Breadboard
version of the PWM
controller.
HPWM calculations Excel spreadsheet.
Timer2 Calculations for PIC12F683 PWM
For CCS C Compiler
Miscellaneous Calculations
Crystal Frequency ( Fosc)
Prescaler (1, 4, 16)
Cycle Time (Tosc)
PR2 (Timer2 period: 0-255)
PWM Period (overflow)
Duty Resolution
PostScaler - sets interrupt (1-16)
Interrupt period
8 000 000. Hz
1.
0.000 000 5 Sec
2 000 000. Hz
200.
0.000 100 5 Sec
9 950.2 Hz
804. ;;1024 is maximum possible ( 10 bits)
16. ;;Not used for PWM
0.001 608 Sec
Crystal
Desired Period Frequency
Prescaler = 1
Prescaler = 4
Prescaler = 16
8 000 000. ;;Enter crystal frequency (Hz)
16 000. ;;Enter desired PWM frequency
PR2
124.00
31.00
8.00
Duty Range
0 to 500
0 to 128
0 to 36
setup_timer_ 2(T2_DIV_1, 124, 1 );
set_pwm1_duty( 250L ); // square wave output - 50% duty
setup_ccp1( CCP_PWM );
56 SERVO 06.2010
formulas provided in the Microchip
documentation. The bottom box accepts
two values: a microcontroller oscillator
frequency and a target PWM frequency.
For this project, the oscillator frequency is
the internally generated 8 MHz. I tested
several computer fans — small and large
— at different frequencies and discovered
that a PWM frequency of 16 kHz worked
best for most of them.
At this frequency, there was minimal
humming or squealing. If you call up the
spreadsheet, you will see these values
entered. If your particular fan is noisy
(especially at lower speeds), experiment
with different frequencies. C statements
are generated that yield the highest
resolution or number of steps for the
duty range. In this case, all three values
for the Timer2 prescaler are allowed. If
you change the target frequency to 4
kHz, you will see that one of the
prescaler values is not allowed. For a
detailed explanation of this, see the
Microchip datasheets and application
notes listed in the References. The
spreadsheet is available on my website
at www.jgscraft.com/ledpwm or
the SERVO site at www.servo
magazine.com.
In the course of converting the
original PICBASIC program to C, I came