|
/* 超聲波模塊的TRIG管腳接在單片機(jī)的P24口,ECHO管腳接在單片機(jī)的P25口 其他的電源管腳接在單片機(jī)
開發(fā)板上電源口即可。 */
#include <reg51.h>
#include <intrins.h>
unsigned long S=0;
unsigned int timer=0;
unsigned char posit=0;
bit flag =0;
//--定義使用的IO--//
//--定義全局變量--//
/*******************************************************************************
* 函 數(shù) 名 : DigDisplay
* 函數(shù)功能 : 使用數(shù)碼管顯示
* 輸 入 : 無
* 輸 出 : 無
*******************************************************************************/
void go1();
void hui1();
void down();
void DigDisplay();
void StartModule();
void comm();
/********************************************************/
void Conut(void);
/********************************************************/
void zd0() interrupt 1 //T0中斷用來計數(shù)器溢出,超過測距范圍
{
flag=1; //中斷溢出標(biāo)志
}
/********************************************************/
void zd3() interrupt 3 //T1中斷用來掃描數(shù)碼管和計800MS啟動模塊
{
TH1=0xf8;
TL1=0x30;
DigDisplay();
timer++;
}
/*********************************************************/
void main(void)
{
TMOD=0x11; //設(shè)T0為方式1,GATE=1;
TH0=0;
TL0=0;
TH1=0xf8; //2MS定時
TL1=0x30;
ET0=1; //允許T0中斷
ET1=1; //允許T1中斷
TR1=1; //開啟定時器
EA=1; //開啟總中斷
while(1)
{
if(timer>=100)
{
timer=0;
StartModule();
Conut(); //計算
if(S<30)
{
down();
comm();
}
else if(S>30)
go1();
}
}
}
下面是csb.c文件
#include "csb.h"
#include <intrins.h>
#define uchar unsigned char//宏定義
#define uint unsigned int//宏定義
#define aa 35//左輪速度調(diào)節(jié)值
#define bb 35//右輪速度調(diào)節(jié)值
sbit y=P2^1;//右轉(zhuǎn)向燈
sbit z=P2^2;//左轉(zhuǎn)向燈
sbit you1=P0^3;//右輪正轉(zhuǎn)信號端
sbit you2=P0^2;//右輪倒轉(zhuǎn)信號端
sbit zuo1=P0^1;//左輪正轉(zhuǎn)信號端
sbit zuo2=P0^0; //左輪倒轉(zhuǎn)信號端
sbit RX=P2^5;
sbit TX=P2^4;
unsigned long S1=0;
unsigned long S2=0;
unsigned long S3=0;
unsigned int time=0;
#define GPIO_DIG P0
sbit LSA=P2^4;
sbit LSB=P2^5;
sbit LSC=P2^6;
unsigned char code DIG_CODE[17]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//0、1、2、3、4、5、6、7、8、9、A、b、C、d、E、F的顯示碼
unsigned char disbuff[4] ={ 0,0,0,0,};
void delay(uint z)//一個帶參數(shù)的延時程序
{
int i,j;//定義兩個變量
for(i=10;i>0;i--)
for(j=z;j>0;j--);//將參數(shù)z賦值給j
}
void go1()//前進(jìn)子程序
{
zuo1=1;
zuo2=0;//左邊輪子正轉(zhuǎn)
// delay(aa);//pwm有用值,越大左邊方向的輪子越快
// zuo2=1;//關(guān)閉左邊輪子
you1=1;
you2=0; //右邊輪子正轉(zhuǎn)
// delay(bb);//pwm有用值,越大右邊的輪子越快
// you2=1;//關(guān)閉右邊輪子
// delay(100);//pwm無用值,越大,小車跑的越慢
z=1;
y=1;//關(guān)閉轉(zhuǎn)向燈
}
void hui1()//小車后退的子程序
{
zuo1=0;
zuo2=1;//左邊輪子倒轉(zhuǎn)
// delay(aa);//pwm有用值,越大左邊方向的輪子越快
// zuo1=1;//關(guān)閉左邊輪子
you1=0;
you2=1; //右邊輪子倒轉(zhuǎn)
// delay(bb);//pwm有用值,越大右邊的輪子越快
// you1=1;//關(guān)閉右邊輪子
// delay(100);//pwm無用值,越大,小車跑的越慢
z=1;
y=1;//關(guān)閉轉(zhuǎn)向燈
}
void turnleft()//小車左轉(zhuǎn)彎子程序
{
z=~z;//左轉(zhuǎn)向燈打開
zuo1=1;
zuo2=1;//左邊輪子停止轉(zhuǎn)動
you1=1;
you2=0; //右邊輪子正轉(zhuǎn)
// delay(bb);//pwm有用值,越大右邊的輪子越快
// you2=1;//右邊輪子停止
// delay(50);//pwm無用值,越大,小車跑的越慢
//
// z=1;
// y=1;//關(guān)閉轉(zhuǎn)向燈
}
void turnright()//小車右轉(zhuǎn)彎子程序
{
y=~y;//右轉(zhuǎn)向燈打開
zuo1=1;
zuo2=0;//左邊輪子正轉(zhuǎn)
// delay(aa);//pwm有用值,越大左邊方向的輪子越快
// zuo2=1;//左邊輪子停止
you1=1;
you2=1; //右邊輪子停止
// delay(50);//pwm無用值,越大,小車跑的越慢
// z=1;
// y=1;//關(guān)閉轉(zhuǎn)向燈
}
void down()//停止子程序
{
zuo1=1;
zuo2=1;
you1=1;
you2=1; //左右輪全部停止
z=1;
y=1;//關(guān)閉轉(zhuǎn)向燈
}
void DigDisplay()
{
unsigned char i;
unsigned int j;
for(i=0;i<8;i++)
{
switch(i) //位選,選擇點(diǎn)亮的數(shù)碼管,
{
case(0):
LSA=0;LSB=0;LSC=0; break;//顯示第0位
case(1):
LSA=1;LSB=0;LSC=0; break;//顯示第1位
case(2):
LSA=0;LSB=1;LSC=0; break;//顯示第2位
case(3):
LSA=1;LSB=1;LSC=0; break;//顯示第3位
case(4):
LSA=0;LSB=0;LSC=1; break;//顯示第4位
case(5):
LSA=1;LSB=0;LSC=1; break;//顯示第5位
case(6):
LSA=0;LSB=1;LSC=1; break;//顯示第6位
case(7):
LSA=1;LSB=1;LSC=1; break;//顯示第7位
}
GPIO_DIG=disbuff[i];//發(fā)送段碼
j=10; //掃描間隔時間設(shè)定
while(j--);
GPIO_DIG=0x00;//消隱
}
}
void Conut(void)
{
while(!RX); //當(dāng)RX為零時等待
TR0=1; //開啟計數(shù)
while(RX); //當(dāng)RX為1計數(shù)并等待
TR0=0; //關(guān)閉計數(shù)
time=TH0*256+TL0;
TH0=0;
TL0=0;
S= (long)(time*0.17); //算出來是CM
if((S>=4000)||flag==1) //超出測量范圍顯示“CHAO”
{
flag=0;
disbuff[0]=0x3f; //“-”
disbuff[1]=0x77; //“-”
disbuff[2]=0x76; //“-”
disbuff[3]=0x39; //“-”
}
else
{
disbuff[3]=DIG_CODE[S%10000/1000];
disbuff[2]=DIG_CODE[S%1000/100];
disbuff[1]=DIG_CODE[S%100/10];
disbuff[0]=DIG_CODE[S%10/1];
}
}
void StartModule() //啟動測距信號
{
TX=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
TX=0;
}
void comm()
{
turnleft();
timer=0;
while(timer==2000);//等待400ms
StartModule();
Conut();
S1=S;
turnright();
timer=0;
while(timer==4000);//等待800ms
StartModule();
Conut();
S2=S;
turnleft();
timer=0;
while(timer==2000);//等待400ms
StartModule();
Conut();
S3=S;
if((S1<30)&&(S2<30))//左右距離都小于30cm
{
hui1();
timer=0;
while(timer==2000);
}
if(S1>S2)
{
turnright();
while(timer==2000);
go1();
timer=0;
while(timer==2000);
}
else
{
turnleft();
while(timer==2000);
go1();
timer=0;
while(timer==2000);
}
}
|
|