Make LED dimmer using PIC16F877A microcontroller and MikroC
In this tutorial we will learn how to make a LED dimmer circuit use pic microcontroller. In this project I am use Pulse Width Modulation technique for LED brightness control. I am use here PIC16F877A and LM16x2 Lcd display.
You can watch the video or read the written tutorial below.
PIC16F877A Microcontroller has tow pin for PWM.
- RC2/CCP1 (pin 17)
- RC1/CCP2 (pic 16)
CCP means Capture / Compare / PWM . CCP module generate pule different duty cycle.
Each Capture / Compare / PWM(CCP) module contains a 16 bit register which can operate as a:
- 16 bit Capture register
- 16 bit Compare register
- PWM Master / Slave Duty Cycle register.
Circuit shematic:
Project Source Code:
// Lcd module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
// End lcd module connnections
#define SW1 PORTD.RD1 // Switch1 is connected RD1
#define SW2 PORTD.RD2 // Switch2 is connected RD2
int i = 0;
float j =0;
char text[2];
void main() {
TRISD = 0XFF; // PORTD is input
PORTD = 0X00; // Clear PORTD
PWM1_Init(1000); // Initialize PWM1 at 1KHz
PWM1_Start(); // Start PWM1
Lcd_Init(); // Initialize Lcd module
Lcd_Cmd(_LCD_CURSOR_OFF); // Lcd cursor off
Lcd_Cmd(_LCD_CLEAR); // Led clear
Lcd_Out(1,3,"DEVELOPED BY"); // Lcd show "DEVELOPED BY"
Lcd_Out(2,1,"MINA TECHNOLOGY"); // Lcd show "MINA TECHNOLOGY"
delay_ms(2000); // 2s delay
Lcd_Cmd(_LCD_CLEAR); // Lcd clear
Lcd_Out(1,3,"LED DIMMER"); // Lcd show "LED DIMMER"
Lcd_Out(2,1,"BRIGHT:"); // Lcd show "BRIGHT:"
Lcd_Out(2,16,"%"); // Lcd show "%"
while(1){ // Endless loop
if(SW1==1){
//while(SW1==1);
i = i+10;
delay_ms(250);
}
if(SW2==1){
//while(SW2==1);
i = i - 10;
delay_ms(250);
}
if(i==110) i =100; // limit value of i;
if(i==-10)i=0; // limit value of i;
intTostr(i,text); // convert integer to string
text[12] =0;
Lcd_Out(2,10,text);
j = (i*255)/100; // calculate the duty cycle for Pusle Width Modulation
PWM1_Set_Duty(j); // duty cycle= (percent of duty cycle*255)/100;
}
}
Download free source code
Post Comments