舵机控制例程
文件大小: 508k
源码售价: 10 个金币 积分规则     积分充值
资源说明:#include #define uchar unsigned char #define uint unsigned int #define N 1 //¿è²¿¶æ»ú sbit PWM_OUT00=P0^0; sbit PWM_OUT01=P0^1; sbit PWM_OUT02=P0^2; sbit PWM_OUT03=P0^3; sbit PWM_OUT04=P0^4; sbit PWM_OUT05=P0^5; //СÍȶæ»ú sbit PWM_OUT10=P1^0; sbit PWM_OUT11=P1^1; sbit PWM_OUT12=P1^2; sbit PWM_OUT13=P1^3; sbit PWM_OUT14=P1^4; sbit PWM_OUT15=P1^5; //´óÍȶæ»ú sbit PWM_OUT20=P2^0; sbit PWM_OUT21=P2^1; sbit PWM_OUT22=P2^2; sbit PWM_OUT23=P2^3; sbit PWM_OUT24=P2^4; sbit PWM_OUT25=P2^5; //PWMµÄÊý¾ÝÖµ uint code PWM_Value[N][18]={1490,1500,1510,1490,1500,1510,1490,1500,1510,1490,1500,1510,1490,1500,1510,1490,1500,1510}; //¶¨ÒåpwmÐźÅÖеĸߵçƽʱ¼ä ·¶Î§ 535< pwm_h <2430 uint PWM_now[18] ; //µ±Ç°pwmµÄÊýÖµ uint *pwm_now_ad[18]; //µ±Ç°pwmµÄµØÖ· uchar dongzuo_pd=0; //¶¯×÷Ƭ¶ÎµÄ˳Ðò´úºÅ uchar order1; //¶¨Ê±Æ÷ɨÃèÐòÁÐ uchar dj_num; bit fa_time=0; bit fa_ck=0; struct Uartframe { unsigned char frameh; unsigned char id; //idΪ0x30ʱΪȫ·¿ØÖÆÖ¸Áî unsigned char cmd; unsigned char udata; unsigned char framee; }; struct Uartframe sendframe={'#',0xff,0xff,0xff,'$'}, recframe={'#',0xff,0xff,0xff,'$'}; /*------------------------------------------------ ´®¿Ú³õʼ»¯ ------------------------------------------------*/ void InitUART (void) { PS=1; SCON |= 0x50; // SCON: ģʽ 1, 8-bit UART, ʹÄܽÓÊÕ TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit ÖØ×° TH1 = 0xFD; // TH1: ÖØ×°Öµ 9600 ²¨ÌØÂÊ ¾§Õñ 11.0592MHz TR1 = 1; // TR1: timer 1 ´ò¿ª EA = 0; //¹Ø±Õ×ÜÖÐ¶Ï ES = 1; //´ò¿ª´®¿ÚÖÐ¶Ï } /* void send() { unsigned char i,*uartp = &sendframe;.frameh; for(i=0;i0;c--) for(b=142;b>0;b--) for(a=2;a>0;a--); } } void pwm_nowfz() { uchar i,j,k,zu; uint *tempp; for(i=0;i<18;i++) { PWM_now[i]=PWM_Value[dongzuo_pd][i]; } for(zu=0;zu<18;zu=zu+3) { for(j=zu;j*pwm_now_ad[k]) { tempp=pwm_now_ad[j]; pwm_now_ad[j]=pwm_now_ad[k]; pwm_now_ad[k]=tempp; } } } } } void dongzuo1() { dongzuo_pd=0;pwm_nowfz();EA=1; delayMS(2); /* delayMS(5000); dongzuo_pd=1;pwm_nowfz(); delayMS(5000); dongzuo_pd=2;pwm_nowfz(); delayMS(5000); dongzuo_pd=3;pwm_nowfz(); delayMS(5000); */ } void io_dp() //io¿ÚµçƽÉú³É { dj_num=pwm_now_ad[order1-1]-&PWM;_now[0]; switch(dj_num) { case 0 : PWM_OUT00=0;break; case 1 : PWM_OUT01=0;break; case 2 : PWM_OUT02=0;break; case 3 : PWM_OUT03=0;break; case 4 : PWM_OUT04=0;break; case 5 : PWM_OUT05=0;break; case 6 : PWM_OUT10=0;break; case 7 : PWM_OUT11=0;break; case 8 : PWM_OUT12=0;break; case 9 : PWM_OUT13=0;break; case 10 : PWM_OUT14=0;break; case 11: PWM_OUT15=0;break; case 12: PWM_OUT20=0;break; case 13: PWM_OUT21=0;break; case 14 : PWM_OUT22=0;break; case 15 : PWM_OUT23=0;break; case 16 : PWM_OUT24=0;break; case 17 : PWM_OUT25=0;break; } } void main(void) { InitPWM(); InitUART(); ad(); while(1) { dongzuo1(); } } unsigned char *uartp = &recframe;.frameh; void UARTInterrupt(void) interrupt 4 { uchar temp; if(RI) { RI = 0; temp=SBUF; SBUF=temp; if('#' == SBUF | (uartp-&recframe;.frameh)>sizeof(recframe)) uartp = &recframe;.frameh; if('$' == SBUF) gengxinshuju(); *uartp = SBUF; uartp++; } else { TI=0; } } /*=================================================================================== ¶¨Ê±Æ÷T0µÄÖжϷþÎñ³ÌÐò Ò»¸öÑ­»·20MS = 8*2.5ms =====================================================================================*/ void timer0(void) interrupt 1 using 1 { //pwm_timer(); switch(order1) { case 0: //io_dp(); TH0 =(-*pwm_now_ad[0])/256; TL0 =(-*pwm_now_ad[0])%6; PWM_OUT00=1; PWM_OUT01=1; PWM_OUT02=1; order1++; break; case 1: TH0 =-(*pwm_now_ad[1]-*pwm_now_ad[0])/256; TL0 =-(*pwm_now_ad[1]-*pwm_now_ad[0])%6; io_dp(); order1++; break; case 2: TH0 =-(*pwm_now_ad[2]-*pwm_now_ad[1])/256; TL0 =-(*pwm_now_ad[2]-*pwm_now_ad[1])%6; io_dp(); order1++; break; case 3: TH0 =-(2303-*pwm_now_ad[2])/256; TL0 =-(2303-*pwm_now_ad[2])%6; io_dp(); order1=19; break; case 19: //io_dp(); TH0 =(-*pwm_now_ad[3])/256; TL0 =(-*pwm_now_ad[3])%6; PWM_OUT03=1; PWM_OUT04=1; PWM_OUT05=1; //order1++; order1=4; break; case 4: TH0 =-(*pwm_now_ad[4]-*pwm_now_ad[3])/256; TL0 =-(*pwm_now_ad[4]-*pwm_now_ad[3])%6; io_dp(); order1++; break; case 5: TH0 =-(*pwm_now_ad[5]-*pwm_now_ad[4])/256; TL0 =-(*pwm_now_ad[5]-*pwm_now_ad[4])%6; io_dp(); order1++; if(order1>18) order1=0; break; case 6: TH0 =-(2303-*pwm_now_ad[5])/256; TL0 =-(2303-*pwm_now_ad[5])%6; io_dp(); order1=20; break; case 20: TH0 =(-*pwm_now_ad[6])/256; TL0 =(-*pwm_now_ad[6])%6; PWM_OUT10=1; PWM_OUT11=1; PWM_OUT12=1; //order1++; order1=7; break; case 7: TH0 =-(*pwm_now_ad[7]-*pwm_now_ad[6])/256; TL0 =-(*pwm_now_ad[7]-*pwm_now_ad[6])%6; io_dp(); order1++; break; case 8: TH0 =-(*pwm_now_ad[8]-*pwm_now_ad[7])/256; TL0 =-(*pwm_now_ad[8]-*pwm_now_ad[7])%6; io_dp(); order1++; if(order1>18) order1=0; break; case 9: TH0 =-(2303-*pwm_now_ad[8])/256; TL0 =-(2303-*pwm_now_ad[8])%6; io_dp(); order1=21; break; case 21: TH0 =(-*pwm_now_ad[9])/256; TL0 =(-*pwm_now_ad[9])%6; PWM_OUT13=1; PWM_OUT14=1; PWM_OUT15=1; //order1++; order1=10; break; case 10: TH0 =-(*pwm_now_ad[10]-*pwm_now_ad[9])/256; TL0 =-(*pwm_now_ad[10]-*pwm_now_ad[9])%6; io_dp(); order1++; break; case 11: TH0 =-(*pwm_now_ad[11]-*pwm_now_ad[10])/256; TL0 =-(*pwm_now_ad[11]-*pwm_now_ad[10])%6; io_dp(); order1++; break; case 12: TH0 =-(2303-*pwm_now_ad[11])/256; TL0 =-(2303-*pwm_now_ad[11])%6; io_dp(); order1=22; break; case 22: TH0 =(-*pwm_now_ad[12])/256; TL0 =(-*pwm_now_ad[12])%6; PWM_OUT20=1; PWM_OUT21=1; PWM_OUT22=1; //order1++; order1=13; break; case 13: TH0 =-(*pwm_now_ad[13]-*pwm_now_ad[12])/256; TL0 =-(*pwm_now_ad[13]-*pwm_now_ad[12])%6; io_dp(); order1++; break; case 14: TH0 =-(*pwm_now_ad[14]-*pwm_now_ad[13])/256; TL0 =-(*pwm_now_ad[14]-*pwm_now_ad[13])%6; io_dp(); order1++; break; case 15: TH0 =-(2303-*pwm_now_ad[14])/256; TL0 =-(2303-*pwm_now_ad[14])%6; io_dp(); order1=23; break; case 23: TH0 =(-*pwm_now_ad[15])/256; TL0 =(-*pwm_now_ad[15])%6; PWM_OUT23=1; PWM_OUT24=1; PWM_OUT25=1; //order1++; order1=16; break; case 16: TH0 =-(*pwm_now_ad[16]-*pwm_now_ad[15])/256; TL0 =-(*pwm_now_ad[16]-*pwm_now_ad[15])%6; io_dp(); order1++; break; case 17: TH0 =-(*pwm_now_ad[17]-*pwm_now_ad[16])/256; TL0 =-(*pwm_now_ad[17]-*pwm_now_ad[16])%6; io_dp(); order1++; break; case 18: TH0 =-(5000-*pwm_now_ad[17])/256; TL0 =-(5000-*pwm_now_ad[17])%6; io_dp(); //order1=19; order1++; if(order1>18) order1=0; break; default : order1=0;break; } }
本源码包内暂不包含可直接显示的源代码文件,请下载源码包。