|
#include<reg52.h>
#include<math.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar star=65536;
// uchar ms_20=47104; /**/
uchar ms2_5=2304; /*2.5ms计数初值*/
uchar ms0_5=461; /*0.5ms计数初值*/
//uchar ms_25=63232; /**/
uchar ms_5=60928; /*5ms计数初值*/
uchar pwm[8]={1383,1383,1383,1383,1383,1383,1383,1383};
uchar j=0;
sbit IO1=P2^0;
sbit IO2=P2^1;
sbit IO3=P2^2;
sbit IO4=P2^4;
void Time0_Init()
{ TMOD |= 0X01;
TH0=ms_25/256;
TL0=ms_25%256;
ET0=1;
TR0=1;
EA=1;
}
void Time0_Int()interrupt 1
{ switch(j)
{ case 1 : IO1=1;TH0=(star-pwm[0])/256;TL0=(star-pwm[0])%256 ; break;
case 2 : IO1=0;TH0=(ms_5+pwm[0])/256;TL0=(ms_5+pwm[0])%256 ; break;
case 3 : IO2=1;TH0=(star-pwm[1])/256;TL0=(star-pwm[1])%256 ; break;
case 4 : IO2=0;TH0=(ms_5+pwm[1])/256;TL0=(ms_5+pwm[1])%256 ; break;
case 5 : IO3=1;TH0=(star-pwm[2])/256;TL0=(star-pwm[2])%256 ; break;
case 6 : IO3=0;TH0=(ms_5+pwm[2])/256;TL0=(ms_5+pwm[2])%256 ; break;
case 7 : IO4=1;TH0=(star-pwm[3])/256;TL0=(star-pwm[3])%256 ; break;
case 8 : IO4=0;TH0=(ms_5+pwm[3])/256;TL0=(ms_5+pwm[3])%256 ; break;
default : TH0=0xff; TL0=0xff; j=0;
}
j++ ;
}
void scanf(uint i,uchar val) /*选择控制哪一路的舵机和转动角度*/
{ uchar a;
a=(val+46)*10;
if(a>=ms2_5)
a=ms2_5;
if(a<=ms0_5)
a=ms0_5;
pwm[i]=a;
}
|
|