|
发表于 2015-9-16 23:35:00
|
显示全部楼层
- #include<REG52.H>
- #define uchar unsigned char
- #define uint unsigned int
- uint buffer[3];
- uint rec_flag=0;
- sbit zhuangtaideng=P3^7;//状态灯
- sbit dadeng =P3^4; //大灯
- sbit zuozhuanxd =P1^1; //转向灯
- sbit youzhuanxd =P2^7;
- sbit in1=P1^2; //电机IO
- sbit in2=P1^3;
- sbit in3=P1^5;
- sbit in4=P1^6;
- sbit ena=P1^4;
- sbit enb=P1^7;
- sbit servo0=P2^0; //舵机IO
- sbit servo1=P2^1;
- sbit servo2=P2^2;
- sbit servo3=P2^3;
- sbit servo4=P0^4;
- sbit servo5=P0^5;
- sbit servo6=P0^6;
- sbit servo7=P0^7;
- uint pwm[]={1382,1382,1382,1382,1382,1382,1382,1382}; //初始90度,(实际是1382.4,取整得1382)
- uint pwm_flag=0;
- uint code ms0_5Con=461; //0.5ms计数 (实际是460.8,取整得461)
- uint code ms2_5Con=2304; //2.5ms计数
- /*-----------串口初始化----------------------------*/
- void UART_Init(void)
- {
- TMOD = 0x21;
- PCON = 0x00;
- SCON = 0x50;
- TH1 = 0xFd; //设置波特率 9600
- TL1 = 0xFd;
- TR1 = 1; //启动定时器1
-
- ES = 1; //开串口中断
- EA = 1; //开总中断
- IT0=0;
- EX0=1;
- }
- void delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=800;y>0;y--);
- }
- void qianjin()
- {
- in1=1;
- in2=0;
- in3=1;
- in4=0;
- ena=1;
- enb=1;
- }
- void houtui()
- {
- in1=0;
- in2=1;
- in3=0;
- in4=1;
- ena=1;
- enb=1;
- }
- void zuozhuan()
- {
- youzhuanxd=1;
- in1=1;
- in2=0;
- in3=0;
- in4=1;
- ena=1;
- enb=1;
- zuozhuanxd=0;
- }
- void youzhuan()
- {
- zuozhuanxd=1;
- in1=0;
- in2=1;
- in3=1;
- in4=0;
- ena=1;
- enb=1;
- youzhuanxd=0;
- }
- void tingzhi()
- {
- youzhuanxd=1;
- zuozhuanxd=1;
- in1=0;
- in2=0;
- in3=0;
- in4=0;
- ena=0;
- enb=0;
- }
- void zhuangtaizhishi()
- {
- uchar i;
- for(i=0;i<70;i++)
- {
- zhuangtaideng=0;
- delay(200);
- zhuangtaideng=1;
- delay(200);
- }
- }
- /*------------------------舵机定时器--------------------------*/
- void Timer0Init()
- {
- //0度=0.5ms, 45度=1ms, 90度=1.5ms, 135度=2ms, 180度=2.5ms
- //2.5 ms初始值 F700, (12n/11059200=2.5/1000, n=2304, X=65536-2304=63232 > F700)
- TMOD |= 0x01; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响
- TH0=-ms2_5Con>>8; //给定初值,2.5ms中断
- TL0=-ms2_5Con;
- EA=1; //总中断打开
- ET0=1; //定时器0中断打开
- TR0=1; //定时器0开关打开
- }
- /*------------------------角度值转换--------------------------*/
- uint Transform(uchar val)
- {
- //0度=0.5ms, 45度=1ms, 90度=1.5ms, 135度=2ms, 180度=2.5ms
- //2.5 ms初始值 F700, (12n/11059200=2.5/1000, n=2304, X=65536-2304=63232 > F700)
- //return (uint)(((float)(2/180)*X+0.5)/1000*11059200/12);
- uint a = (val+46)*10;
- if(a<ms0_5Con)
- a=ms0_5Con;
- if(a>ms2_5Con)
- a=ms2_5Con;
- return a;
- }
- /*------------------主函数---------------------------*/
- void main(void)
- {
- zhuangtaizhishi();
- UART_Init(); //初始化串口
- Timer0Init();//舵机定时初始化
- while(1)
- {
- }
- }
- /*--------------------串口解析执行-------------------------*/
- void Communication_Decode(void)
- {
- if(buffer[0]==0x00)
- {
- switch(buffer[1])
- {
- case 0x01:qianjin(); return;
- case 0x02:houtui(); return;
- case 0x03:zuozhuan(); return;
- case 0x04:youzhuan(); return;
- case 0x00:tingzhi(); return;
- default: return;
- }
- }
- else if(buffer[0]==0x01)
- {
- if(buffer[2]>180)
- return;
- switch(buffer[1])
- {
- case 0x01:pwm[0]=Transform(buffer[2]); return; //角度值与脉宽计数值转换
- case 0x02:pwm[1]=Transform(buffer[2]); return;
- case 0x03:pwm[2]=Transform(buffer[2]); return;
- case 0x04:pwm[3]=Transform(buffer[2]); return;
- case 0x05:pwm[4]=Transform(buffer[2]); return;
- case 0x06:pwm[5]=Transform(buffer[2]); return;
- case 0x07:pwm[6]=Transform(buffer[2]); return;
- case 0x08:pwm[7]=Transform(buffer[2]); return;
- default : return;
- }
- }
- else if(buffer[0]==0x04)
- {
- switch(buffer[1])
- {
- case 0x00:dadeng=1;return;
- case 0x01:dadeng=0;return;
- default : return;
- }
- }
- else
- {
- return;
- }
- }
- /*------------------------舵机中断函数--------------------------*/
- void SteeringGear() interrupt 1
- {
- switch(pwm_flag)
- {
- case 1: servo0=1; TH0=-pwm[0]>>8; TL0=-pwm[0]; break;
- case 2: servo0=0; TH0=-(ms2_5Con-pwm[0])>>8; TL0=-(ms2_5Con-pwm[0]); break;
- case 3: servo1=1; TH0=-pwm[1]>>8; TL0=-pwm[1]; break;
- case 4: servo1=0; TH0=-(ms2_5Con-pwm[1])>>8; TL0=-(ms2_5Con-pwm[1]); break;
- case 5: servo2=1; TH0=-pwm[2]>>8; TL0=-pwm[2]; break;
- case 6: servo2=0; TH0=-(ms2_5Con-pwm[2])>>8; TL0=-(ms2_5Con-pwm[2]); break;
- case 7: servo3=1; TH0=-pwm[3]>>8; TL0=-pwm[3]; break;
- case 8: servo3=0; TH0=-(ms2_5Con-pwm[3])>>8; TL0=-(ms2_5Con-pwm[3]); break;
- case 9: servo4=1; TH0=-pwm[4]>>8; TL0=-pwm[4]; break;
- case 10: servo4=0; TH0=-(ms2_5Con-pwm[4])>>8; TL0=-(ms2_5Con-pwm[4]); break;
- case 11: servo5=1; TH0=-pwm[5]>>8; TL0=-pwm[5]; break;
- case 12: servo5=0; TH0=-(ms2_5Con-pwm[5])>>8; TL0=-(ms2_5Con-pwm[5]); break;
- case 13: servo6=1; TH0=-pwm[6]>>8; TL0=-pwm[6]; break;
- case 14: servo6=0; TH0=-(ms2_5Con-pwm[6])>>8; TL0=-(ms2_5Con-pwm[6]); break;
- case 15: servo7=1; TH0=-pwm[7]>>8; TL0=-pwm[7]; break;
- case 16: servo7=0; TH0=-(ms2_5Con-pwm[7])>>8; TL0=-(ms2_5Con-pwm[7]); break;
- default:TH0=0xff; TL0=0x80; pwm_flag=0;
- }
- pwm_flag++;
- }
- /*------------------------串口中断函数--------------------------*/
- void INT_UartRcv(void) interrupt 4
- {
- static int i;
- if(RI==1)
- {
- RI = 0;
- if(rec_flag==0)
- {
- if(SBUF==0xff)
- {
- rec_flag=1;
- i=0;
- }
- }
- else
- {
- if(SBUF==0xff)
- {
- rec_flag=0;
- if(i==3)
- {
- Communication_Decode();
- }
- i=0;
- }
- else
- {
- buffer[i]=SBUF;
- i++;
- }
- }
- }
- else
- {
- TI = 0;
- }
- }
复制代码
这个是通过楼主的程序加了状态灯延时,和转向指示灯,只用了4路舵机P20-P23。我的单片机是STC12C5608A D IO口不太够用,想添加超声波和循迹。记住晶振必须11.0592 |
|