⑴ 51单片机蜂鸣器程序
#include<reg51.h>
#include<intrins.h>
#defineucharunsignedchar
ucharpwm,ctl=5;
sbitfmq=P1^0;
bitflag;
voiddelay(uchara)
{
uchari;
while(a--)for(i=0;i<120;i++);
}
voidt0isr()interrupt1
{
pwm++;
if(pwm<=ctl)P0=0xff;
elseP0=0x00;
if(pwm==100)
{
pwm=0;
ctl++;
}
if(ctl==100)flag=1;
}
main()
{
uchari,led;
TMOD=0x02;
TH0=236;
TL0=236;
ET0=1;
EA=1;
while(1)
{
led=0xfe;
for(i=0;i<8;i++)
{
P0=led;
led=led<<1;
delay(200);
}
led=0xff;
delay(200);
led=0x7f;
for(i=0;i<8;i++)
{
P0=led;
led=led>>1;
delay(200);
}
TR0=1;
flag=1;
while(flag);
TR0=0;
}
}