PIC18F452 PWM Code and Proteus Simulation

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.


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. For example, write 1 here to select the CCP1 module and write 2 here to select the CCP2 module. And the second input arguement is the required value of the duty cycle of PWM. Write 0 here for 0% duty cycle and write 255 here for 100% duty cycle. For example, to generate a PWM with 50% duty cycle on CCP2 ( i-e pin 16 of PIC18F452 ) write ‘SetPWMDutyCycle( 2, 127 )‘ in your code.

PIC18F452 PWM schematic

The code for the main function is shown below.


The code was compiled in MPLAB v8.76 with HI-TECH C and simulation was made in Proteus v7.7.
To download code and proteus simulation click here.


For more detail: PIC18F452 PWM Code and Proteus Simulation

Current Project / Post can also be found using:

  • code for gps interfacing with pic18f452 in mplab ide with proteus simulation
  • gps simulation in proteus code in mplab ide
  • pic 18 microcontroller pwm control duty cycle proteus

Leave a Comment

= 4 + 0

Read previous post:
PIC12F675 LED blinking
PIC12F675 LED blinking code and Proteus simulation

This post provides the code to make an LED blink using PIC12F675 microcontroller. This code is written in C language...

Scroll to top