各位前辈们我现在要一个频率5-120HZ,脉宽20US-5000US的PWM的C程序 脉宽20US一调 频率1HZ一调

来源:百度知道 编辑:UC知道 时间:2024/06/30 22:00:20
我的是51单片机

#include <reg51.h>

#define CIRCLE 10

void pwmkey(void);

unsigned char count=1,last;

sbit led=P2^0;
sbit key=P3^6;

/*void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}*/

void main(void)
{
EA=1;
ET0=1;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
TR0=1;
count=1;
last=1;

while(1)
{
pwmkey();
}
}

void pwmkey(void)
{
char old=0 ,new=0;
while(1)
{
new=key;
if(new && !old)
last++;
if(last>CIRCLE)
last=1;
old=new;
}
}

void into(void) interrupt 1
{
count++;
if(count>CIRCLE)
{
count=1;
}
if(count<=last)
led=0;

else
led=1;

TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
}

试试这个改改参数 我试了比较大的可以达到25khz