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.

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. 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.

Downloads

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

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter