为什么我写的这个PWM控制LED灯亮灭的程序是错的呢?
时间:10-02
整理:3721RD
点击:
我检查了许多遍,都觉得应该没有错误,但是烧进板子,按键功能是正确的,就是灯不亮!
#include<reg51.h> //?
#include<intrins.h>
sbit K1=P3^2;
sbit K2=P3^3;
sbit BEEP=P3^6;
unsigned char PWM=0x7f;
void Beep();
void delayms(unsigned char ms);
void delay(unsigned char t);
//---------主函数--------------//
void mian()
{
P0=0xff;
TMOD=0x21;
TH0=0xfc; //T0产生1MS的周期;
TL0=0x18;
TH1=PWM; //T1产生脉宽
TL1=0;
EA=1;
ET0=1;
ET1=1;
TR0=1;
while(1)
{
do{
if(PWM!=0xff) //255
{PWM++; delayms(10);}
else Beep();
}
while(K1==0);
do{
if(PWM!=0x0)
{PWM--; delayms(10);}
else Beep();
}
while(K2==0);
}
}
//---------------------T0定时器中断------------
void timer0() interrupt 1
{
TR1=0;
TH0=0xfc;
TL0=0x18;
TH1=PWM;
TR1=1;
P0=0x00;
}
//------------------T1中断服务程序--------------
void timer1() interrupt 3
{
TR1=0;
P0=0xff;
}
//----------蜂鸣器--------------//
void Beep()
{
unsigned char i;
for(i=0;i<100;i++)
{
delay(100);
BEEP=!BEEP;
}
BEEP=1;
delayms(100);
}
//---------------延时程序---------------------
void delay(unsigned char t)
{
while(t--);
}
void delayms(unsigned char ms)
{
unsigned char i;
while(ms--)
{
for(i=0;i<120;i++);
}
}
#include<reg51.h> //?
#include<intrins.h>
sbit K1=P3^2;
sbit K2=P3^3;
sbit BEEP=P3^6;
unsigned char PWM=0x7f;
void Beep();
void delayms(unsigned char ms);
void delay(unsigned char t);
//---------主函数--------------//
void mian()
{
P0=0xff;
TMOD=0x21;
TH0=0xfc; //T0产生1MS的周期;
TL0=0x18;
TH1=PWM; //T1产生脉宽
TL1=0;
EA=1;
ET0=1;
ET1=1;
TR0=1;
while(1)
{
do{
if(PWM!=0xff) //255
{PWM++; delayms(10);}
else Beep();
}
while(K1==0);
do{
if(PWM!=0x0)
{PWM--; delayms(10);}
else Beep();
}
while(K2==0);
}
}
//---------------------T0定时器中断------------
void timer0() interrupt 1
{
TR1=0;
TH0=0xfc;
TL0=0x18;
TH1=PWM;
TR1=1;
P0=0x00;
}
//------------------T1中断服务程序--------------
void timer1() interrupt 3
{
TR1=0;
P0=0xff;
}
//----------蜂鸣器--------------//
void Beep()
{
unsigned char i;
for(i=0;i<100;i++)
{
delay(100);
BEEP=!BEEP;
}
BEEP=1;
delayms(100);
}
//---------------延时程序---------------------
void delay(unsigned char t)
{
while(t--);
}
void delayms(unsigned char ms)
{
unsigned char i;
while(ms--)
{
for(i=0;i<120;i++);
}
}
会不会是延时太短了呢
改了许多次,灯还是不亮,很费解
慢慢来咯,要不你把延时该长一点试下