#include“reg52.h”#defineucharunsignedchar#defineuintunsignedintP0M1=0X00;P0M0=0XFF;设置P0为强推挽输出sbitservo0=P0^0;sbitservo1=P0^1;sbitservo2=P0^2;sbitservo3=P0^3;sbitservo4=P0^4;sbitservo5=P0^5;sbitservo6=P0^6;sbitservo7=P0^7;ucharserVal[2];uintpwm[]={1382,1382,1382,1382,1382,1382,1382,1382};初始90度,(实际是1382.4,取整得1382)ucharpwm_flag=0;uintcodems0_5Con=461;0.5ms计数(实际是460.8,取整得461)uintcodems2_5Con=2304;2.5ms计数功能串口初始化,晶振11.0592,波特率9600,使能了串口中断voidCom_Init(){TMOD=0x20;用定时器设置串口波特率TH1=0xFD;256-11059200(32129600)=253(FD)TL1=0xFD;同上TR1=1;定时器1开关打开REN=1;开启允许串行接收位SM0=0;串口方式,8位数据SM1=1;同上EA=1;开启总中断ES=1;串行口中断允许位}功能舵机PWM中断初始化voidTimer0Init(){0度=0.5ms,45度=1ms,90度=1.5ms,135度=2ms,180度=2.5ms2.5ms初始值F700,(12n11059200=2.51000,n=2304,X=65536-2304=63232F700)TMOD=0x01;使用模式1,16位定时器,使用符号可以在使用多个定时器时不受影响TH0=-ms2_5Con8;给定初值,17ms中断TL0=-ms2_5Con;EA=1;总中断打开ET0=1;定时器0中断打开TR0=1;定时器0开关打开}功能舵机PWM中断,舵机控制函数周期为20ms一个循环20MS=82.5msvoidSteeringGear()interrupt1{switch(pwm_flag){case1servo0=1;TH0=-pwm[0]8;TL0=-pwm[0];break;case2servo0=0;TH0=-(ms2_5Con-pwm[0])8;TL0=-(ms2_5Con-pwm[0]);break;case3servo1=1;TH0=-pwm[1]8;TL0=-pwm[1];break;case4servo1=0;TH0=-(ms2_5Con-pwm[1])8;TL0=-(ms2_5Con-pwm[1]);break;case5servo2=1;TH0=-pwm[2]8;TL0=-pwm[2];break;case6servo2=0;TH0=-(ms2_5Con-pwm[2])8;TL0=-(ms2_5Con-pwm[2]);break;case7servo3=1;TH0=-pwm[3]8;TL0=-pwm[3];break;case8servo3=0;TH0=-(ms2_5Con-pwm[3])8;TL0=-(ms2_5Con-pwm[3]);break;case9servo4=1;TH0=-pwm[4]8;TL0=-pwm[4];break;case10servo4=0;TH0=-(ms2_5Con-pwm[4])8;TL0=-(ms2_5Con-pwm[4]);break;case11servo5=1;TH0=-pwm[5]8;TL0=-pwm[5];break;case12servo5=0;TH0=-(ms2_5Con-pwm[5])8;TL0=-(ms2_5Con-pwm[5]);break;case13servo6=1;TH0=-pwm[6]8;TL0=-pwm[6];break;case14servo6=0;TH0=-(ms2_5Con-pwm[6])8;TL0=-(ms2_5Con-pwm[6]);break;case15servo7=1;TH0=-pwm[7]8;TL0=-pwm[7];break;case16servo7=0;TH0=-(ms2_5Con-pwm[7])8;TL0=-(ms2_5Con-pwm[7]);break;defaultTH0=0xff;TL0=0x80;pwm_flag=0;}pwm_flag++;}voidSetSteeringGear(uchari,ucharval){uinta=(val+46)10;if(ams0_5Con)a=ms0_5Con;if(ams2_5Con)a=ms2_5Con;pwm[i]=a;serVal[0]=255;清除缓存}voidSteeringGearUp(uchari){if(pwm[i]ms0_5Con)pwm[i]=pwm[i]-10;}voidSteeringGearDown(uchari){if(pwm[i]ms2_5Con)pwm[i]=pwm[i]+10;}功能串口中断接收数据voidser()interrupt4{serVal[0]=serVal[1];serVal[1]=SBUF;RI=0;串口中断清0}函数功能:主函数voidmain(){bitstarted=0;路由是否已经启动完毕Com_Init();串口初始化Timer0Init();舵机初始化while(1){if(serVal[0]=='w'&&serVal[1]=='d'){started=1;路由启动最后会出现:ar71xx-wdt,由此判断路由已经启动}if(started){if(serVal[0]==0){switch(serVal[1]){case'A'SteeringGearUp(0);break;case'B'SteeringGearDown(0);break;case'C'SteeringGearUp(1);break;case'D'SteeringGearDown(1);break;case'E'SteeringGearUp(2);break;case'F'SteeringGearDown(2);break;case'G'SteeringGearUp(3);break;case'H'SteeringGearDown(3);break;case'I'SteeringGearUp(4);break;case'J'SteeringGearDown(4);break;case'K'SteeringGearUp(5);break;case'L'SteeringGearDown(5);break;defaultbreak;}serVal[0]=255;清除缓存}elseif(serVal[0]==1){SetSteeringGear(0,serVal[1]);}elseif(serVal[0]==2){SetSteeringGear(1,serVal[1]);}elseif(serVal[0]==3){SetSteeringGear(2,serVal[1]);}elseif(serVal[0]==4){SetSteeringGear(3,serVal[1]);}elseif(serVal[0]==5){SetSteeringGear(4,serVal[1]);}elseif(serVal[0]==6){SetSteeringGear(5,serVal[1]);}}}}