Generating PWM with PIC Microcontroller using CCP Module




PWM is a technique used to generate analog output signal using digital signals. It is commonly used to control average power delivered to a load, motor speed control, generating analog voltage levels and for generating analog waveforms.

CCP Modules are available with a number of PIC Microcontrollers. CCP stands for Capture/Compare/PWM. Using PWM module is far more easier and cost effective than using extra chips for PWM generation.  MikroC Pro for PIC Microcontroller provide built-in library for PWM which makes our task very simple.

Generating PWM with PIC Microcontroller using CCP ModuleMikroC Functions

  • PWM1_Init(constant long frequency) : This function initializes the PWM module with duty ratio 0. Frequency parameter is the desired frequency in Hz. It should be a numeric constant, should not be a variable.
  • PWM1_Set_Duty(unsigned short duty_ratio) : This function is used to set the duty cycle of the PWM. The parameter duty_ratio takes values from 0 to 255, ie 0 means 0% , 127 means 50% and 255 means 100% duty cycle. The PWM1_Init() routine must be called before using this.
  • PWM1_Start() : This function starts the PWM output. PWM1_Init() must be called before calling this routine,
  • PWM1_Stop() : This function stops the PWM output. PWM1_Init() must be called before calling this routine. PWM1_Start() should be called before calling this function otherwise calling this function will not have any effect as PWM module is not running.

Note 1 :  For microcontrollers with more than one CCP module, to use the desired CCP module for PWM generation simply change the number “1”  in the prototype with desired module number. eg: PWM2_Init(5000), PWM2_Start().

Note 2 : All PWM modules in a PIC Microcontroller uses Timer 2 for its operation, so you cannot set different frequencies for different PWM modules.

READ  Interfacing Servo Motor with PIC Microcontroller – MPLAB XC8

In this tutorial we are using PIC 16F877A for demonstrating PWM generation using CCP module. PIC 16F877A contains two CCP modules.

Circuit Diagram – Using internal PWM Module of PIC

In the below circuit four switches are provided for controlling the Duty Ratio of PWM generated by two CCP modules of the PIC Microcontroller.

  • Switch 1 : To increase the Duty Ratio of PWM produced by CCP1
  • Switch 2 : To decrease the Duty Ratio of PWM produced by CCP1
  • Switch 3 : To increase the Duty Ratio of PWM produced by CCP2
  • Switch 4 : To decrease the Duty Ratio of PWM produced by CCP2

 

For more detail: Generating PWM with PIC Microcontroller using CCP Module




Current Project / Post can also be found using:

  • projects based on pulse width modulation
  • PWM project

Leave a Comment

*
= 5 + 8

Read previous post:
Fastest 18-Bit SAR ADC unveiled
Fastest 18-Bit SAR ADC unveiled

The AD7960 is an 18-bit, 5 MSPS charge redistribution successive approximation (SAR), analog-to-digital converter (ADC). The SAR architecture allows unmatched...

Close
Scroll to top