if(PWM)
PWM=0;
else
PWM=1;
为什么不直接写成PWM=!PWM;??
还有就是你把P1^0声明做PWM时,下面用"if(PWM)"如果P1.0脚外面接了负载约1K下地时,
可能会导致下次检测if(PWM)时PWM读出状态一直保持为0.
你要改频率的话,
#include"reg52.h"
#define uchar unsigned char
#define uint unsigned int
sbit PWM=P1^0;
sbit KEY1=P1^1;
sbit KEY2=P1^2;
uint mun;
void Delayms(unsigned int ms) //1mS@11.0592MHz
{unsigned char i, j;
while(ms--)
{
i = 11;
j = 190;
do
{
while (--j);
} while (--i);}
}
void main()
{
TMOD=0x01;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
EA=1;
ET0=1;
TR0=1;
while(1)
{
if(!KEY1)
{
Delayms(10);
if(!KEY1)
{
num++;
}
}
if(!KEY2)
{
Delayms(10);
if(!KEY2)
{
num--;
}
}
}
void T0_time(void) interrupt 1
{
TH0=(65536-mun)/256;
TL0=(65536-mun)%256;
//if(PWM)
//PWM=0;
//else
//PWM=1;
PWM=!PWM;
}
|