超声测距模块,用51调,接受的数据始终不对啊,请各位大...
#include<STC12C5A60S2.h>#include"uart.h"
sbit T=P1^0;
sbit R=P3^2;
unsigned char succeed_flag,H,L;
unsigned int distance_data;
#define time_value 0x28
void delay_20us()
{
unsigned char a;
for(a=109;a>0;a--);
}
void ini_timer()
{
TMOD=0x01;
TR0=0;
}
void ini_exti()
{
EX0=0;
}
void open_timer()
{
TR0=1;
}
void open_exti()
{
EX0=1;
}
void close_timer()
{
TR0=0;
}
void close_exti()
{
EX0=0;
}
void light()
{
P1&=0x7f;
}
void main()
{
unsigned char send_data;
ini_timer();
ini_exti();
uart_ini(0xfd);
succeed_flag=0;
T=0;
EA=1;
uart_send_u8('7');
while(1)
{
TH0=0;
TL0=0;
T=1;
delay_20us();
T=0;
while(R==0);
open_exti();
open_timer();
while(TH0<time_value);
close_timer();
if(succeed_flag==1)
{
light();
distance_data=H;
distance_data<<=8;
distance_data|=L;
distance_data*=12;
distance_data/=58;
send_data=distance_data/1000+0x30;
send_data=distance_data%1000/100+0x30;
send_data=distance_data%100/10+0x30;
send_data=distance_data%10+0x30;
send_data='\n';
uart_send_string(send_data,5);
}
}
}
void extern0() interrupt 0
{
succeed_flag=1;
H=TH0;
L=TL0;
close_exti();
} 始终返回的都是两个时钟周期 本帖最后由 ypengfei 于 2014-11-6 21:12 编辑
succeed_flag不需要回0吗?
没明白你说什么意思。 if(succeed_flag==1)
{
light();
distance_data=H;
distance_data<<=8;
distance_data|=L;
distance_data*=12;
distance_data/=58;
send_data=distance_data/1000+0x30;
send_data=distance_data%1000/100+0x30;
send_data=distance_data%100/10+0x30;
send_data=distance_data%10+0x30;
send_data='\n';
uart_send_string(send_data,5);
}
里面第一件事应把 succeed_flag清零,不然就是一直是你现在测试到的两个周期祝你好运 yao2013lin 发表于 2014-11-6 21:21
if(succeed_flag==1)
{
light();
谢谢!!!!!!!!!!!!!!!!!!!!!!!
iridescent_rsy 发表于 2014-11-7 10:22
谢谢!!!!!!!!!!!!!!!!!!!!!!!
咦,改完之后结果还是一样啊,如果有 distance_data/=58;这条语句,就返回0,如果把那条语句注释掉,就是24或者36(之前除以个12,所以就是两个机器周期) ypengfei 发表于 2014-11-6 21:07
succeed_flag不需要回0吗?
没明白你说什么意思。
改过之后还是那样啊{:sad:} iridescent_rsy 发表于 2014-11-7 10:29
咦,改完之后结果还是一样啊,如果有 distance_data/=58;这条语句,就返回0,如果把那条语句注释掉,就是 ...
你发下你的程序看看就是我说的那个程序 #include<STC12C5A60S2.h>
#include"uart.h"
sbit T=P1^0;
sbit R=P3^2;
unsigned char succeed_flag,H,L;
unsigned int distance_data;
#define time_value 0x28
void delay_50us()
{
unsigned char a,b;
for(b=61;b>0;b--)
for(a=3;a>0;a--);
}
void ini_timer()
{
TMOD=0x01;
TR0=0;
}
void ini_exti()
{
EX0=0;
}
void open_timer()
{
TR0=1;
}
void open_exti()
{
EX0=1;
}
void close_timer()
{
TR0=0;
}
void close_exti()
{
EX0=0;
}
void light()
{
P1&=0x7f;
}
void main()
{
unsigned char send_data;
ini_timer();
ini_exti();
uart_ini(0xfd);
succeed_flag=0;
T=0;
EA=1;
while(1)
{
TH0=0;
TL0=0;
T=1;
delay_50us();
T=0;
while(R==0);
open_exti();
open_timer();
while(TH0<time_value);
close_timer();
if(succeed_flag==1)
{
succeed_flag=0;
light();
distance_data=H;
distance_data<<=8;
distance_data|=L;
distance_data*=12;
distance_data/=58;
send_data=distance_data/1000+0x30;
send_data=distance_data%1000/100+0x30;
send_data=distance_data%100/10+0x30;
send_data=distance_data%10+0x30;
send_data='\n';
uart_send_string(send_data,5);
}
}
}
void extern0() interrupt 0
{
succeed_flag=1;
H=TH0;
L=TL0;
close_exti();
} 我把高电平改成50us了 我用示波器看,接收端就是2~3us的高电平,难道是我的模块坏了{:sad:}
页:
[1]