8.10. Circuit diagram of the project. In this project, the 40
kHz PW1 signal is output from pin OC1 (or RD0, pin 72) of the microcontroller. If you are using the LV-32MX V6 development board, then the following jumper must be set as follows: DIP switch SW20, jumper 8, set to ON 8.2.3 Project PDL Before developing the PDL for this project, it is important to understand how the project works. The PWM module inside the PIC32 microcontroller uses a timer to control the signal frequency and duty cycle. The period of the generated PWM signal is given by the following: PWM Period = [(PR + 1) × TPB × (Timer prescaler value)] where PR is the value loaded into the period register, and TPB is the clock period of the peripheral clock. Assuming a peripheral clock frequency of 80 MHz, the peripheral clock period is given by the following: TPB = 1/(80 × 10−6) = 0.0125 × 10−6 s We are normally interested in finding the value to be loaded into the period register. Assuming that we are using Timer 2 (Timer 1 cannot be used in PWM mode), the value loaded into this register is calculated as follows: Also, PWM frequency = 1/[PWM Period] The PWM duty cycle is specified by writing to the OCxRS register. The maximum PWM duty cycle resolution is calculated using the following formula: where, FPB is the peripheral clock frequency, and FPWM is the clock frequency of the PWM signal to be generated. The specifications of this project are summarized below: • Frequency of the PWM signal = 40 MHz (or, period = 0.025 × 10−3 s) • Peripheral bus clock frequency = 80 MHz (or 0.0125 × 10−6 s) • Timer to be used = Timer 2 • Timer 2 prescaler value = 1 • Duty cycle = 50%. The value to be loaded into period register PR2 is calculated as follows: The maximum PWM resolution is calculated as follows: The steps for configuring the PWM module for the above specifications are given below (notice that since we are using output pin OC1, the register OCxCON is the register OC1CON): • Calculate the PWM period (0.025 × 10−3) • Calculate the PWM duty cycle (50%) • Use Timer 2 in 16-bit mode • Clear register OC1CON, bit 5 (OC32) for 16-bit operation • Load PR2 with decimal 1999 • Load OC1RS low 16-bits with duty cycle (50% duty cycle corresponds to 1999/2 = decimal 1000) • No interrupts required • Set OCM bits of OC1CON to six to enable PWM mode of operation • Select Timer 2 as the timer source • Clear TCKPS bits of T2CON to set Timer 2 prescal