汽车测距测速及倒车提示程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include <AT89X52.H>
unsigned char code dispcode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90, 0x88,0x83,0xc6,0xa1,0x86,0x8e,0x89,0xa3,0x8c,0xc8};//0-9,a-f
unsigned char dispcodex[]={0xc0,0xc0,0xc0,0xc0};// 初始数据
unsigned char dispbuf[4]={0,0,0,0};// 0x76,0x3f,0x3f,0x4f
unsigned char temp[4];
unsigned char dispcount;
unsigned int fs;
unsigned int fs1;
unsigned int fs2;
unsigned char i;
unsigned char j;
unsigned int x;
unsigned long l,time;
/*unsigned char k;
unsigned char temp;
unsigned char a,b;*/
//-----------------------定义管脚--------------------------------------------------------
#define timer_data (256-200) //定时器预置值,12M时钟时,定时0.1ms
#define PWM_T 100 //定义PWM的周期T为10ms
unsigned char PWM_t; //PWM_t为脉冲宽度(0~100)时间为0~10ms
unsigned char PWM_count; //输出PWM周期计数
unsigned char time_count; //定时计数
unsigned char timecount;
unsigned int INT0count;
unsigned char k3;
unsigned char zf;
unsigned char testok;
/*数码管控制*/
sbit W0=P1^0; //
sbit W1=P1^1; //
sbit W2=P1^2; //
sbit W3=P1^3; //位选
/*电机控制*/
sbit HW=P3^3; //测速
sbit PWM=P1^6; //电机控制PWM 波形输出
sbit M2=P1^7; //电机控制
sbit DJDY=P1^4; //电机继电器控制
//sbit PWM=P3^4; //PWM 波形输出
/*超声波定义*/
sbit FS=P3^4; //发射
sbit JS=P3^2; //接受
sbit CSDY=P1^5; //电源继电器
//sbit LS=P2^6; //蜂鸣器控制
/*开关定义*/
sbit K1=P2^3; //倒车键
sbit K2=P2^2; //减速键
sbit K3=P2^1; //加速键
sbit K4=P2^0; //正转键
//sbit K5=P3^7; //加强
bit hw;
bit k1; //按键标识
bit k2; //按键标识
bit k4; //按键标识
bit csxs;
bit flag;
bit dc;
void delay100us(void)
{
unsigned char i,j;
for(i=40;i>0;i--)
for(j=248;j>0;j--);
}
void csfs(void) //超声波发射程序//
{
unsigned char times=0;
while(TF1==0) //testok==1||testok==2//
{
unsigned char j;
for(j=10;j>0;j--);
FS=~FS;
times++; //翻转20次,发送10个脉冲信号的超声波// TF1=0;
if(times==10)
break;
}
times=0;
TH1=0x00;//(65536-4000)/256;
TL1=0x00;//(65536-4000)%256;
EX0=1;
TR1=1;
ET1=1; //开定时器//
delay100us();
//testok=0;
}
void main(void)
{
DJDY=0;
CSDY=0;
M2=1;
zf=0;
testok=0;
PWM=1;
PWM_t=100;
TMOD=0x12; /*定时器1为工作模式1,0为模式2(8位自动重装) */ T2MOD=0x00; /*定时器2为工作模式) */
T2CON=0x04; /*定时器2为自动重装定时工作模式) */
RCAP2L=(65536-4000)%256;
RCAP2H=(65536-4000)/256;
// TH2=0x16;
//TL2=0x56;
TH0=0x216; //保证定时时长为0.1ms
TL0=0x216;
TH1=0x00;//(65536-4000)/256;
TL1=0x00;//(65536-4000)%256;
IT0=1;
IT1=1;
EX0=1;
EX1=1;
TR2=1;
TR1=1;
TR0=1;
ET0=1;
ET1=1;
ET2=1;
EA=1;
while(1)
{
if(zf==0&&dc==0)
{
for(i=0;i<4;i++)
{
dispbuf[i]=0;
}
i=0;
}
if(zf==2&&k1==1)
{
csfs();
}
if(flag==1&&zf==1) {
flag=0;
x=INT0count;
for(i=0;i<4;i++)
{
temp[i]=0;
}
i=0;
while(x/10)
{
temp[i]=x%10;
x=x/10;
i++;
}
temp[i]=x;
dispbuf[3]=temp[0]; dispbuf[2]=temp[1]; dispbuf[1]=temp[2]; dispbuf[0]=temp[3];
// for(i=0;i<4;i++)
// {
// dispbuf[i]=temp[i]; // }
timecount=0;
INT0count=0;
//TH0=0;
//TL0=0;
//TR0=1;
}
if(testok==1&&zf==2) //
{
if(1==testok)
{
//delay100us();
testok=0;
x=l;
//delay100us();
// delay100us();
if(x)
{for(i=0;i<4;i++)
{
temp[i]=0;
} }
i=0;
while(x/10)
{
temp[i]=x%10;
x=x/10;
i++;
}
temp[i]=x;
dispbuf[3]=temp[0]; dispbuf[2]=temp[1]; dispbuf[1]=temp[2]; dispbuf[0]=temp[3]; // for(i=0;i<4;i++)
// {
// dispbuf[i]=temp[i]; // }
timecount=0;
INT0count=0;
//TH0=0;
//TL0=0;
//TR0=1;
if(l==20)
{
DJDY=0;
CSDY=0;
csxs=0;
PWM=1;
M2=1;
k1=0;
zf=0;
TR0=0;
dc=0;
testok=0;
}
}
}
testok=0;
if(K4==0&&k4==0) {
{
k4=1;
DJDY=1;
M2=1;
zf=1;
TR0=1;
dc=1;
}
while(K4==0);
}
else if(K4==0&&k4==1)
{
{
k4=0;
CSDY=0;
DJDY=0;
PWM_t=100;
M2=1;
zf=0;
TR0=0;
dc=0;
}
while(K4==0);
}
else if(K2==0&&k4==1&&k2==0) {
if(K2==0&&k4==1&&k2==0) {
k2=1;
DJDY=1;
PWM_t=65;
M2=1;
zf=1;
TR0=1;
dc=1;
}
while(K2==0);
}
else if(K2==0&&k4==1&&k2==1) {
if(K2==0&&k4==1&&k2==1) {
k2=0;
CSDY=0;
DJDY=0;
M2=1;
zf=0;
TR0=0;
dc=0;
}
while(K2==0);
}
else if(K3==0&&k4==1)
{
if(K3==0&&k4==1)
{
switch(k3)
{ case 0:DJDY=1;PWM_t=45;zf=1;TR0=1;dc=1;break;
case 1:DJDY=1;PWM_t=30;zf=1;TR0=1;dc=1;break;
case 2:DJDY=1;PWM_t=15;zf=1;TR0=1;dc=1;break;
case 3:DJDY=0;CSDY=0;PWM_t=100;k4=0;TR0=0;dc=0;zf=0;break; }
k3++;
if(k3==4)
{k3=0;
M2=1;
zf=0;
dc=0;
}
}
while(K3==0);
}
else if(K1==0&&k1==0)
{
{
DJDY=1;
CSDY=1;
csxs=1;
PWM=1;
M2=0;
k1=1;
TR0=0;
zf=2;
dc=1;
testok=1;
}
while(K1==0);
}
else if(K1==0&&k1==1)
{
{
DJDY=0;
CSDY=0;
csxs=0;
PWM=1;
M2=1;
k1=0;
zf=0;
TR0=0;
dc=0;
testok=0;
}
while(K1==0);
}
}
}
void int0(void) interrupt 0 using 0 //接收中断程序//
{
EX0=0;
TR1=0;//关定时器//
ET1=0;//中断关闭
time=TH1*256+TL1; //算出t的值,t的单位为us// RCAP2H l=time*180;
l=l/10000;
TH1=0x00;//(65536-4000)/256;
TL1=0x00;//(65536-4000)%256;
testok=1;
}
void int2(void) interrupt 3 using 0 //超时中断程序//
{
EX0=0;
TR1=0;//关定时器//
ET1=0;// 中断关闭
TH1=0x00;//(65536-4000)/256;
TL1=0x00;//(65536-4000)%256;
testok=2;
}
void t0(void) interrupt 1 using 0
{
time_count++;
if(time_count>=PWM_T)
{
time_count=0;
PWM_count++;
}
if(time_count<PWM_t)
PWM=1;
else
PWM=0;
}
void int1(void) interrupt 2 using 0
{
INT0count++;
}
void t1(void) interrupt 5 using 0 {
TF2=0;
// TH1=(65536-4000)/256;
//TL1=(65536-4000)%256;
//TH1=(65536-500)/256;
//TL1=(65536-500)%256;
// TR0=1;
// ET0=1;
timecount++;
if(timecount==250)
{
//TR0=0;
timecount=0;
flag=1;
}
P0=dispcode[dispbuf[dispcount]]; if (dispcount==0)
{
W0=0;
W1=1;
W2=1;
W3=1;
}
if(dispcount==1)
{
W0=1;
W1=0;
W2=1;
W3=1;
}
if(dispcount==2) {
W0=1;
W1=1;
W2=0;
W3=1;
}
if(dispcount==3) {
W0=1;
W1=1;
W2=1;
W3=0;
}
dispcount++;
if(dispcount==4) dispcount=0;
}。