|
#include<reg52.h>
sbit ina=P1^0;
sbit IN1=P1^1;
sbit IN2=P1^2;
sbit inb=P1^5;
sbit IN3=P1^3;
sbit IN4=P1^4;
sbit inc=P0^0;
sbit IN5=P0^1;
sbit IN6=P0^2;
sbit ind=P0^5;
sbit IN7=P0^3;
sbit IN8=P0^4;
#define left_go {inc=1,IN5=1,IN6=0,inb=1,IN3=1,IN4=0;}//P1:0-3控制左邊 前
#define left_back {inc=1,IN5=0,IN6=1,inb=1,IN3=0,IN4=1;} //后
#define left_stop {inc=0,IN5=0,IN6=0,inb=0,IN3=0,IN4=0;} //停止
#define right_go {ina=1,IN1=1,IN2=0,ind=1,IN7=1,IN8=0;}//P1:4-7控制右邊
#define right_back {ina=1,IN1=0,IN2=1,ind=1,IN7=0,IN8=1;} //
#define right_stop {ina=0,IN1=0,IN2=0,ind=0,IN7=0,IN8=0;}
#define uchar unsigned char
#define uint unsigned int
void run();
void backrun();
void leftrun();
void rightrun();
void stoprun();
void delay(uchar time);
void init();
uchar flag,com;
void main()
{
init();
while(1)
{
if(flag==1)
{
switch(com)
{
case 'A':
run();
break;
case 'B':
backrun();
break;
case 'C':
leftrun();
break;
case 'D':
rightrun();
break;
case 'E':
stoprun();
break;
case '1':
stoprun();
break;
default:
break;
}
}
RI=1;
}
}
void init()
{
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
TR1=1;
REN=1;
SM0=0;
SM1=1;
EA=1;
ES=1;
flag=0;
}
void run()
{
left_go;
right_go;
}
void backrun()
{
left_back;
right_back;
}
void leftrun()
{
left_back;
right_go;
}
void rightrun()
{
left_go;
right_back;
}
void stoprun()
{
left_stop;
right_stop;
}
void ser() interrupt 4
{
RI=0;
com=SBUF;
flag=1;
}
#include<reg52.h>
#include <intrins.h>
sbit Sevro_moto_pwm = P2^7; //接舵機(jī)信號端輸入PWM信號調(diào)節(jié)速度
sbit ECHO= P0^7; //超聲波接口定義
sbit TRIG=P0^6; //超聲波接口定義
sbit ina=P1^0;
sbit IN1=P1^1;
sbit IN2=P1^2;
sbit inb=P1^5;
sbit IN3=P1^3;
sbit IN4=P1^4;
sbit inc=P0^0;
sbit IN5=P0^1;
sbit IN6=P0^2;
sbit ind=P0^5;
sbit IN7=P0^3;
sbit IN8=P0^4;
#define left_go {inc=1,IN5=1,IN6=0,inb=1,IN3=1,IN4=0;}//P1:0-3控制左邊 前
#define left_back {inc=1,IN5=0,IN6=1,inb=1,IN3=0,IN4=1;} //后
#define left_stop {inc=0,IN5=0,IN6=0,inb=0,IN3=0,IN4=0;} //停止
#define right_go {ina=1,IN1=1,IN2=0,ind=1,IN7=1,IN8=0;}//P1:4-7控制右邊
#define right_back {ina=1,IN1=0,IN2=1,ind=1,IN7=0,IN8=1;} //
#define right_stop {ina=0,IN1=0,IN2=0,ind=0,IN7=0,IN8=0;}
#define uchar unsigned char
#define uint unsigned int
unsigned char pwm_val_left = 0;//變量定義
unsigned char push_val_left =14;//舵機(jī)歸中,產(chǎn)生約,1.5MS 信號
unsigned long S=0;
unsigned long S1=0;
unsigned long S2=0;
unsigned long S3=0;
unsigned long S4=0;
unsigned int time=0; //時(shí)間變量
unsigned int timer=0; //延時(shí)基準(zhǔn)變量
unsigned char timer1=0; //掃描時(shí)間變量
/************************************************************************/
//延時(shí)函數(shù)
void delay(unsigned int k)
{
unsigned int x,y;
for(x=0;x<k;x++)
for(y=0;y<2000;y++);
}
/************************************************************************/
//前速前進(jìn)
void run(void)
{
left_go;
right_go;
}
//前速后退
void backrun(void)
{
left_back;
right_back;
}
//左轉(zhuǎn)
void leftrun(void)
{
left_back;
right_go; ;
}
//右轉(zhuǎn)
void rightrun(void)
{ left_go;
right_back;
}
//STOP
void stoprun(void)
{
left_stop;
right_stop;
}
/************************************************************************/
/************************************************************************/
void StartModule() //啟動測距信號
{
TRIG=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
TRIG=0;
}
/***************************************************/
void Conut(void) //計(jì)算距離
{
while(!ECHO); //當(dāng)RX為零時(shí)等待
TR0=1; //開啟計(jì)數(shù)
while(ECHO); //當(dāng)RX為1計(jì)數(shù)并等待
TR0=0; //關(guān)閉計(jì)數(shù)
time=TH0*256+TL0; //讀取脈寬長度
TH0=0;
TL0=0;
S=(time*1.7)/100; //算出來是CM
}
/************************************************************************/
void COMM( void )
{
push_val_left=5; //舵機(jī)向左轉(zhuǎn)90度
timer=0;
while(timer<=4000); //延時(shí)400MS讓舵機(jī)轉(zhuǎn)到其位置
StartModule(); //啟動超聲波測距
Conut(); //計(jì)算距離
S2=S;
push_val_left=23; //舵機(jī)向右轉(zhuǎn)90度
timer=0;
while(timer<=4000); //延時(shí)400MS讓舵機(jī)轉(zhuǎn)到其位置
StartModule(); //啟動超聲波測距
Conut(); //計(jì)算距離
S4=S;
push_val_left=14; //舵機(jī)歸中
timer=0;
while(timer<=4000); //延時(shí)400MS讓舵機(jī)轉(zhuǎn)到其位置
StartModule(); //啟動超聲波測距
Conut(); //計(jì)算距離
S1=S;
if((S2<20)||(S4<20)) //只要左右各有距離小于,20CM小車后退
{
backrun(); //后退
timer=0;
while(timer<=4000);
}
if(S2>S4)
{
rightrun(); //車的左邊比車的右邊距離小 右轉(zhuǎn)
timer=0;
while(timer<=4000);
}
else
{
leftrun(); //車的左邊比車的右邊距離大 左轉(zhuǎn)
timer=0;
while(timer<=4000);
}
}
/*調(diào)節(jié)push_val_left的值改變電機(jī)轉(zhuǎn)速,占空比 */
void pwm_Servomoto(void)
{
if(pwm_val_left<=push_val_left)
Sevro_moto_pwm=1;
else
Sevro_moto_pwm=0;
if(pwm_val_left>=200)
pwm_val_left=0;
}
/***************************************************/
///*TIMER1中斷服務(wù)子函數(shù)產(chǎn)生PWM信號*/
void time1()interrupt 3 using 2
{
TH1=(65536-100)/256; //100US定時(shí)
TL1=(65536-100)%256;
timer++; //定時(shí)器100US為準(zhǔn)。在這個(gè)基礎(chǔ)上延時(shí)
pwm_val_left++;
pwm_Servomoto();
}
/************************************************************************/
/*********************************************************************/
/*--主函數(shù)--*/
void main(void)
{
stoprun();
TMOD=0X11;
TH1=(65536-100)/256; //100US定時(shí)
TL1=(65536-100)%256;
TH0=0;
TL0=0;
TR1= 1;
ET1= 1;
ET0= 1;
EA = 1;
delay(100);
push_val_left=14; //舵機(jī)歸中
while(1) /*無限循環(huán)*/
{
if(timer>=1000) //100MS檢測啟動檢測一次
{
timer=0;
StartModule(); //啟動檢測
Conut(); //計(jì)算距離
if(S<20) //距離小于20CM
{
stoprun(); //小車停止
COMM(); //方向函數(shù)
}
else
if(S>30) //距離大于,30CM往前走
run();
}
}
}
#include<reg52.h>
#include <intrins.h>
sbit Sevro_moto_pwm = P2^7; //接舵機(jī)信號端輸入PWM信號調(diào)節(jié)速度
sbit ECHO= P0^7; //超聲波接口定義
sbit TRIG=P0^6; //超聲波接口定義
sbit ina=P1^0;
sbit IN1=P1^1;
sbit IN2=P1^2;
sbit inb=P1^5;
sbit IN3=P1^3;
sbit IN4=P1^4;
sbit inc=P0^0;
sbit IN5=P0^1;
sbit IN6=P0^2;
sbit ind=P0^5;
sbit IN7=P0^3;
sbit IN8=P0^4;
#define left_go {inc=1,IN5=1,IN6=0,inb=1,IN3=1,IN4=0;}//P1:0-3控制左邊 前
#define left_back {inc=1,IN5=0,IN6=1,inb=1,IN3=0,IN4=1;} //后
#define left_stop {inc=0,IN5=0,IN6=0,inb=0,IN3=0,IN4=0;} //停止
#define right_go {ina=1,IN1=1,IN2=0,ind=1,IN7=1,IN8=0;}//P1:4-7控制右邊
#define right_back {ina=1,IN1=0,IN2=1,ind=1,IN7=0,IN8=1;} //
#define right_stop {ina=0,IN1=0,IN2=0,ind=0,IN7=0,IN8=0;}
#define uchar unsigned char
#define uint unsigned int
unsigned char pwm_val_left = 0;//變量定義
unsigned char push_val_left =14;//舵機(jī)歸中,產(chǎn)生約,1.5MS 信號
unsigned long S=0;
unsigned long S1=0;
unsigned long S2=0;
unsigned long S3=0;
unsigned long S4=0;
unsigned int time=0; //時(shí)間變量
unsigned int timer=0; //延時(shí)基準(zhǔn)變量
unsigned char timer1=0; //掃描時(shí)間變量
/************************************************************************/
//延時(shí)函數(shù)
void delay(unsigned int k)
{
unsigned int x,y;
for(x=0;x<k;x++)
for(y=0;y<2000;y++);
}
/************************************************************************/
//前速前進(jìn)
void run(void)
{
left_go;
right_go;
}
//前速后退
void backrun(void)
{
left_back;
right_back;
}
//左轉(zhuǎn)
void leftrun(void)
{
left_back;
right_go; ;
}
//右轉(zhuǎn)
void rightrun(void)
{ left_go;
right_back;
}
//STOP
void stoprun(void)
{
left_stop;
right_stop;
}
/************************************************************************/
/************************************************************************/
void StartModule() //啟動測距信號
{
TRIG=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
TRIG=0;
}
/***************************************************/
void Conut(void) //計(jì)算距離
{
while(!ECHO); //當(dāng)RX為零時(shí)等待
TR0=1; //開啟計(jì)數(shù)
while(ECHO); //當(dāng)RX為1計(jì)數(shù)并等待
TR0=0; //關(guān)閉計(jì)數(shù)
time=TH0*256+TL0; //讀取脈寬長度
TH0=0;
TL0=0;
S=(time*1.7)/100; //算出來是CM
}
/************************************************************************/
void COMM( void )
{
push_val_left=5; //舵機(jī)向左轉(zhuǎn)90度
timer=0;
while(timer<=4000); //延時(shí)400MS讓舵機(jī)轉(zhuǎn)到其位置
StartModule(); //啟動超聲波測距
Conut(); //計(jì)算距離
S2=S;
push_val_left=23; //舵機(jī)向右轉(zhuǎn)90度
timer=0;
while(timer<=4000); //延時(shí)400MS讓舵機(jī)轉(zhuǎn)到其位置
StartModule(); //啟動超聲波測距
Conut(); //計(jì)算距離
S4=S;
push_val_left=14; //舵機(jī)歸中
timer=0;
while(timer<=4000); //延時(shí)400MS讓舵機(jī)轉(zhuǎn)到其位置
StartModule(); //啟動超聲波測距
Conut(); //計(jì)算距離
S1=S;
if((S2<20)||(S4<20)) //只要左右各有距離小于,20CM小車后退
{
backrun(); //后退
timer=0;
while(timer<=4000);
}
if(S2>S4)
{
rightrun(); //車的左邊比車的右邊距離小 右轉(zhuǎn)
timer=0;
while(timer<=4000);
}
else
{
leftrun(); //車的左邊比車的右邊距離大 左轉(zhuǎn)
timer=0;
while(timer<=4000);
}
}
/*調(diào)節(jié)push_val_left的值改變電機(jī)轉(zhuǎn)速,占空比 */
void pwm_Servomoto(void)
{
if(pwm_val_left<=push_val_left)
Sevro_moto_pwm=1;
else
Sevro_moto_pwm=0;
if(pwm_val_left>=200)
pwm_val_left=0;
}
/***************************************************/
///*TIMER1中斷服務(wù)子函數(shù)產(chǎn)生PWM信號*/
void time1()interrupt 3 using 2
{
TH1=(65536-100)/256; //100US定時(shí)
TL1=(65536-100)%256;
timer++; //定時(shí)器100US為準(zhǔn)。在這個(gè)基礎(chǔ)上延時(shí)
pwm_val_left++;
pwm_Servomoto();
}
/************************************************************************/
/*********************************************************************/
/*--主函數(shù)--*/
void main(void)
{
stoprun();
TMOD=0X11;
TH1=(65536-100)/256; //100US定時(shí)
TL1=(65536-100)%256;
TH0=0;
TL0=0;
TR1= 1;
ET1= 1;
ET0= 1;
EA = 1;
delay(100);
push_val_left=14; //舵機(jī)歸中
while(1) /*無限循環(huán)*/
{
if(timer>=1000) //100MS檢測啟動檢測一次
{
timer=0;
StartModule(); //啟動檢測
Conut(); //計(jì)算距離
if(S<20) //距離小于20CM
{
stoprun(); //小車停止
COMM(); //方向函數(shù)
}
else
if(S>30) //距離大于,30CM往前走
run();
}
}
}
|
|