#include<p18f1330.h>
#include<delays.h>
//#include <stdio.h>
//#include <stdlib.h>
#include <math.h>
#pragma config OSC=INTIO2
#pragma config WDT=OFF
#pragma config PWMPIN = OFF
#define Trig PORTBbits.RB3
#define Echo PORTBbits.RB2
void Call_Init_PIC (void);
void sendPulse(void);
void T0Delay10us(void);
void delay2(void);
unsigned int distance ;
unsigned int time;
void main(void)
{
OSCCONbits.IRCF2 = 1;
OSCCONbits.IRCF1 = 1;
OSCCONbits.IRCF0 = 0;
TRISAbits.TRISA0=0;
TRISAbits.TRISA1=0;
Call_Init_PIC ();
while(1)
{
//T1CON=0x0;
TMR1H= 0;
TMR1L= 0;
sendPulse();
while(Echo == 0);
T1CONbits.TMR1ON = 1;
while(Echo == 1);
T1CONbits.TMR1ON = 0;
//Delay10KTCYx(255);
time= TMR1H;
time= time<<8;
time= time|TMR1L;
distance = (.0017* time);
Delay10KTCYx(100);
PIR1bits.TMR1IF=0;
/*
time = (TMR1L | (TMR1H<<8)); //Reads Timer Value
time = time/58.82; //Converts Time to Distance
time = time + 1; //Distance Calibration\
if(time>=2 && time<=6)
{
PORTBbits.RB0=1;
Delay10KTCYx(255);
PORTBbits.RB0=0;
Delay10KTCYx(255);
}
*/
//if(time>=7)
//{
//PORTBbits.RB1=1;
//}
if(distance<=100)
{
LATAbits.LATA0=1;
Delay10KTCYx(100);
LATAbits.LATA0=0;
Delay10KTCYx(100);
}
if(distance>=101)
{
LATAbits.LATA0=0;
Delay10KTCYx(100);
LATAbits.LATA1=1;
}
}
}
void Call_Init_PIC (void)
{
TRISBbits.TRISB3=0;
TRISBbits.TRISB2=1;
}
void sendPulse(void)
{
Trig=1;
Delay10TCYx(1);
Trig=0;
Delay10KTCYx(255);
Delay10KTCYx(255);
}
/*
void T0Delay10us()
{
T0CON=0x00;
TMR0H=0XFF;
TMR0L=0XCD;
T0CONbits.TMR0ON=1;
while(INTCONbits.TMR0IF==0);
T0CONbits.TMR0ON=0;
INTCONbits.TMR0IF=0;
}
void delay2()
{
Delay1TCY();
Delay1TCY();
Delay1TCY();
Delay1TCY();
Delay1TCY();
}
*/