5EXP:PWM BASED CONTROL FOR BRIGHTNESS OF LED USING 8051
#include <reg52.h>
sbit LED = P1^0;
sbit BTN_DIM = P3^2;
sbit BTN_BRIGHT = P3^3;
unsigned char duty = 80; // Start bright
unsigned char pwm_count = 0;
void timer0_ISR(void) interrupt 1
{
TH0 = 0xFF;
TL0 = 0x9C; // ~100us interrupt
pwm_count++;
if(pwm_count >= 100)
pwm_count = 0;
if(pwm_count < duty)
LED = 0; // ON
else
LED = 1; // OFF
}
void delay()
{
unsigned int i, j;
for(i=0; i<200; i++)
for(j=0; j<200; j++);
}
void main()
{
TMOD = 0x01; // Timer0 Mode 1
TH0 = 0xFF;
TL0 = 0x9C;
ET0 = 1; // Enable Timer0 interrupt
EA = 1; // Enable Global interrupt
TR0 = 1; // Start Timer0
while(1)
{
if(BTN_DIM == 0)
{
if(duty > 0)
duty--;
delay();
}
if(BTN_BRIGHT == 0)
{
if(duty < 100)
duty++;
delay();
}
}
}
//
Simulation in Keil:
Create a new project in Keil and select AT89C51/AT89S52.
Write and compile the above C program.
Generate the HEX file and upload it to Proteus.
Connect LED to P1.0 and push buttons to P3.2 (INT0) and P3.3 (INT1).
Run the simulation and test brightness control.
Hardware Implementation:
Connect the components on a breadboard as per the circuit diagram.
Burn the HEX file into the 8051 microcontroller.
Power the circuit and observe LED brightness.
Press the INC button (P3.2) to increase brightness.
Press the DEC button (P3.3) to decrease brightness.