PWM控制LED灯的亮度(C 语言)
以下程序是在单片机中控制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++)
{}
}
PWM控制LED 相关文章:
- 用PWM控制LED的亮度的程序(11-13)
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)