鍵控可調(diào)的PWM輸出
#include
#define uint unsigned int
#define uchar unsigned char
sbit k1=P3^2;
sbit k2=P3^3;
sbit bee=P1^2;
uchar PWM,num;
void delay(uchar z)
{
uchar i;
while(z--)
{
for(i=120;i>0;i--);
}
}
void spk()
{
bee=0;
delay(100);
bee=1;
delay(100);
}
void init_t0()
{
TMOD=0X01;
TH0=255;
TL0=256-PWM;
EA=1;
ET0=1;
TR0=1;
}
void key_scan()
{
while(k1==0)
{
if(PWM!=0XFF)
{
PWM++;
delay(10);
}
else
spk();
}
while(k2==0)
{
if(PWM!=0X01)
{
PWM--;
delay(10);
}
else
spk();
}
}
void main()
{
PWM=0X7f;
P2=0XFF;
init_t0();
while(1)
key_scan();
}
void t0() interrupt 1
{
num++;
TR0=0;
if(num%2==1)
{
P2=0;
TH0=0XFF;
TL0=256-PWM;
TR0=1;
}
if(num%2==0)
{
P2=0XFF;
TH0=0XFF;
TL0=PWM;
TR0=1;
}
}
評(píng)論