Introduction:
A PIC16F628A has an in-built Capture/Compare/PWM (CCP) module for which the I/O pin is served by RB.3 (Pin No. 9). In this experiment we are going to use the CCP as a PWM to control the power to a LED. PWM stands for the Pulse Width Modulation where the width of a digital waveform is varied to control the power delivered to a load. The underlying principle in the whole process is that the average power delivered is directly proportional to the modulation duty cycle. The term duty cycle describes the proportion of on time to the regular interval or period of time; a low duty cycle corresponds to low power, because the power is off for most of the time. Duty cycle is expressed in percent, 100% being fully on.
Experimental Setup:
In this experiment, we are going to have 11 different intensities (including complete turn OFF) of a LED by varying the duty cycle. We will connect a LED to RB.3, and two Push Buttons to RB.0 and RB.1. The two buttons are for Increment/Decrement the intensity of the LED.
Software:
Experimental Output Video:
A PIC16F628A has an in-built Capture/Compare/PWM (CCP) module for which the I/O pin is served by RB.3 (Pin No. 9). In this experiment we are going to use the CCP as a PWM to control the power to a LED. PWM stands for the Pulse Width Modulation where the width of a digital waveform is varied to control the power delivered to a load. The underlying principle in the whole process is that the average power delivered is directly proportional to the modulation duty cycle. The term duty cycle describes the proportion of on time to the regular interval or period of time; a low duty cycle corresponds to low power, because the power is off for most of the time. Duty cycle is expressed in percent, 100% being fully on.
Image Source: http://www.micromouseinfo.com/introduction/images/intro_hardware/PWMod.gif
The mikroC has an in-built library functions for PWM hardware module. Click here for details.Experimental Setup:
In this experiment, we are going to have 11 different intensities (including complete turn OFF) of a LED by varying the duty cycle. We will connect a LED to RB.3, and two Push Buttons to RB.0 and RB.1. The two buttons are for Increment/Decrement the intensity of the LED.
Software:
/*
Project Name: Use of Timer 0 and Interrupt
* Copyright:
(c) Rajendra Bhatt, 2009.
* Description:
Use of CCP module as a Pulse Width Modulation
* Test configuration:
MCU: PIC16F628A
Oscillator: XT, 4.0 MHz
*/
unsigned short new_DC, current_DC;
void main() {
PORTB = 0; // Initial state of port B
TRISB = 3; // RB0, RB1 input, RB3 (PWM1) output
PWM1_Init(5000); // PWM module initialization (5KHz)
new_DC = 0; // Initial value of variable Duty Cycle
current_DC = 0;
PWM1_Start(); // Start PWM1 module with Zero DC
PWM1_Set_Duty(current_DC);
while (1) {
if (Button(&PORTB, 0,1,0)) { // If the button connected to RB0 is pressed
if (new_DC < 250) // Don't go above 250
new_DC = new_DC + 25 ; // increment Duty Cycle by 25
}
if (Button(&PORTB, 1,1,0)) { // If the button connected to RB1 is pressed
if (new_DC !=0) // Don't go below 0
new_DC= new_DC - 25 ; // decrement Duty Cycle by 25
}
if (current_DC != new_DC) {
current_DC = new_DC ;
PWM1_Set_Duty(current_DC); // Change the current DC to new value
}
Delay_ms(150);
}
}
Experimental Output Video:
Do you have a schematic for your test board?
ReplyDeletehttp://pic16f628a.blogspot.com/2009/09/pic16f628a-development-board-part-1.html
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis is awesome, I never know controlling PWM in PIC would be this easy.
ReplyDeleteprogram that will control two LEDs using PWM. Every time the external interrupt button is pushed the program reads the analogue input and uses the value to set up the duty cycle of one of PWM LEDs (hint--> you must use A/D module). Thus, the external interrupt toggles between the two PWM LEDs. The program also uses other two LEDs for signalling which PWM LED is the active output.
ReplyDeletedo u have a way to get through this ? like gv me hints and can u gv me ideas how to use the external interrupt to toggle between the two leds
please
The PIC I am using is PIC16F628A, and it doesn't have A/D converter. If you want to implement more than one PWM with PIC16F628A, you can use software PWMs. Regarding the use of interrupt, I will write an article on that very soon.
ReplyDeleteHi. Can you put only the led brightness controling schematic? I want to build a led brightness controller with 2 buttons. 1 to increase the brightness and 1 to deacrease. Thanks
ReplyDeleteI have posted a similar tutorial here, and it has circuit diagram too.
ReplyDeletehttp://embedded-lab.com/blog/?p=1443
This comment has been removed by the author.
ReplyDeletethx very much. I have one question. Can you modify it to a pic 12f675.
ReplyDeleteHi, Can you help me? I have test it on Proteus 7.5SP3, but It's not work!
ReplyDeleteThanh,
ReplyDeleteI suggest you to read my new post here:
http://embedded-lab.com/blog/?p=1443
Thanks
Very useful tutorial, thank you!
ReplyDelete