www.pudn.com > jian.rar > duoji.c, change:2014-07-26,size:4915b


#include<reg52.h> 
 
sbit PWM0 = P1^0; 
sbit PWM1 = P1^1; 
sbit PWM2 = P1^2; 
sbit PWM3 = P1^3; 
sbit PWM4 = P1^4; 
sbit PWM5 = P1^5; 
sbit PWM6 = P1^6; 
sbit PWM7 = P1^7; 
 
sbit ADD = P3^6; 
sbit SUB = P3^7; 
 
#define uchar unsigned char 
#define uint unsigned int 
 
uint t_up0 = 1500;		//舵机PWM高电平时间 1000~2000表示1ms到2ms 
uint t_up1 = 1500; 
uint t_up2 = 1500; 
uint t_up3 = 1500; 
uint t_up4 = 1500; 
uint t_up5 = 1500; 
uint t_up6 = 1500; 
uint t_up7 = 1500; 
 
uint t0_h; 
uint t0_l; 
 
 
void delayms(uint ms) 
{	 
	unsigned char a,b,c; 
	while(ms--) 
	{		 
    for(c=1;c>0;c--) 
        for(b=142;b>0;b--) 
            for(a=2;a>0;a--); 
	} 
} 
void timer_init() 
{ 
	EA = 1; 
	ET0 = 1; 
	PT0 = 1; 
	TMOD = 0x11; 
	TH0 = (65536 - t_up0)/256; 
	TL0 = (65536 - t_up0)%256; 
} 
 
uchar t0_flag = 0; 
uint num_max = 65535;	//直接用65535 - t_up 不用变量 - t_up 时,误差较大,原因暂时不明 【注:65536不能存到uint类型变量中】 
uint t_change = 63036;//换路周期2.5ms    8路 
 
uchar error0 = 45; 
uchar error1 = 45; 
uchar error2 = 52; 
uchar error3 = 52; 
uchar error4 = 57; 
uchar error5 = 57; 
uchar error6 = 63; 
uchar error7 = 63; 
uchar error8 = 70; 
uchar error9 = 70; 
uchar error10 = 76; 
uchar error11 = 76; 
uchar error12 = 82; 
uchar error13 = 82; 
uchar error14 = 88; 
uchar error15 = 88; 
 
void timer0() interrupt 1 
{ 
	 
	if(t0_flag == 0) 
	{ 
		PWM0 = 1; 
		TH0 = (num_max - t_up0 + error0)/256; //+?是为了抵消执行语句花的时间 
		TL0 = (num_max - t_up0 + error0)%256; 
		 
		t0_flag = 1; 
	} 
	else if(t0_flag == 1) 
	{ 
		PWM0 = 0; 
		TH0 = (t_change + t_up0 +error1)/256; 
		TL0 = (t_change + t_up0 +error1)%256; 
		 
		t0_flag = 2; 
	} 
	else if(t0_flag == 2) 
	{ 
		PWM1 = 1; 
		TH0 = (num_max - t_up1 + error2)/256; //+?是为了抵消执行语句花的时间 
		TL0 = (num_max - t_up1 + error2)%256; 
		 
		t0_flag = 3; 
	} 
	else if(t0_flag == 3) 
	{ 
		PWM1 = 0; 
		TH0 = (t_change + t_up1 +error3)/256; 
		TL0 = (t_change + t_up1 +error3)%256; 
		 
		t0_flag = 4; 
	} 
	else if(t0_flag == 4) 
	{ 
		PWM2 = 1; 
		TH0 = (num_max - t_up2 + error4)/256; //+?是为了抵消执行语句花的时间 
		TL0 = (num_max - t_up2 + error4)%256; 
		 
		t0_flag = 5; 
	} 
	else if(t0_flag == 5) 
	{ 
		PWM2 = 0; 
		TH0 = (t_change + t_up2 +error5)/256; 
		TL0 = (t_change + t_up2 +error5)%256; 
		 
		t0_flag = 6; 
	} 
	else if(t0_flag == 6) 
	{ 
		PWM3 = 1; 
		TH0 = (num_max - t_up3 + error6)/256; //+?是为了抵消执行语句花的时间 
		TL0 = (num_max - t_up3 + error6)%256; 
		 
		t0_flag = 7; 
	} 
	else if(t0_flag == 7) 
	{ 
		PWM3 = 0; 
		TH0 = (t_change + t_up3 +error7)/256; 
		TL0 = (t_change + t_up3 +error7)%256; 
		 
		t0_flag = 8; 
	} 
	else if(t0_flag == 8) 
	{ 
		PWM4 = 1; 
		TH0 = (num_max - t_up4 + error8)/256; //+?是为了抵消执行语句花的时间 
		TL0 = (num_max - t_up4 + error8)%256; 
		 
		t0_flag = 9; 
	} 
	else if(t0_flag == 9) 
	{ 
		PWM4 = 0; 
		TH0 = (t_change + t_up4 +error9)/256; 
		TL0 = (t_change + t_up4 +error9)%256; 
		 
		t0_flag = 10; 
	} 
	else if(t0_flag == 10) 
	{ 
		PWM5 = 1; 
		TH0 = (num_max - t_up5 + error10)/256; //+?是为了抵消执行语句花的时间 
		TL0 = (num_max - t_up5 + error10)%256; 
		 
		t0_flag = 11; 
	} 
	else if(t0_flag == 11) 
	{ 
		PWM5 = 0; 
		TH0 = (t_change + t_up5 + error11)/256; 
		TL0 = (t_change + t_up5 + error11)%256; 
		 
		t0_flag = 12; 
	} 
	else if(t0_flag == 12) 
	{ 
		PWM6 = 1; 
		TH0 = (num_max - t_up6 + error12)/256; //+?是为了抵消执行语句花的时间 
		TL0 = (num_max - t_up6 + error12)%256; 
		 
		t0_flag = 13; 
	} 
	else if(t0_flag == 13) 
	{ 
		PWM6 = 0; 
		TH0 = (t_change + t_up6 + error13)/256; 
		TL0 = (t_change + t_up6 + error13)%256; 
		 
		t0_flag = 14; 
	} 
	else if(t0_flag == 14) 
	{ 
		PWM7 = 1; 
		TH0 = (num_max - t_up7 + error14)/256; //+?是为了抵消执行语句花的时间 
		TL0 = (num_max - t_up7 + error14)%256; 
		 
		t0_flag = 15; 
	} 
	else if(t0_flag == 15) 
	{ 
		PWM7 = 0; 
		TH0 = (t_change + t_up7 + error15)/256; 
		TL0 = (t_change + t_up7 + error15)%256; 
		 
		t0_flag = 0; 
	} 
	 
} 
void main() 
{ 
	uint t_while; 
	timer_init(); 
	TR0 = 1; 
	while(1) 
	{ 
		 
		if(ADD == 0) 
		{ 
			delayms(2); 
			if(ADD == 0) 
			{ 
				if(t_up0 <= 1950) 
				{ 
					t_up0 = t_up0 + 50; 
					t_up1 = t_up1 + 50; 
					t_up2 = t_up2 + 50; 
					t_up3 = t_up3 + 50; 
					t_up4 = t_up4 + 50; 
					t_up5 = t_up5 + 50; 
					t_up6 = t_up6 + 50; 
					t_up7 = t_up7 + 50; 
				} 
				 
				t_while = 50000; 
				while(t_while--) 
				{ 
					if(ADD == 1) 
						break; 
				} 
				 
			} 
		} 
		 
		if(SUB == 0) 
		{ 
			delayms(2); 
			if(SUB == 0) 
			{ 
				if(t_up0 >= 1050) 
				{ 
					t_up0 = t_up0 - 50; 
					t_up1 = t_up1 - 50; 
					t_up2 = t_up2 - 50; 
					t_up3 = t_up3 - 50; 
					t_up4 = t_up4 - 50; 
					t_up5 = t_up5 - 50; 
					t_up6 = t_up6 - 50; 
					t_up7 = t_up7 - 50; 
				} 
				 
				t_while = 50000; 
				while(t_while--) 
				{ 
					if(SUB == 1) 
						break; 
				} 
				 
			} 
		} 
	} 
	 
}