pwm_puls width modulation chapteres.pptx

ssuser3cbb4c 11 views 14 slides Jul 29, 2024
Slide 1
Slide 1 of 14
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14

About This Presentation

Described the principal of pwm


Slide Content

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 ); } }

void main() { INTCON = 0; TRISA = 0x04; TRISC = 0; PORTC = 0; PWM1_Init(10000); Lcd_Init (); Lcd_Cmd (_LCD_CURSOR_OFF); Lcd_Cmd (_LCD_CLEAR); text = "ADC PWM"; Lcd_Out (1,1,text); text = " Cycle_Duty :"; Lcd_Out (2,1,text); ADCON1 = 0x82; Delay_ms (2000); PWM1_Start(); while (1) { adc_rd = ADC_Read (2); Delay_ms (5); tlong = (long) adc_rd * 255; tlong = tlong / 1023; tlong = ( tlong * 100)/255; //convert into percentage ch = tlong /100; Lcd_Chr (2,12,48+ch); ch = ( tlong / 10) % 10; Lcd_Chr_CP (48+ch); ch = tlong % 10; Lcd_Chr_CP (48+ch); division = adc_rd /4; PWM1_Set_Duty(division); Delay_ms (100); } } sbit LCD_RS at RB2_bit; sbit LCD_EN at RB3_bit; sbit LCD_D4 at RB4_bit; sbit LCD_D5 at RB5_bit; sbit LCD_D6 at RB6_bit; sbit LCD_D7 at RB7_bit; sbit LCD_RS_Direction at TRISB2_bit; sbit LCD_EN_Direction at TRISB3_bit; sbit LCD_D4_Direction at TRISB4_bit; sbit LCD_D5_Direction at TRISB5_bit; sbit LCD_D6_Direction at TRISB6_bit; sbit LCD_D7_Direction at TRISB7_bit; unsigned char ch ; unsigned int adc_rd ; unsigned int division; char *text; long tlong ;

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