PIC18F452 PWM Code and Proteus Simulation

PIC18F452 PWM

This post provides the PWM code (using C language) and Proteus simulation for PIC18F452 micro-controller CCP1 and CCP2 modules ( written using MPLAB and HI-TECH C18 compiler ). There are many uses for PWM signals, e-g you can control dc motor speed using PWM.

PIC18F452 PWM

CCP is the name for the built in PWM module in PIC18F452. There are 2 built in PWM modules available, namely CCP1 and CCP2. You can download the complete project code and proteus simulation from the ‘Downloads‘ section at the bottom of this page. The following diagram (made in Proteus) shows the circuit required.

A crystal of 10MHz is used here with PIC18F452. This crystal frequency is fed into PLL of PIC18F452, which boosts it to 40MHz. As we know that any PIC micro controller has an architecture which executes an instruction in 4 CPU cycles, hence this 10Mhz crystal + PLL makes this PIC run at 10MIPS (Million of instructions per second). The code for PWM initialization is shown below.

This function initializes the PWM modules CCP1 and CCP2 to generate PWM signal at 10KHz using ‘Timer 2‘. RC2 ( Pin 17 ) gives the output of the CCP1 module. And RC1 ( Pin 16 ) gives the output of the  CCP2 module. ‘SetPWMDutyCycle()‘ is the function which is used to set the duty cycle value for the PWM of CCP1 or CCP2. This function has 2 input arguements. First one is the CCP module number.

PIC18F452 PWM schematic

Read more: PIC18F452 PWM Code and Proteus Simulation

Leave a Comment

Your email address will not be published. Required fields are marked *