舵机工作原理与单片机控制

需积分: 18 4 下载量 52 浏览量 更新于2024-09-10 2 收藏 323KB PDF 举报
"这篇资料详细解释了舵机的工作原理,包括其结构、功能以及如何通过PWM信号控制舵机的角度。文中还提到了舵机在机器人和微机电系统中的应用,并给出了单片机控制舵机转角的方法。" 舵机是一种广泛应用在机器人、无人机和航模等领域的微型伺服驱动装置,其主要特点是结构紧凑、易于安装和控制,具有较大的扭矩和适中的成本。舵机的最大力矩和工作速度是衡量其性能的关键指标,它能根据控制信号精确地改变并保持特定的角度。 舵机的内部构造通常包括电机、减速齿轮组和一个电位器。电机负责提供动力,减速齿轮组用于增大扭矩并降低转速,电位器则用来反馈电机的转动位置,形成一个闭环控制系统。在接收到控制信号时,舵机会根据信号的占空比来改变电机的转动,进而调整输出轴的角度。标准的舵机有三条引线,分别连接电源、地线和控制信号线。 控制信号是PWM(脉冲宽度调制)信号,其周期固定为20毫秒,但脉冲宽度可以在0.5毫秒到2.5毫秒之间变化,对应的舵机输出轴转角范围是0°到180°。脉冲宽度越宽,舵机转角越大。 在单片机控制舵机时,首先要生成一个20毫秒的PWM周期信号,然后通过改变这个周期内脉冲的宽度,即占空比,来调整舵机的角度。这一过程涉及到单片机的定时器配置和中断处理,通过设置定时器的预分频值和计数模式,可以产生稳定的PWM信号,并通过改变比较寄存器的值来改变脉冲宽度。 在实际应用中,可以通过编写程序来实现对舵机的精确控制。例如,可以设置一个计数器,当计数值达到设定的脉宽时,切换PWM信号的高电平和低电平,从而改变占空比,进而调整舵机的角度。通过这种方式,单片机可以根据需要实时调整舵机的位置,实现各种复杂的运动控制。 理解舵机的工作原理和PWM信号的控制方式对于设计和控制涉及舵机的系统至关重要。通过熟练掌握这些知识,开发者可以灵活地将舵机应用于各种自动化和机器人项目中,实现精确的角度定位和运动控制。
2018-06-12 上传
#include<REG52.h> #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;i<sizeof(sendframe);i++) { ES = 0; TI = 0; SBUF = *uartp; while(!TI); TI = 0; ES = 1; uartp++; } } */ void gengxinshuju() { if(recframe.frameh=='#' & recframe.framee=='$') { switch(recframe.id) { case 0x04: PWM_now[recframe.cmd-1] = recframe.udata*7+550; P2=0x80; //ÓÃÁ÷Ë®µÆÑéÖ¤ break; case 0x01: P2=0x10; break; case 0x02: P2=0x20; break; case 0x03: P2=0x40; break; default: P2=0x0f; } } /* SBUF=recframe.frameh; SBUF=recframe.id; SBUF=recframe.cmd; SBUF=recframe.udata; SBUF=recframe.framee; */ } void ad() { uchar i; for(i=0;i<18;i++) { pwm_now_ad[i]=&PWM;_now[i]; } } void pwm_timer(); /*=================================================================================== ³õʼ»¯ÖÐ¶Ï =====================================================================================*/ void InitPWM(void) { order1=0; TMOD |=0x11; TH0=-1500/256; TL0=-1500%6; EA=0; EX0=0; ET0=1; TR0=1;PT0=1;PX0=0; } void delayMS(uint x) //ºÁÃ뼶ÑÓʱ Îó²î 0us { uchar a,b,c; while(x--) { for(c=1;c>0;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<zu+2;j++) //Èý¸ö¶æ»úÅÅÐò { for(k=j+1;k<zu+3;k++) { if(*pwm_now_ad[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; } }