#include <p18f4580.h>
#include <usart.h>
#include <delays.h>
#include <stdio.h>
#include <stdlib.h>
#pragma config OSC= HS
#pragma config WDT=OFF
#pragma config LVP=OFF
void T0Delay10us(void);
void T0Delay5ms(void);
void uart(void);
void Call_Init_PIC (void);
void Call_Init_USART (void);
void uart(void);
#define Trig PORTCbits.RC0
//ECHO is TRISCbits.TRISC2=1; //CCP1 input pin
void main(void)
{
unsigned char a,b;
CCP1CON=0x05;
T3CON=0x0;
T1CON=0x0;
TRISCbits.TRISC2=1; //CCP1 input pin
CCPR1L=0;
CCPR1H=0;
while(1)
{
uart();
Call_Init_PIC ();
TMR1H=0;
TMR1L=0;
Trig=1;
T0Delay10us();
Trig=0;
PIR1bits.CCP1IF=0;
while(PIR1bits.CCP1IF=0);
T1CONbits.TMR1ON=1;
PIR1bits.CCP1IF=0;
while(PIR1bits.CCP1IF=0);
T1CONbits.TMR1ON=0;
a=CCPR1L;
b=CCPR1H;
while (1)
{
printf("ULTRA SONIC SENSOR = %5d",a);
printf("XXXXXX");
printf("ULTRA SONIC SENSOR = %5d",b);
Delay10KTCYx(255);
}
}
//PIR1bits.TMR1IF=0;
T0Delay5ms();
}
void Call_Init_PIC (void)
{
TRISCbits.TRISC2=0;
TRISCbits.TRISC0=0;
}
void uart(void)
{
TXSTA=0B00100000;
SPBRG=32;
TRISCbits.RC6=0;
TXSTAbits.TXEN=1;
RCSTAbits.SPEN=1;
}
void T0Delay10us()
{
T0CON=0x08;
TMR0H=0XFF;
TMR0L=0XCE;
T0CONbits.TMR0ON=1;
while(INTCONbits.TMR0IF==0);
T0CONbits.TMR0ON=0;
INTCONbits.TMR0IF=0;
}
void T0Delay5ms()
{
T0CON=0x08;
TMR0H=0X9E;
TMR0L=0X58;
T0CONbits.TMR0ON=1;
while(INTCONbits.TMR0IF==0);
T0CONbits.TMR0ON=0;
INTCONbits.TMR0IF=0;
}