单片机控制电调源码:
欢迎交流:QQ,763466152
#include<reg51.h>
#include<stdio.h>
sbit p00=P0^0;
sbit p01=P0^1;
sbit p10=P1^0;
sbit p11=P1^1;
sbit p14=P1^4;
sbit p15=P1^5;
sbit p16=P1^6;
char n,m;
char shudu=0;
char code time0[]={0xff,0xf5,0xff,0xf2,0xff,0xf1};//使用计时器1、计时器2产生PWM信号波,约1.5ms脉宽,20ms周期。
char code time1[]={0xfc,0x7f,0xfc,0x88,0xfc,0x8b};//由于晶振频率低,产生的精度不高
char th1,tl1,th0,tl0;
void delay( char ms)
{int i;
while(ms--)
{for(i=1000;i>0;i--);
}
}
void init(void) //启动计时器0
{
TMOD=0x11;
TH0=0xff;
TL0=0xed;
th0=time0[0];
tl0=time0[1];
th1=time1[0];
tl1=time1[1];
EA=1;
ET0=1;
ET1=1;
TR0=1;
}
void Timer0() interrupt 1 using 1 //计时器0用于产生肪冲
{
TH0=th0;
TL0=tl0;
n++;
if(n==50)
{p00=0;
p10=1;
p14=1;
n=0;
TH1=th1;
TL1=tl1;
TR0=0;
TR1=1;
}
else
{p00=1;
p10=0;
}
}
void Timer1() interrupt 3 using 3 //计时器1用于产生空值
{ TH1=th1;
TL1=tl1;
m++;
if(m==20)
{p00=1;
p11=1;
p14=0;
m=0;
TH0=th0;
TL0=tl0;
TR0=1;
TR1=0;
}
else
{p00=0;
p11=0;
}
}
main()
{
init();
while(1)
{
if(p15==0)
{delay(5);
if(p15==0)
if(shudu>0)
{shudu--;
th0=time0[shudu*2];
tl0=time0[shudu*2+1];
th1=time1[shudu*2];
tl1=time1[shudu*2+1];
}
while(p15==0)p10=0;
p10=1;
delay(5);
}
if(p16==0)
{delay(5);
if(p16==0)
if(shudu<2)
{ shudu++;
th0=time0[shudu*2];
tl0=time0[shudu*2+1];
th1=time1[shudu*2];
tl1=time1[shudu*2+1];
}
while(p16==0)p11=0;
p11=1;
delay(5);
}
}
} |