C语言控制舵机程序详解

4星 · 超过85%的资源 需积分: 9 37 下载量 85 浏览量 更新于2024-09-15 收藏 4KB TXT 举报
"这篇资源是关于使用C语言编写舵机控制程序的学习教程,旨在帮助初学者熟悉C语言在控制舵机中的应用。" 舵机控制是电子工程领域中常见的任务,尤其是在机器人和遥控模型制作中。C语言是一种广泛应用的编程语言,因其高效和灵活性而被选择用于控制舵机。舵机是一种能够精确地在一定角度范围内转动的伺服电机,通过发送特定脉宽调制(PWM)信号来设定其转动角度。 在这个C程序中,可以看到以下几个关键知识点: 1. **定义头文件和数据类型**:`#include<reg51.h>`表明程序是针对51系列单片机编写的。`#define`语句用于定义符号常量,如`uchar`和`uint`分别代表无符号字符型和无符号整型,简化了代码的可读性。 2. **定义变量和标志位**:`uchar key_stime_counter`, `hight_votage`, 和 `timeT_counter`是无符号字符型变量,用于计时和状态检测。`bit key_stime_ok`和`sbit`定义的`control_signal`, `turn_left`, `turn_right`是位变量和特殊功能寄存器位,用于控制舵机信号和转向。 3. **定时器初始化**:`TimerInit()`函数用于设置定时器0,它使用模式1(16位定时器)。`TMOD=0x01`设置定时器工作模式,`EA`和`ET0`开启全局中断和定时器0中断。`TH0`和`TL0`设置初始计数值,以实现20毫秒的周期,这通常是舵机PWM信号的周期。`TR0=1`启动定时器。 4. **定时器中断服务子程序**:`timer0(void)interrupt 1 using 0`是定时器0的中断服务程序。当定时器溢出时,它会更新计数值并检查`key_stime_counter`,以执行舵机控制逻辑。这里,`key_stime_counter`用于检测20毫秒的时间间隔,`timeT_counter`则用于检测高电压时间。 5. **PWM信号生成**:通过改变`control_signal`的状态,可以控制舵机的PWM信号,进而改变舵机的角度。`turn_left`和`turn_right`则用于控制舵机的左转和右转。 6. **脉宽调制(PWM)原理**:PWM是一种通过改变占空比(高电平持续时间与总周期的比例)来控制信号平均功率的技术。在舵机控制中,通过调整PWM信号的脉宽,可以设定舵机转动到相应角度。 7. **中断处理**:中断是单片机处理外部事件的重要机制。在本程序中,定时器0的中断每20毫秒发生一次,用于控制舵机的脉冲周期,并进行其他逻辑判断。 这个C程序实例提供了一个基础的舵机控制框架,开发者可以通过修改其中的计数值和控制逻辑,实现对不同型号舵机的更复杂控制。学习和理解这个程序,有助于提升C语言编程和单片机控制技能。
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; } }