這是一個超聲波避障小車的源程式,可以參考下,用的89C52微控制器,舵機控制轉角避障。
#include
#define Sevro_moto_pwm P2_7 //接舵機訊號端輸入PWM訊號調節速度
#define ECHO P2_4 //超聲波介面定義
#define TRIG P2_5 //超聲波介面定義
#define Left_moto_go {P1_0=1,P1_1=0,P1_2=1,P1_3=0;} //左邊兩個電機向前走
#define Left_moto_back {P1_0=0,P1_1=1,P1_2=0,P1_3=1;} //左邊兩個電機向後轉
#define Left_moto_Stop {P1_0=0,P1_1=0,P1_2=0,P1_3=0;} //左邊兩個電機停轉
#define Right_moto_go {P1_4=1,P1_5=0,P1_6=1,P1_7=0;} //右邊兩個電機向前走
#define Right_moto_back {P1_4=0,P1_5=1,P1_6=0,P1_7=1;} //右邊兩個電機向前走
#define Right_moto_Stop {P1_4=0,P1_5=0,P1_6=0,P1_7=0;} //右邊兩個電機停轉
unsigned char const discode[] ={ 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xBF,0xff/*-*/};
unsigned char const positon[3]={ 0xfe,0xfd,0xfb};
unsigned char disbuff[4] ={ 0,0,0,0,};
unsigned char posit=0;
unsigned char pwm_val_left = 0;//變數定義
unsigned char push_val_left =14;//舵機歸中,產生約,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; //時間變數
unsigned int timer=0; //延時基準變數
unsigned char timer1=0; //掃描時間變數
/************************************************************************/
void delay(unsigned int k) //延時函式
{
unsigned int x,y;
for(x=0;x
這是一個超聲波避障小車的源程式,可以參考下,用的89C52微控制器,舵機控制轉角避障。
#include
#include
#define Sevro_moto_pwm P2_7 //接舵機訊號端輸入PWM訊號調節速度
#define ECHO P2_4 //超聲波介面定義
#define TRIG P2_5 //超聲波介面定義
#define Left_moto_go {P1_0=1,P1_1=0,P1_2=1,P1_3=0;} //左邊兩個電機向前走
#define Left_moto_back {P1_0=0,P1_1=1,P1_2=0,P1_3=1;} //左邊兩個電機向後轉
#define Left_moto_Stop {P1_0=0,P1_1=0,P1_2=0,P1_3=0;} //左邊兩個電機停轉
#define Right_moto_go {P1_4=1,P1_5=0,P1_6=1,P1_7=0;} //右邊兩個電機向前走
#define Right_moto_back {P1_4=0,P1_5=1,P1_6=0,P1_7=1;} //右邊兩個電機向前走
#define Right_moto_Stop {P1_4=0,P1_5=0,P1_6=0,P1_7=0;} //右邊兩個電機停轉
unsigned char const discode[] ={ 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xBF,0xff/*-*/};
unsigned char const positon[3]={ 0xfe,0xfd,0xfb};
unsigned char disbuff[4] ={ 0,0,0,0,};
unsigned char posit=0;
unsigned char pwm_val_left = 0;//變數定義
unsigned char push_val_left =14;//舵機歸中,產生約,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; //時間變數
unsigned int timer=0; //延時基準變數
unsigned char timer1=0; //掃描時間變數
/************************************************************************/
void delay(unsigned int k) //延時函式
{
unsigned int x,y;
for(x=0;x