English
Language : 

PIC32MX440F256H-80I Datasheet, PDF (382/646 Pages) Microchip Technology – 64/100-Pin General Purpose and USB 32-Bit Flash Microcontrollers
PIC32MX3XX/4XX
EXAMPLE 16-2: PWM PERIOD AND DUTY CYCLE CALCULATION
Desired PWM frequency is 52.08 kHz,
FPB = 10 MHz
Timer 2 prescale setting: 1:1
1/52.08 kHz
19.20 μs
PR2
= (PR2 + 1) • FBP • (Timer2 prescale value)
= (PR2 + 1) • 0.1 μs • (1)
= 191
Find the maximum resolution of the duty cycle that can be used with a 52.08 kHz PWM frequency
and a 10 MHz peripheral bus clock rate.
1/52.08 kHz = 2PWM RESOLUTION • 1/10 MHz • 1
19.20 μs
= 2PWM RESOLUTION • 100 nsec • 1
192
= 2PWM RESOLUTION
log10(192) = (PWM Resolution) • log10(2)
PWM Resolution= 7.6 bits
Note:
If the PR value exceeds 16 bits the module must be used in 32-bit mode to maintain the calculated PWM
resolution. If reduced resolution is acceptable the timer prescaler may be increased and the calculation
repeated until the result is a 16-bit value. Increasing the timer prescaler to allow operation in 16-bit mode
may result in reduced PWM resolution.
EXAMPLE 16-3: PWM MODE PULSE SETUP AND INTERRUPT SERVICING (32-BIT MODE)
// The following code example will set the Output Compare 1 module
// for PWM mode with FAULT pin disabled, a 50% duty cycle and a
// PWM frequency of 52.08 kHz at FPB = 40 MHz. Timer2 is selected as
// the clock for the PWM time base and Output Compare 1 interrupts
// are enabled.
OC1CON = 0x0000;
OC1R = 0x00600000;
OC1RS = 0x00600000;
OC1CON = 0x0006;
PR2 = 0x00600000;
// Turn off OC1 while doing setup.
// Initialize primary Compare Register
// Initialize secondary Compare Register
// Configure for PWM mode, Fault pin Disabled
// Set period
IFS0 &= ~0x00000080;
IEC0 |= 0x00000080;
IPC1 |= 0x001C0000;
IPC1 |= 0x00000003;
// configure int
// Clear the OC1 interrupt flag
// Enable OC1 interrupt
// Set OC1 interrupt priority to 7,
// the highest level
// Set subpriority to 3, maximum
T2CON |= 0x8000;
OC1CON |= 0x8000;
// Enable timer2
// turn on OC1 module
// Example code for Output Compare 1 ISR:
void__ISR (_OUTPUT_COMPARE_1_VECTOR, ipl4) OC1_IntHandler(void)
{
// insert user code here
IFS0CLR = 0x00000080; // Clear the interrupt flag
}
DS61143E-page 380
Preliminary
© 2008 Microchip Technology Inc.