PWM Using Pic Microcontroller

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

PWM using Pic Microcontroller

PWM using Pic Microcontroller

• with Examples, In this tutorial, you will learn


to generate a PWM signal with the help of PIC
microcontroller (PIC16F877A).

• We will provide pulse width modulation


examples with MikroC compiler.
What is pulse width modulation?
• PWM (Pulse Width Modulation) is a powerful technique used to control
analog circuits with the digital output from the microcontroller. There
are two major components of a PWM signal that defines its behavior;
PWM duty cycle, time period and frequency.
• PWM Duty cycle
It describes the ‘on-time’ of a signal. An on-time is the duration of
a signal for which the signal stays HIGH. Duty cycle is measured in
percentage. For example, if a digital signal is on for half of the time
duration and off for the other half, the digital signal is said to have a
duty cycle of 50%. Similarly, if a signal stays high for a longer period
of time than it stays low, the signal will have a duty cycle greater
than 50%.
• Time Period or Frequency 
The frequency determines the amount of time taken by PWM to
complete one cycle. For example, a frequency of 1000Hz would mean
1000 cycles completed per second. Before using PWM module of Pic
microcontroller, we should define the frequency/timer period of the
signal. 
PWM using PIC Microcontroller
• Now you know the basics of pulse width modulation and you also know
its related terminology. Now let’s start understanding how to generate
different frequency and duty cycle digital signals using PIC16F877A
microcontroller.

• Although, we use PIC16F877A in this post, but you can easily apply the
same concepts and examples to other PIC microcontrollers also. As
mentioned earlier, we will discuss an example with two compilers,
namely, MPLAB XC8 and MikroC Pro. First let’s begin with MikroC for PIC
compiler.

• PIC16F877A PWM Examples using MikroC

• To generate PWM with the help of a PIC16F877A microcontroller, built-in


CCP modules are used. CCP stands for Capture/Compare/PWM. There
are two CCP modules present in PIC16F877A; CCP1 and CCP2 at pins RC2
and RC1 respectively.
MikroC for PIC PWM Library
• As mentioned earlier, MikroC provides a built-in library for
pulse width modulation module of pic microcontroller. This
library is generic and can be used with all PIC16F, PIC18 series
microcontrollers. These are the four functions that are used to
generate PWM, set frequency and change duty cycle. 

• PWMx_Init(long frequency) function used to set/declare


frequency of signal. The input parameter to this function is
required frequency. Here x denotes the number of CCP
module. Because some pic microcontrollers come in more
than one CCP modules. For instance, PIC16F877A
microcontroller has two CCP modules CCP1 and CCP2. 
• For example, we want to use CCP1 module
andwant set frequency of 5000Hz or 5KHz. we
initialize this routine like this: 
• PWM1_Init(5000); //initialize CCP1 module
with frequency 5kHz.
PWM2_Init(5000); //initialize CCP2 module
with frequency 5kHz.
• PWMx_Set_Duty(unsigned int duty_cycle)
• PWMx_Set_Duty() function defines the duty cycle of PWM.
• The input parameter to this function is a duty cycle. The
duty cycle value can be any number between 0-255. Here
‘0’ means 0%, 127 means 50% and 255 means 100% duty
cycle. But we can adjust the duty cycle to any percentage
with this formula: 
• duty_cycle = (pecentage x 255 ) /100
• PWMx_Start(): This routine starts the PWM module and
after calling this function, you can see output on CCP1 and
CCP2 pins.
•  PWMx_Stop(): If you can this function, it will stop the pic
microcontroller from outputting signal on CCP1 and CCP2
pins. 
Generate Fix Duty Cycle PWM using
PIC16F877A.
// LCD Module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D4 at RB4_bit;
// End LCD module connections

// LCD Pin direction


sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;

sbit LCD_D7_Direction at TRISB7_bit;


sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB4_bit;
// End of LCD Pin direction
• void main()
{

TRISC = 0x00; // PORTC as output


PWM1_Init(5000); // Initialize PWM1
PWM1_Start(); // start PWM1

Lcd_Init(); // Initialize LCD


Lcd_Cmd(_LCD_CLEAR); // Clear Display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor Off

while (1) // endless loop


{
Lcd_Out(1,1,"25% Duty Cycle");// Write “25% Duty Cycle” in the first row
PWM1_Set_Duty(63); //Change the duty cycle
Delay_ms(1000);

Lcd_Out(1,1,"50% Duty Cycle");// Write "50% Duty Cycle" in the first row
PWM1_Set_Duty(127); //Change the duty cycle
Delay_ms(1000);

Lcd_Out(1,1,"75% Duty Cycle");// Write “"75% Duty Cycle" in the first row
PWM1_Set_Duty(190); //Change the duty cycle
Delay_ms(1000);

Lcd_Out(1,1,"100% Duty Cycle");// Write "100% Duty Cycle"in the first row
PWM1_Set_Duty(255); //Change the duty cycle
Delay_ms(1000);
}
}
sbit LCD_RS at RB2_bit; void main()
sbit LCD_EN at RB3_bit; while (1)
{
sbit LCD_D4 at RB4_bit; {
adc_rd = ADC_Read(2);
sbit LCD_D5 at RB5_bit; INTCON = 0; Delay_ms(5);
sbit LCD_D6 at RB6_bit; TRISA = 0x04; tlong = (long)adc_rd * 255;
sbit LCD_D7 at RB7_bit; TRISC = 0; tlong = tlong / 1023;
sbit LCD_RS_Direction at TRISB2_bit; PORTC = 0; tlong = (tlong * 100)/255;
sbit LCD_EN_Direction at TRISB3_bit; PWM1_Init(10000); //convert into percentage
sbit LCD_D4_Direction at TRISB4_bit; Lcd_Init(); ch = tlong/100;
sbit LCD_D5_Direction at TRISB5_bit; Lcd_Cmd(_LCD_CURSOR_OFF); Lcd_Chr(2,12,48+ch);
sbit LCD_D6_Direction at TRISB6_bit; Lcd_Cmd(_LCD_CLEAR); ch = (tlong / 10) % 10;
sbit LCD_D7_Direction at TRISB7_bit; text = "ADC PWM"; Lcd_Chr_CP(48+ch);
Lcd_Out(1,1,text); ch = tlong % 10;
unsigned char ch; text = "Cycle_Duty:"; Lcd_Chr_CP(48+ch);
unsigned int adc_rd; Lcd_Out(2,1,text); division = adc_rd/4;
unsigned int division; ADCON1 = 0x82; PWM1_Set_Duty(division);
char *text; Delay_ms(100);
Delay_ms(2000);
long tlong; }
PWM1_Start();
}
PWM Applications
PWM can be used in a variety of applications.
• DC Motor Speed Control 
• It can be used to control a servo motor as well
as other motors requiring speed control.
• AC Dimmer Voltage Control.
• PWM is also used to control the average power
delivered to a load.

You might also like