#include<stdio.h>
#include <string.h>
#include"uart.h"
#define _XTAL_FREQ 20000000
#define trigger RD2
#define echo RD3
void port_init()
{
TRISA1=0;
TRISD3=0XFF; //echo
TRISD2=0X00; //trigger
T1CON = 0x80;
TMR1IF = 0;
TMR1=0;
}
void delay(unsigned int de)
{
unsigned int maxde,minde;
for(maxde=0;maxde<de;maxde++)
for(minde=0;minde<453;minde++);
}
void Delay10Us()
{
int dCnt;
for(dCnt=0;dCnt<3;dCnt++);
}
void trigger_US()
{
trigger=1;
__delay_us(10);
trigger=0;
__delay_us(10);
while(echo == 0);
TMR1=0;
TMR1ON=1;
while(echo == 1 && !TMR1IF);
Time=TMR1;
TMR1ON=0;
Distance = ((float)Time/117.00);
}
void main()
{
port_init();
port1_init();
UART_init();
while(1)
{
RA1=1; //led on
delay(2000);
RA1=0; // led off
delay(2000);
trans_string("\n test"); // test uart
trigger_US();
sprintf(Total_distance,"%.03f",Distance);
trans_string(Total_distance);
}
}