Mohamed Slama
Member level 4
this code is apart from ultrasonic code to detect the distance between SR-HC04 sensor and obstacle .
can anyone write the code in procedures such as
1- variable declration
2- detect rising edg
...... and so on
can anyone write the code in procedures such as
1- variable declration
2- detect rising edg
...... and so on
Code:
// variables
char txt1[] = "Auto driving ";
char txt2[] = "Car";
char txt3[] = "Distance in Cm = ";
char txt4[7];
char txt5[7];
char txt6[7];
unsigned tWord,tOld, tNew ;
char tl;
char edge = 0;
char capture = 0;
float temps = 0;
float distance = 0;
float distance3 = 0;
float distance5 = 0;
float distance2 = 0;
float distance4 = 0;
float distance6 = 0;
unsigned int Int_Distance1=0;
unsigned int Int_Distance2=0;
unsigned int Int_Distance3=0;
void interrupt() // FUNCION INTERRUPCION:
{
if(PIR1.CCP1IF)
{
if(!edge)
{
CCP1CON = 0x04; //Capture mode, every falling edge
tOld = 256*CCPR1H + CCPR1L;
edge = 1;
} else
{
tNew =256*CCPR1H + CCPR1L;
capture = 1;
edge = 0;
}
PIR1.CCP1IF = 0;
}
}
void Calcular_Distancia2()
{
if(capture)
{
PIE1.CCP1IE = 0;
capture = 0;
tWord = tNew-tOld ;
CCP1CON = 0x05; //Capture mode, every rising edge
temps =((float)tWord);
distance = ((float)temps*17*2)/1000;
distance=(int)distance/100;
Int_distance2=distance;
IntToStr(Int_distance2,txt5);
// floatToStr(temps,txt5);
// lcd_out(2,1,txt5);
Delay_ms(70);
PIR1.CCP1IF = 0;
PIE1.CCP1IE = 1;
if(distance3 !=distance4)
{
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,txt3);
Lcd_Out(2,1,txt5);
}
distance4 = distance3;
}
}