top of page

How to generate a PWM wave using mikroC Compiler for PIC

By Bernice Zuiya

Introduction

                What is a PWM ? Ans: PWM stands of pulse width modulation, it is basically a technique trough which the width of pulses varies. PWM is an important technique used in many system to perform various operations such as speed control of DC motor etc... The generation of PWM with mikroc compiler is simple since it contains a rich PWM's library.

PWM Wave form

Working

              

              A PIC MCU has two pins dedicated for PWM:

1. CPP1 at RC2

2. CPP2 at RC1

 

Video:

Code and Comment

#define pwm__ 255
unsigned int current_duty_cycle;
unsigned int i;
void main() {
 ADCON1 = 0X06;
 TRISC = 0X00;
 PWM1_Init(5000); // initialization of pulse width modulation with a frequency equal to 5000Hz or 5KHz
 Delay_ms(75);    // Delay of 75 millisecond
 current_duty_cycle=20;  // this value should be between 0 to 255 because this has a resolution 2^8
 PWM1_Start();   //  starting of mudulation
 PWM1_Set_Duty(current_duty_cycle);
 while(1){
    for(i = 0 ;i < pwm__ ; i ++){
      PWM1_Set_Duty(i);
      Delay_ms(5);  // delay of 5ms
    }
    for(i = pwm__ ; i > 0; i --){
      PWM1_Set_Duty(i);
      Delay_ms(5);  // delay of 5ms
    }
  }
}

Watch from Youtube Here

bottom of page