单片机(8bit)的16路舵机调速分析与实现
时间:11-18
来源:互联网
点击:
// main.c
[cpp]view plaincopyprint?
- #include
1.H> - #include"ControlRobot.h"
- #include
- #defineDEBUG_PROTOCOL
- typedefunsignedcharUCHAR8;
- typedefunsignedintUINT16;
- #undefTRUE
- #undefFALSE
- #defineTRUE1
- #defineFALSE0
- #defineMEMORY_MODEL
- UINT16MEMORY_MODELdelayVar1;
- UCHAR8MEMORY_MODELdelayVar2;
- #defineBAUD_RATE0xF3
- #defineUSED_PIN2
- #definePIN_GROUP_10
- #definePIN_GROUP_21
- #defineGROUP_1_CONTROL_PINP0
- #defineGROUP_2_CONTROL_PINP2
- #defineSTEERING_ENGINE_CYCLE2000
- #defineSTEERING_ENGINE_INIT_DELAY50
- #defineSTEERING_ENGINE_FULL_CYCLE((STEERING_ENGINE_CYCLE)-(STEERING_ENGINE_INIT_DELAY))
- volatileUCHAR8MEMORY_MODELg_angle[2][ROBOT_JOINT_AMOUNT];
- volatilebitMEMORY_MODELg_fillingBufferIndex=0;
- volatilebitMEMORY_MODELg_readyBufferIndex=1;
- volatilebitMEMORY_MODELg_swapBuffer=FALSE;
- volatileUINT16MEMORY_MODELg_diffAngle[USED_PIN][8];
- volatileUCHAR8MEMORY_MODELg_diffAngleMask[USED_PIN][8]=
- {
- {
- ROBOT_PIN_MASK_LEFT_SHOULDER_VERTICAL,
- ROBOT_PIN_MASK_LEFT_SHOULDER_HORIZEN,
- ROBOT_PIN_MASK_LEFT_ELBOW,
- ROBOT_PIN_MASK_RIGHT_SHOULDER_VERTICAL,
- ROBOT_PIN_MASK_RIGHT_SHOULDER_HORIZEN,
- ROBOT_PIN_MASK_RIGHT_ELBOW,
- ROBOT_PIN_MASK_LEFT_HIP_VERTICAL,
- ROBOT_PIN_MASK_RIGHT_HIP_VERTICAL
- },
- {
- ROBOT_PIN_MASK_LEFT_HIP_HORIZEN,
- ROBOT_PIN_MASK_LEFT_KNEE,
- ROBOT_PIN_MASK_LEFT_ANKLE,
- ROBOT_PIN_MASK_LEFT_FOOT,
- ROBOT_PIN_MASK_RIGHT_HIP_HORIZEN,
- ROBOT_PIN_MASK_RIGHT_KNEE,
- ROBOT_PIN_MASK_RIGHT_ANKLE,
- ROBOT_PIN_MASK_RIGHT_FOOT
- }
- };
- #ifdefDEBUG_PROTOCOL
- sbitP10=P1^0;//正在填充交换区1
- sbitP11=P1^1;//正在填充交换区2
- sbitP12=P1^2;//交换区变换
- sbitP13=P1^3;//协议是否正确
- #endif
- voidDelay10us(UINT16ntimes)
- {
- for(delayVar1=0;delayVar1
- for(delayVar2=0;delayVar2<21;++delayVar2)
- _nop_();
- }
- voidInitPwmPollint()
- {
- UCHAR8i;
- UCHAR8j;
- UCHAR8k;
- UINT16temp;
- for(i=0;i
- {
- for(j=0;j<7;++j)
- {
- for(k=j;k<8;++k)
- {
- if(g_diffAngle[i][j]>g_diffAngle[i][k])
- {
- temp=g_diffAngle[i][j];
- g_diffAngle[i][j]=g_diffAngle[i][k];
- g_diffAngle[i][k]=temp;
- temp=g_diffAngleMask[i][j];
- g_diffAngleMask[i][j]=g_diffAngleMask[i][k];
- g_diffAngleMask[i][k]=temp;
- }
- }
- }
- }
- for(i=0;i
- {
- for(j=0;j<8;++j)
- {
- if(INVALID_JOINT_VALUE==g_diffAngle[i][j])
- {
- g_diffAngle[i][j]=STEERING_ENGINE_FULL_CYCLE;
- }
- }
- }
- for(i=0;i
- {
- for(j=7;j>=1;--j)
- {
- g_diffAngle[i][j]=g_diffAngle[i][j]-g_diffAngle[i][j-1];
- }
- }
- }
- voidInitSerialPort()
- {
- AUXR=0x00;
- ES=0;
- TMOD=0x20;
- SCON=0x50;
- TH1=BAUD_RATE;
- TL1=TH1;
- PCON=0x80;
- EA=1;
- ES=1;
- TR1=1;
- }
- voidOnSerialPort()interrupt4
- {
- staticUCHAR8previousChar=0;
- staticUCHAR8currentChar=0;
- staticUCHAR8counter=0;
- if(RI)
- {
- RI=0;
- currentChar=SBUF;
- if(PROTOCOL_HEADER[0]==currentChar)//协议标志
- {
- previousChar=currentChar;
- }
- else
- {
- if(PROTOCOL_HEADER[0]==previousChar&&PROTOCOL_HEADER[1]==currentChar)//协议开始
- {
- counter=0;
- previousChar=currentChar;
- g_swapBuffer=FALSE;
- }
- elseif(PROTOCOL_END[0]==previousChar&&PROTOCOL_END[1]==currentChar)//协议结束
- {
- previousChar=currentChar;
- if(ROBOT_JOINT_AMOUNT==counter)//协议接受正确
- {
- if(0==g_fillingBufferIndex)
- {
- g_readyBufferIndex=0;
- g_fillingBufferIndex=1;
- }
- else
- {
- g_readyBufferIndex=1;
- g_fillingBufferIndex=0;
- }
- g_swapBuffer=TRUE;
- #ifdefDEBUG_PROTOCOL
- P13=0;
- #endif
- }
- else
- {
- g_swapBuffer=FALSE;
- #ifdefDEBUG_PROTOCOL
- P13=1;
- #endif
- }
- counter=0;
- }
- else//接受协议正文
- {
- g_swapBuffer=FALSE;
- previousChar=currentChar;
- if(counter
- g_angle[g_fillingBufferIndex][counter]=currentChar;
- ++counter;
- }
- }//if(PROTOCOL_HEADER[0]==currentChar)
- SBUF=currentChar;
- while(!TI)
- ;
- TI=0;
- }//(RI)
- }
- voidFillDiffAngle()
- {
- //设置舵机要调整的角度
- g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_SHOULDER_VERTICAL]=g_angle[g_readyBufferIndex][ROBOT_LEFT_SHOULDER_VERTICAL];
- g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_SHOULDER_HORIZEN]=g_angle[g_readyBufferIndex][ROBOT_LEFT_SHOULDER_HORIZEN];
- g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_ELBOW]=g_angle[g_readyBufferIndex][ROBOT_LEFT_ELBOW];
- g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_RIGHT_SHOULDER_VERTICAL]=g_an
单片机1舵机调 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)