#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[5];
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[0]=distance_data/1000+0x30;
send_data[1]=distance_data%1000/100+0x30;
send_data[2]=distance_data%100/10+0x30;
send_data[3]=distance_data%10+0x30;
send_data[4]='
';
uart_send_string(send_data,5);
}
}
}
void extern0() interrupt 0
{
succeed_flag=1;
H=TH0;
L=TL0;
close_exti();
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>