iam back again
in wrote this program to do what i mentioned above but the RB1 is high/low randomly don't know whats the problem
here i have divided my_Time by 58 to obtain the distance in cm
as
Distance = Velocity(speed)*time
speed of sound in air is approx 340m/s and the time measured in uS hence i derived the constant 58
somebody please tell me whats wrong with this program
Code:
#include <htc.h>
#define _XTAL_FREQ 10000000 //10MHz xtal speed
__CONFIG(LVPDIS & BORDIS & PWRTEN & WDTDIS & HS);
unsigned int sensorRead()
{
unsigned int SENSOR = 0;//variable used to combine two 8 bit variables into 1 16 bit variable
TRISC2 = 0; //RC2 output
RC2 = 1; //set RC2 high
__delay_us(5); //for 5uS
TRISC2 = 1; //RC2 input
TMR1H = 0; //clear timer 1
TMR1L = 0;
T1CON = 0b00110000; //init timer 1
while(!RC2); //wait until RC2 goes high via echo pulse
TMR1ON = 1; //start timer 1
CCP1CON = 0x04; //enable capture module, capture every falling edge of RC2/CCP1
while(!CCP1IF); //wait for falling edge on RC2
CCP1CON = 0x00; //CCP module off
CCP1IF = 0; //clear CCP interrupt flag
TMR1ON = 0; //stop timer 1
SENSOR = (CCPR1H + CCPR1L); // 1:8 prescaler ie 3.2uS so minimum value 115uS/3.2uS = 36 (approx)
return SENSOR;
}
void main(void)
{
PORTB=0; //clear portB
TRISB=0; //PORTB as output
unsigned int my_Time; //variable for storing the time to calculate distance
my_Time=sensorRead();
unsigned int my_Distance= my_Time/58;
while(1)
{
if(my_Distance > 30 && my_Distance < 50)
{
RB1=1;
}
else
RB2=1;
}
}
in wrote this program to do what i mentioned above but the RB1 is high/low randomly don't know whats the problem
here i have divided my_Time by 58 to obtain the distance in cm
as
Distance = Velocity(speed)*time
speed of sound in air is approx 340m/s and the time measured in uS hence i derived the constant 58
somebody please tell me whats wrong with this program