PWM控制LED燈的亮度(C 語言)
以下程序是在單片機中控制LED...
本文引用地址:http://cafeforensic.com/article/201611/315553.htm
//***********************
// PWM控制LED發(fā)光亮度
// 作者:周 勇
//***********************
#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優(yōu)先中斷
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++)
{}
}
評論