PWM control LED lamp brightness program (C language)

Controlling the brightness of an LED can be done by adjusting the voltage, but this method is not ideal due to its limited adjustability. Even a slight increase in voltage can easily damage the LED. The most effective and widely used approach is to control the brightness using PWM (Pulse Width Modulation), which allows for smooth and efficient brightness adjustment without risking the LED.

The following code demonstrates how to control an LED's brightness using PWM on a microcontroller.

//**********************************

// PWM Control for LED Brightness

// Author: Yong

//**********************************

#include <reg52.h>

sbit speaker = P2^1; // Define the speaker pin

sbit up = P1^5; // Define the UP button

sbit down = P1^6; // Define the DOWN button

static unsigned char pwm;

unsigned char t;

void main()

{

void delay(unsigned char ms);

void beep(void);

void alarm_delay(void);

TMOD = 0x21; // Timer 0 in mode 1, Timer 1 in mode 1

ET0 = 1; // Enable Timer 0 interrupt

ET1 = 1; // Enable Timer 1 interrupt

EA = 1; // Global interrupt enable

TH0 = 0xFC; // Timer 0, 1ms timing

TL0 = 0x18;

TH1 = 0x9C; // Timer 1, 0.1ms timing

TL1 = 0x00;

TR0 = 1; // Start Timer 0

PT1 = 1; // Set Timer 1 as high priority

pwm = 0x9c; // Initial PWM value

while(1)

{

do

{

pwm++;

delay(10);

if(pwm == 0xff) beep();

} while(pwm != 0xff);

do

{

pwm--;

delay(10);

if(pwm == 0x01) beep();

} while(pwm != 0x01);

}

}

void timer0() interrupt 1 // Timer 0 interrupt

{

TH0 = 0xFC;

TL0 = 0x18;

TR1 = 1;

TH1 = pwm;

P0 = 0x00;

}

void timer1() interrupt 3 // Timer 1 interrupt

{

P0 = 0xFF;

TR1 = 0;

}

void delay(unsigned char ms) // Delay function

{

unsigned char i = 120;

while(ms--)

{

while(i--);

}

}

void beep(void) // Beep function

{

unsigned char b;

for(b=40; b>2; b--)

{

speaker = ~speaker;

alarm_delay();

}

speaker = 1;

}

void alarm_delay(void) // Alarm delay

{

unsigned char time;

for(time=0; time<120; time++);

}

Double Concave Cylindrical Lens

Double concave Cylindrical Lens has two outward surfaces.

Cylindrical lenses are typically applied to focus incoming light to a line, or to change the aspect ratio of an image. they are used for the optics, lasers, medical science, electronics, tele-communication and others.

Realpoo Optics can offer cylindrical Lenses include convex cylindrical lens, concave cylindrical lens, meniscus cylindrical lens, and doublet achromatic cylindrical lens ect.


Specifications:

Material : N-BK7, N-SF1 , Fused Silica , N-SF10 etc
Diameter : 2mm ~ 300mm
Focal Legnth Tolerance :+/-1%
Diameter Tolerance :+0.0/-0.1mm
Thickness Tolerance :+/-0.1mm
Surface Quality :40/20 or better
Centration :<3 arc minutes
Clear Aperture :>90%
Bevel : Protective bevel as needed
Coating :Upon your request

Double Concave Cylindrical Lens,Fused Silica Concave Cylindrical Lens,Double Concave Cylindrical Lenses,Cylindrical Lense

Changchun Realpoo Photoelectric Co., Ltd. , https://www.optics-realpoo.com