PWM Using Pic Microcontroller
PWM Using Pic Microcontroller
PWM Using Pic 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.
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.