|
楼主 |
发表于 2015-9-16 10:53:14
|
显示全部楼层
#include<STC12C5A60S2.H>
#include<intrins.h>
#include<math.h>
#define uchar unsigned char
#define uint unsigned int
sbit MAXLED = P2^0;//灯
sbit TRIG = P2 ^ 2;
sbit ECHU = P2 ^ 1;//超声波
sbit Input_Detect1=P2^4;
sbit Input_Detect2=P2^5;//距离检测
uchar serVal[2];
uint pwm[]={1500,1500,1500,1500,1500,1500,1500,1500};
uchar pwm_flag = 1;
uint code ms0_5Con = 500;//0.5ms
uint code ms2_5Con = 2500;//2.5ms
bit key_stime_ok;
sbit servo0 = P0^0;
sbit servo1 = P0^1;
sbit servo2 = P0^2;
sbit servo3 = P0^3;
sbit servo4 = P0^4;
sbit servo5 = P0^5;
sbit servo6 = P0^6;
sbit servo7 = P0^7;
#define Left_moto_go {IN1 = 1,IN2 = 0,IN3 = 1,IN4 = 0;} //左边两个电机向前走
#define Left_moto_back {IN1 = 0,IN2 = 1,IN3 = 0,IN4 = 1;} //左边两个电机向后转
#define Left_moto_Stop {IN1 = 0,IN2 = 0,IN3 = 0,IN4 = 0;} //左边两个电机停转
#define Right_moto_go {IN5 = 1,IN6 = 0,IN7 = 1,IN8 = 0;} //右边两个电机向前走
#define Right_moto_back {IN5 = 0,IN6 = 1,IN7 = 0,IN8 = 1;} //右边两个电机向前走
#define Right_moto_Stop {IN5 = 0,IN6 = 0,IN7 = 0,IN8 = 0;} //右边两个电机停转
sbit IN1 = P1^0;
sbit IN2 = P1^1;
sbit IN3 = P1^2;
sbit IN4 = P1^3;
sbit IN5 = P1^4;
sbit IN6 = P1^5;
sbit IN7 = P1^6;
sbit IN8 = P1^7;
uchar Buffer =0; //从串口接收的数据
uint i=0,j,URTAReceivedCount=0,n=1;
uchar flag=0;
uchar data Tempdatatable[5],CommandDatatable[5];//数据包
void DELAY_1ms(uint i)
{
uchar x,j;
for(j=0;j<i;j++)
for(x=0;x<=148;x++);
}
void Send_Data(uchar data type,uchar data cmd,uchar data com )
{
uchar data Buffer[4];
uchar *p;
uint Send_Count = 0;
p = Buffer;
Buffer[0]=0xff;
Buffer[1]=type;
Buffer[2]=cmd;
Buffer[3]=com;
Buffer[4]=0xff;
while(1)
{
if(*p==0xff)
{
Send_Count++;
}
SBUF = *p;
while(!TI)
{
_nop_();
}
p++;
TI = 0;
if(Send_Count == 2)
{
TI=0;
break;
}
}
}
/**********************************************************************
协议规定:
包头 类型位 数据位 结束位
0XFF 1 1 0XFF
各命令说明:
类型位 数据位 功能
0X00 0X01 前进
0X00 0X02 后退
0X00 0X03 左转
0X00 0X04 右转
0X00 0X00 停止
0X01 0X01 舵机上
0X01 0X02 舵机下
0X02 0X01 车灯亮
0X02 0X02 车灯灭
0X03 雷达数据 发送雷达数据
***********************************************************************/
void Com_Init(void)
{
SCON = 0X50;
BRT = 0XD9;
AUXR = 0X15;
PCON=0X00;
ES = 1;
EA = 1;
IT0 = 0;
EX0 = 1;
TMOD|=0x11;
}
void UART_T (unsigned char UART_data){ //定义串口发送数据变量
SBUF = UART_data; //将接收的数据发送回去
while(TI == 0); //检查发送中断标志位
TI = 0; //令发送中断标志位为0(软件清零)
}
void UART_TC (unsigned char *str){
while(*str != '\0'){
UART_T(*str);
*str++;
}
*str = 0;
}
//void TimerInit()
//{
// control_signal = 0;
// EA = 1;
// ET0 = 1;//定时器0中断开启
// TH0 = 0XFF;
// TL0 = 0XA3;
// TR0 = 0;
//}
void Moto_Forward()
{
Left_moto_go ; //左电机往前走
Right_moto_go ; //右电机往前走
DELAY_1ms(100);
}
void Moto_Backward()
{
Left_moto_back ; //左电机往前走
Right_moto_back ; //右电机往前走
DELAY_1ms(100);
}
void Moto_Rightward()
{
Left_moto_go ; //左电机往前走
Right_moto_back ; //右电机往前走
DELAY_1ms(100);
}
void Moto_Lightward()
{
Left_moto_back ; //左电机往前走
Right_moto_go ; //右电机往前走
DELAY_1ms(100);
}
void Moto_Stopward()
{
Left_moto_Stop ; //左电机往前走
Right_moto_Stop ; //右电机往前走
DELAY_1ms(100);
}
void Timer0Init()
{
/*0度=0.5ms, 45度=1ms, 90度=1.5ms, 135度=2ms, 180度=2.5ms
/.5 ms初始值 F700, (12n/11059200=2.5/1000, n=2304, X=65536-2304=63232 > F700)*/
TMOD |=0x11;
TH0=-ms2_5Con>>8;
TL0=-ms2_5Con;
EA = 1;
ET0 = 1;
TR0 = 1;
}
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 SetSteeringGear(uint i,uchar val)
{
uint a = (val+46)*10;
if(a<ms0_5Con)
a=ms0_5Con;
if(a>ms2_5Con)
a=ms2_5Con;
pwm[i]=a;
CommandDatatable[2]=0xff;//清除缓存
}
void SetSteeringGearUp(uchar i)
{
pwm[i]=pwm[i]+50;
if(pwm[i]>ms2_5Con)
pwm[i]=2500;
}
void SetSteeringGearDown(uchar i)
{
pwm[i]=pwm[i]-50;
if(pwm[i]<ms0_5Con)
pwm[i]=pwm[i]+50;
}
void UL_START (void){//给至少10uS的高电平(两次采集间隔应大于60mS)
TRIG = 1;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
TRIG = 0;
}
/**********************************************************************************************/
int UL_ECHO (void) {//超声波返回数据等待及数据处理
unsigned int CM;
unsigned char b;
UL_START();
//等待返回数据
TH1 = 0; //16位计数寄存器T0高8位
TL1 = 0; //16位计数寄存器T0低8位
while(ECHU==0);//
TR1 = 1; //T/C0启动开关
while(ECHU==1);
TR1 = 0; //T/C0启动开关
//处理返回时间值
CM=TH1*256+TL1;
CM =CM/58;//Y米=(X秒*344)/2 ---> X秒=(2*Y米)/344 ---> X秒=0.0058*Y米 ---> 厘米=微秒/58
//把十六进制换成十进制数据(在十六进制下显示)
b=CM/10;
CM=CM%10;
CM=b*16+CM;//最终得到的a即距离(单位:CM)
return CM; //返回值
}
/*void StreeEngineTurningUp()
{
ET0 = 1;
TR0 = 1;
DELAY_1ms(40);
hight_votage-=1;
if(hight_votage<5)
hight_votage = 5;
}
void StreeEngineTurningDowm()
{
ET0 = 1;
TR0 = 1;
DELAY_1ms(40);
hight_votage+=1;
if(hight_votage>20)
hight_votage = 20;
}
void StreeEngine () interrupt 1
{
TH0 = 0XFF;
TL0 = 0XA3;
if(++key_stime_counter>=200)
{
key_stime_counter = 0;
control_signal = 1;
key_stime_ok = 1;//20ms到
timeT_counter = 0;
}
if(key_stime_ok && (++timeT_counter>=hight_votage))
{
key_stime_ok = 0;
timeT_counter = 0;
control_signal = 0;//hight_votage*0.1MS到
}
}*/
void main()
{
P0M1=0X00;
P0M0=0XFF;
DELAY_1ms(200);
MAXLED=1;
Com_Init();
Timer0Init();
while(1)
{
if(UL_ECHO()<15 || (Input_Detect1==1 && Input_Detect2==1))
{
Moto_Backward();
DELAY_1ms(30);
Moto_Stopward();
}
if(CommandDatatable[0]==0xff &&CommandDatatable[4] == 0xff)
{
if(CommandDatatable[1]==0x00 && CommandDatatable[3]==0x00)
{
switch(CommandDatatable[2])
{
case 0x00 :
Moto_Forward();
return;
case 0x01 :
Moto_Backward();
return;
case 0x02 :
Moto_Lightward();
return;
case 0x03:
Moto_Rightward();
return;
case 0x04 :
Moto_Stopward();
return;
}
}
if(CommandDatatable[1]==0x01)
{
if(CommandDatatable[3]>180)
return;
switch(CommandDatatable[2])
{
case 0x01:SetSteeringGearUp(0);break;
case 0x02:SetSteeringGearDown(0);break;
case 0x03:SetSteeringGearUp(1);break;
case 0x04:SetSteeringGearDown(1);break;
case 0x05:SetSteeringGearUp(2);break;
case 0x06:SetSteeringGearDown(2);break;
case 0x07:SetSteeringGearUp(3);break;
case 0x08:SetSteeringGearDown(3);break;
case 0x10:SetSteeringGearUp(4);break;
case 0x20:SetSteeringGearDown(4);break;
case 0x30:SetSteeringGearUp(5);break;
case 0x40:SetSteeringGearDown(5);break;
case 0x50:SetSteeringGearUp(6);break;
case 0x60:SetSteeringGearDown(6);break;
case 0x70:SetSteeringGearUp(7);break;
case 0x80:SetSteeringGearDown(7);break;
default : break;
}
}
if(CommandDatatable[1]==0x02 && CommandDatatable[3]==0x00)
{
switch(CommandDatatable[2])
{
case 0x00: MAXLED=1;break;
case 0x01: MAXLED=0;break;
}
}
}CommandDatatable[1]=0xff;
}
}
void Com_Int(void) interrupt 4
{
uchar temp;
ES = 0;
RI = 0;
temp=SBUF;
if(temp == 0xff && URTAReceivedCount<2)
{
Tempdatatable[0]=SBUF;
URTAReceivedCount++;
}
else
{
Tempdatatable[n]=temp;
n++;
}
if(URTAReceivedCount == 2)
{
Tempdatatable[0] = 0XFF;
Tempdatatable[4] = 0XFF;
n=1;
URTAReceivedCount = 0;
Send_Data(Tempdatatable[1],Tempdatatable[2],Tempdatatable[3]);
temp="";
}
CommandDatatable[0] = Tempdatatable[0];
CommandDatatable[1] = Tempdatatable[1];
CommandDatatable[2] = Tempdatatable[2];
CommandDatatable[3] = Tempdatatable[3];
CommandDatatable[4] = Tempdatatable[4];
ES=1;
} |
|