操控LED灯的亮度,经过调理电压的办法能够到达,但不抱负,并且可调的规模很小,电压稍高,还简单把LED焚毁,仅有可行的办法便是经过PWM(脉冲宽度调理)来操控其亮度.
以下程序是在单片机中操控LED…
//***********************
// PWM操控LED发光亮度
// 作者:周 勇
//***********************
#include
sbit speaker=P2^1;//界说喇叭操控引脚
sbit up=P1^5; //界说UP键
sbit down=P1^6; //界说DOWN键
static unsigned char pwm;
unsigned char t;
main ()
{
void delay (unsigned char ms);
void beep (void);
void alarm_delay (void);
TMOD=0X21;
ET0=1;
ET1=1;
EA=1;
TH0=0XFC; //守时器0,守时1ms
TL0=0X18;
TH1=0X9C; //守时器1,守时0.1ms
TL1=0X00;
TR0=1;
PT1=1; //界说守时器1优先中止
pwm=0x9c;
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 (void) interrupt 1 //界说守时器0
{
TH0=0XFC;
TL0=0X18;
TR1=1;
TH1=pwm;
P0=0X00;
}
void timer1(void) interrupt 3 //界说守时器1
{
P0=0XFF;
TR1=0;
}
void delay (unsigned char ms)//延时
{
unsigned char i=120;
while (ms–)
{
while (i–)
{}
}
}
void beep (void)//报警
{
unsigned char b;
for (b=40;b>=2;b–)
{speaker=!speaker;
alarm_delay ();
}
speaker=1;
}
void alarm_delay (void) //报警延时
{
unsigned char time;
for (time=0;time<120;time++)
{}
}