原理圖:
/*******************************************************************************
* 文件名稱:main.c
* 說明:本文件為小車控制的主函數(shù)
* 功能:1.看門狗定時器初始化
2.調(diào)用函數(shù),進(jìn)行小車直流電機(jī)的開環(huán)控制
*******************************************************************************/
#include? <AT89X52.h>
#include? <kongzhi.h>
/********************************************************
*函數(shù)名稱:void main()
*函數(shù)功能:調(diào)用函數(shù)
*函數(shù)入口:無
*函數(shù)出口:無
********************************************************/
void main()
{
WDT_CONTR=0x3b;
timer_init();????????????????? //初始化
while(1)
{
keyboard();????????? //鍵盤掃描
// jieshou();??????????? //遙控信號檢測,要與傳感器實物連接才能正常使用.
xunji();????????????????? //循跡檢測
bizhang();???????????? //避礙物檢測
stop();?????????????????? //目標(biāo)站點檢測
WDT_CONTR=0x3b;? ?????? //喂狗
}
}
/*******************************************************************************
* 文件名稱:kongzhi.c
* 說明:本文件為小車控制的c文件
* 功能:1.檢測障礙物傳感器查詢
2.小車的狀態(tài)控制
*******************************************************************************/
#include? <AT89X52.h>
#define???? kongzhi_GLOBAL 1
#include? <kongzhi.h>
uchar????? PWM_t,PWM_t0;??????????????? //PWM_t為脈沖寬度(0~100)時間為0~10ms
uchar????? time_count;?????? ???????????//定時計數(shù)
uint?????? number,number1;
uchar????? flag;??????????????? //flag為小車狀態(tài)標(biāo)志,0:停止,1:前進(jìn),2:后退
uchar????? flag2;????????????? //flag2為小車狀態(tài)恢復(fù)寄存區(qū),當(dāng)小車遇到障礙物停止,等到障礙物排除之后,小車在停止之前的狀態(tài)就寄存在flag2中
uchar????? flag3;????????????? //flag3為小車避障停止延時標(biāo)志,初始化為0,等待障礙排除之后賦值為1,開始計時,數(shù)秒之后恢復(fù)停止前的行進(jìn)狀態(tài)
uchar????? flag4;???? //--flag4為看門狗復(fù)位后狀態(tài)恢復(fù)寄存區(qū)
uchar????? kind = 0;???????? //傳感器的種類
/********************************************************
*函數(shù)名稱:void bizhang()
*函數(shù)功能:遇到障礙物時的處理函數(shù)
*函數(shù)入口:無
*函數(shù)出口:無
********************************************************/
void bizhang()
{
zhang_ai = 1;
if(zhang_ai == 0)
{
flag2=flag;
flag=0;
while(zhang_ai == 0)
{
keyboard();
xunji();
stop();
WDT_CONTR=0x3b;
}
flag3=1;
}
}
/********************************************************
*函數(shù)名稱:void stop()
*函數(shù)功能:到達(dá)目標(biāo)點的控制函數(shù)
*函數(shù)入口:無
*函數(shù)出口:無
********************************************************/
void stop()
{
stop_1=1;????????????????????????????????? ? //目標(biāo)站點1
if(stop_1==0)flag=0;
stop_2=1;????????????????????????????????? ? //目標(biāo)站點2
if(stop_2==0)flag=0;
stop_3=1;????????????????????????????????? ? //目標(biāo)站點3
if(stop_3==0)flag=0;
}
/*******************************************************
*函數(shù)名稱:void timer_init()
*函數(shù)功能:定時器初始化設(shè)置
*函數(shù)入口:無
*函數(shù)出口:無
********************************************************/
void timer_init()
{
flag=0;flag3=0;
key = 0xFF;
TMOD = 0x20;???????????????????????????????????????????????????? //定時器1為模式2(8位自動重裝)
TF1 = 0;??????????????????????????????????????????????????????? //清中斷標(biāo)志
TH1 = timer_data;???????????????????????????????????? //保證定時時長為0.1ms
TL1 = TH1;
ET1 = 1;??????????????????????????????????????????????????????? //允許T0中斷
TR1 = 1;?????????????????????????????????????????????????????? //開始計數(shù)
EA = 1;????????????????????????????????????????????????????????? //中斷允許
ENA = 0;?????????????????????????????????????????????????????? //電機(jī)禁止
PWM_t = PWM_t0;?????????????????????????????????????????? //看門狗復(fù)位后速度恢復(fù)
flag = flag4;??????????????????????????????????????????????? //看門狗復(fù)位后小車狀態(tài)恢復(fù)
}
/*******************************************************
*函數(shù)名稱:void IntTime1() interrupt 3 using 2
*函數(shù)功能:定時器中斷處理,小車行走控制
*函數(shù)入口:無
*函數(shù)出口:無
********************************************************/
void IntTime1()interrupt 3????? using??????? 2
{
time_count++;
if(flag3)number++;
if(number==6000){number=0;number1++;}
if(number1==8){flag3=0;number1=0;flag=flag2;}
if(flag==0)ENA=0;???????????????????????????? //小車停止,flag=0,小車停止標(biāo)志
if(flag==1)?????????????????????????????????????????????????? //小車前進(jìn),flag=1,小車前進(jìn)標(biāo)志
{
ENA=1;???????????????????????????????????????????????????????? //PWM波輸出使能
DR=0;?????????????????????????????????????????????????????????? //直流電機(jī)一端接低電平
DR2=0;???????????????????????????????????????????????????????? //直流電機(jī)一端接低電平
if(kind==0)?????????????????????????????????????????????????? //沒有傳感器檢測到黑線,走直線
{?????? if(time_count<PWM_t)
{PWM=1;PWM2=1;}
else
{PWM=0;PWM2=0;}
}
if(kind==1)?????????????????????????????????????????????????? //左邊第一個傳感器檢測到黑線
{
if(time_count<(PWM_t-20))
{PWM=1;}
else
{PWM=0;}
if(time_count<PWM_t)
{PWM2=1;}
else
{PWM2=0;}
}
if(kind==2)?????????????????????????????????????????????????? //左邊第二個傳感器檢測到黑線
{
if(time_count<3)
{PWM=1;}
else
{PWM=0;}
if(time_count<PWM_t)
{PWM2=1;}
else
{PWM2=0;}
}
if(kind==3)?????????????????????????????????????????????????? //右邊第一個傳感器檢測到黑線
{
if(time_count<(PWM_t-20))
{PWM2=1;}
else
{PWM2=0;}
if(time_count<PWM_t)
{PWM=1;}
else
{PWM=0;}
}
if(kind==4)?????????????????????????????????????????????????? //右邊第二個傳感器檢測到黑線
{
if(time_count<3)
{PWM2=1;}
else
{PWM2=0;}
if(time_count<PWM_t)
{PWM=1;}
else
{PWM=0;}
}
}
if(flag==2)?????????????????????????????????????????????????? //小車倒退,flag=2,小車倒退標(biāo)志
{
ENA=1;
PWM=0;
PWM2=0;
if(kind==0)?????????????????????????????????????????????????? //沒有傳感器檢測到黑線,走直線
{?????? if(time_count<PWM_t)
{DR=1;DR2=1;}
else
{DR=0;DR2=0;}
}
if(kind==1)?????????????????????????????????????????????????? //左邊第一個傳感器檢測到黑線
{
if(time_count<(PWM_t-20))
{DR=1;}
else
{DR=0;}
if(time_count<PWM_t)
{DR2=1;}
else
{DR2=0;}
}
if(kind==2)?????????????????????????????????????????????????? //左邊第二個傳感器檢測到黑線
{
}
if(time_count >= PWM_T)????????? ? ??? //每輸出一個PWM波計數(shù)器清一次零,也即PWM波周期長度
{
time_count=0;
}
}