PIC18F452 PWM Code and Proteus Simulation

Summary of PIC18F452 PWM Code and Proteus Simulation


This article explains generating PWM with PIC18F452 using CCP1 and CCP2 modules, showing C code (HI-TECH C18) and a Proteus simulation. It describes using a 10 MHz crystal with the PLL to run the PIC at 40 MHz (10 MIPS), configuring Timer2 for 10 kHz PWM, RC2 (pin 17) as CCP1 output and RC1 (pin 16) as CCP2 output, and a SetPWMDutyCycle(module, value) function where 0 = 0% and 255 = 100% duty cycle.

Parts used in the PIC18F452 PWM Project:

  • PIC18F452 microcontroller
  • 10 MHz crystal oscillator
  • Proteus simulation software (Proteus v7.7)
  • MPLAB IDE (MPLAB v8.76)
  • HI-TECH C18 compiler
  • Wiring/circuit connections as per Proteus diagram
  • Power supply for PIC18F452

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

Quick Solutions to Questions related to PIC18F452 PWM Project:

  • What PWM modules does PIC18F452 provide?
    PIC18F452 provides two built-in PWM modules: CCP1 and CCP2.
  • Which pins output CCP1 and CCP2 PWM signals?
    CCP1 PWM is output on RC2 (pin 17) and CCP2 PWM is output on RC1 (pin 16).
  • What crystal frequency is used and how fast does the PIC run?
    A 10 MHz crystal is used and the PLL boosts it so the PIC runs at 40 MHz, which yields 10 MIPS.
  • Which timer is used to generate 10 kHz PWM?
    Timer2 is used to generate the 10 kHz PWM signal.
  • How do you set the PWM duty cycle in the provided code?
    Use the function SetPWMDutyCycle(module, value) where module is 1 for CCP1 or 2 for CCP2, and value ranges 0 (0%) to 255 (100%).
  • How do you generate 50% duty cycle on CCP2?
    Call SetPWMDutyCycle(2, 127) to generate approximately 50% duty cycle on CCP2.
  • Which tools were used to compile and simulate the project?
    The code was compiled in MPLAB v8.76 with HI-TECH C and simulated in Proteus v7.7.
  • Where can I download the complete project code and simulation?
    The article provides a Downloads section with a link to download the complete 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