/*********************************************************************
光電開關循跡程序
*********************************************************************/
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar x3,x4;
uint i;
sbit P20=P1^2;//L298N使能端ENA?
sbit P21=P1^5;//L298N使能端ENB?
sbit P22=P1^0;//左電機A1? IN1
sbit P23=P1^1;//左電機A2? IN2
sbit P24=P1^3;//右電機B1? IN3
sbit P25=P1^4;//右電機B2? IN4
sbit P10=P3^6;//右邊探頭?
sbit P12=P3^7;//左邊探頭?
void delay_us(unsigned int aa)
{
while(aa--);
}
void ds(uchar x1,uchar x2)
{
TMOD=0X01;//定時器0,工作方式1??
TH0=x1;//賦高8位初值
TL0=x2;//賦低8位初值??
x3=x1;
x4=x2;//保存計數初值
EA=1;ET0=1;//開總中斷,開定時器0中斷
TR0=1;//定時器0開始計數?
}
void xunji()
{
if(P10==0&&P12==0)//左、右探頭都檢測到黑色
{
P22=1;
//停止???
P23=0;
P24=1;
P25=0;
}
if((P10==1&&P12==1))//全檢測不到黑線,前進
{
//ds(0xff,0xff);
//計數初值65535????????
P22=0;
P23=0;//全速前進
P24=0;
P25=0;
for(i=0;i<100;i++)
{
delay_us(5);
P20=~P20;
P21=~P21;
}
}
if((P10==0&&P12==1))
//右邊探頭檢測到黑線
{
ds(0xff,0xe1);
//計數初值65505??
}
if((P10==1&&P12==0))//右邊探頭檢測到黑線或右、中探頭檢測到黑線
{
ds(0xff,0xe1);//計數初值65505??
}
}
void main()
{
while(1)
{
xunji();
P20=1;
P21=1;//ENA和ENB同時為高電平時,L298N芯片正常工作
P22=1;
P23=1;
P24=1;
P25=1;
}
}
void time0()interrupt 1
{
TH0=x3;
TL0=x4;//重新賦計數初值
if((P10==1&&P12==1))//全檢測不到黑線,前進
{
P22=0;
P23=0;//全速前進
P24=0;
P25=0;
}
if((P10==0&&P12==1))//右邊探頭檢測到黑線,小車左轉
{
P22=1;
P23=0;//小車左拐???
P24=0;
P25=0;
}
if((P10==1&&P12==0))//左邊探頭檢測到黑線,小車右轉
{
P22=0;
P23=0;//小車右拐??
P24=1;
P25=0;
}
}
/***************************************************************************
2.
超聲波測距并用數碼管顯示程序
**********************************************************************/
#include <reg52.h>//器件配置文件
#include <intrins.h>
sbit RX=P1^1;//echo
sbit TX=P1^2;//trig
sbit DU= P2^6;//數碼管段選
sbit WE= P2^7;//數碼管位選
unsigned int time=0;
unsigned int timer=0;
unsigned char posit=0;
unsigned long S=0;
bit flag =0;
unsigned char const discode[] ={ 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F,0x6F,0x40,0x00/*-*/};
unsigned char const positon[3]={ 0xfe,0xfd,0xfb};
unsigned char disbuff[4]={ 0,0,0,0,};
/********************************************************/
void Display(void)//掃描數碼管
{
DU = 0;
if(posit==0)
{P0=(discode[disbuff[posit]])|0x80;}
else
{P0=discode[disbuff[posit]];}
DU = 1;
DU = 0;
WE = 0;
P0=positon[posit];
WE=1;
WE=0;
if(++posit>=3)
posit=0;
}
/********************************************************/
void Conut(void)
{
time=TH0*256+TL0;
TH0=0;
TL0=0;
S=(time*1.7)/100;//算出來是CM
if((S>=700)||flag==1) //超出測量范圍顯示“-”
{
flag=0;
disbuff[0]=10;//“-”
disbuff[1]=10;//“-”
disbuff[2]=10;//“-”
}
else
{
disbuff[0]=S%1000/100;
disbuff[1]=S%1000%100/10;
disbuff[2]=S%1000%10 %10;
}
}
/********************************************************/
void zd0() interrupt 1//T0中斷用來計數器溢出,超過測距范圍
{
flag=1;//中斷溢出標志
}
/********************************************************/
void zd3() interrupt 3//T1中斷用來掃描數碼管和計800MS啟動模塊
{
TH1=0xf8;
TL1=0xcd;
Display();
timer++;
if(timer>=400)
{
timer=0;
TX=1;//800MS??啟動一次模塊
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
TX=0;
}
}
/*********************************************************/
void main(void)
{
TMOD=0x11;//設T0為方式1,GATE=1;
TH0=0;
TL0=0;
TH1=0xf8;//2MS定時
TL1=0xcd;
ET0=1; //允許T0中斷
ET1=1;//允許T1中斷
TR1=1;//開啟定時器
EA=1;//開啟總中斷
while(1)
{
while(!RX); //當RX為零時等待
TR0=1;//開啟計數
while(RX);//當RX為1計數并等待
TR0=0;//關閉計數
Conut();//計算
}
}
|