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 cycledescribes 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.
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 cycledescribes 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:
No comments:
Post a Comment