老铁们,超声波模块连在开发板上,都能测不出距离
时间:10-02
整理:3721RD
点击:
老铁们,帮忙看看,太感谢了
#include<reg52.h>
#include<intrins.h>
sbit RX = P3^2;//单片机接收回响信号
sbit TX = P3^3;//单片机输出启动超声波模块脉冲
sbit ADDR0 = P1^0;//LED开启引脚
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
sbit Led = P0^0; //(测试)
bit flags = 0; //超出测量范围标志
unsigned int S = 0; //距离
unsigned char code LedChar[11] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xBF}; //数码管显示字符
unsigned char LedBuff[4] = {0xff,0xff,0xff,0xff}; //数码管字符缓冲区
void count() //距离计算,同时为数码管字符缓冲区赋值
{
unsigned int time = 0;
time =( (TH0 - 0xD6) * 256 + (TL0 - 0x08)) / 2;
TH0 = 0xD5;
TL0 = 0xA5;
S = (time * 12 / 11059200) * 34000;
if(flags == 1)
{
flags = 0;
LedBuff[0] = LedChar[2]; //测试
LedBuff[1] = LedChar[6];
LedBuff[2] = LedChar[6];
LedBuff[3] = LedChar[6];
/* LedBuff[3] = LedChar[10];
LedBuff[2] = LedChar[10];
LedBuff[1] = LedChar[10];
LedBuff[0] = LedChar[10];
*/
}
else
{
LedBuff[0] = LedChar[S % 10];
LedBuff[1] = LedChar[S / 10 % 10];
LedBuff[2] = LedChar[S / 100 % 10];
LedBuff[3] = LedChar[S / 1000 % 10];
}
// TH0 = 0xD6;
// TL0 = 0x08;
}
void Display()//数码管显示距离
{
static unsigned char i = 0;
P0 = LedBuff[i] ;
P1 = (P1 & 0xF8) | i;
if(i < 3)
{
i ++;
}
else i = 0;
}
void Interrupt() interrupt 3 //中断1,用于扫描数码管,开启超声波模块
{
unsigned int timer = 0;
TH1 = 0xF8;
TL1 = 0x30;//定时2ms
timer ++;
Display();
if(timer >= 400)//定是达到800ms时,输出一次超声波模块开启脉冲
{
timer = 0;
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 Interrupt0() interrupt 1 //用T0对回响信号进行扫描
{
flags = 1; //超出了测距范围
TH0 = 0xD5;
TL0 = 0xA5;
// flags = 0; //(测试)
// TH0 = 0xD6;
// TL0 = 0x08;
/*ADDR2 = 1;
ADDR1 = 1;
ADDR0 = 0;
Led = 0;*/ //(测试)
}
void main()
{
ADDR3 = 1;
ENLED = 0;//使能数码管
TMOD = 0x11;
TR1 = 1;
TH1 = 0xf8;
TL1 = 0X30;
EA = 1;
ET1 = 1; //中断1打开
ET0 = 1; //中断0打开
// TH0 = 0xD6;
// TL0 = 0x08;
TH0 = 0xd5;
TL0 = 0xa5;
while(1)
{
while(!RX);//当为高电平时
TR0 = 1;
while(RX);//当为低电平时
TR0 = 0;
count();
}
}
#include<reg52.h>
#include<intrins.h>
sbit RX = P3^2;//单片机接收回响信号
sbit TX = P3^3;//单片机输出启动超声波模块脉冲
sbit ADDR0 = P1^0;//LED开启引脚
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
sbit Led = P0^0; //(测试)
bit flags = 0; //超出测量范围标志
unsigned int S = 0; //距离
unsigned char code LedChar[11] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xBF}; //数码管显示字符
unsigned char LedBuff[4] = {0xff,0xff,0xff,0xff}; //数码管字符缓冲区
void count() //距离计算,同时为数码管字符缓冲区赋值
{
unsigned int time = 0;
time =( (TH0 - 0xD6) * 256 + (TL0 - 0x08)) / 2;
TH0 = 0xD5;
TL0 = 0xA5;
S = (time * 12 / 11059200) * 34000;
if(flags == 1)
{
flags = 0;
LedBuff[0] = LedChar[2]; //测试
LedBuff[1] = LedChar[6];
LedBuff[2] = LedChar[6];
LedBuff[3] = LedChar[6];
/* LedBuff[3] = LedChar[10];
LedBuff[2] = LedChar[10];
LedBuff[1] = LedChar[10];
LedBuff[0] = LedChar[10];
*/
}
else
{
LedBuff[0] = LedChar[S % 10];
LedBuff[1] = LedChar[S / 10 % 10];
LedBuff[2] = LedChar[S / 100 % 10];
LedBuff[3] = LedChar[S / 1000 % 10];
}
// TH0 = 0xD6;
// TL0 = 0x08;
}
void Display()//数码管显示距离
{
static unsigned char i = 0;
P0 = LedBuff[i] ;
P1 = (P1 & 0xF8) | i;
if(i < 3)
{
i ++;
}
else i = 0;
}
void Interrupt() interrupt 3 //中断1,用于扫描数码管,开启超声波模块
{
unsigned int timer = 0;
TH1 = 0xF8;
TL1 = 0x30;//定时2ms
timer ++;
Display();
if(timer >= 400)//定是达到800ms时,输出一次超声波模块开启脉冲
{
timer = 0;
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 Interrupt0() interrupt 1 //用T0对回响信号进行扫描
{
flags = 1; //超出了测距范围
TH0 = 0xD5;
TL0 = 0xA5;
// flags = 0; //(测试)
// TH0 = 0xD6;
// TL0 = 0x08;
/*ADDR2 = 1;
ADDR1 = 1;
ADDR0 = 0;
Led = 0;*/ //(测试)
}
void main()
{
ADDR3 = 1;
ENLED = 0;//使能数码管
TMOD = 0x11;
TR1 = 1;
TH1 = 0xf8;
TL1 = 0X30;
EA = 1;
ET1 = 1; //中断1打开
ET0 = 1; //中断0打开
// TH0 = 0xD6;
// TL0 = 0x08;
TH0 = 0xd5;
TL0 = 0xa5;
while(1)
{
while(!RX);//当为高电平时
TR0 = 1;
while(RX);//当为低电平时
TR0 = 0;
count();
}
}
当连接GND的线断开时,就显示0000(应该是拔出时电平由高变低),导致测试时间为0。插入时,变为6662(插入时由低变高,定时器0溢出,)导致无法显示超出测量距离(即6662),所以,各位大神,是哪里错了呢。