Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] can anyone understand this code

Status
Not open for further replies.

Mohamed Slama

Member level 4
Member level 4
Joined
Nov 8, 2013
Messages
72
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
443
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

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;
    }
}
 

It is already in 'procedures'.

The variable declaration is under the section marked "// variables"
The edge detection is in the section called "// FUNCION INTERRUPCION"
The conversion to distance is in the section marked "void Calcular_Distancia2()"

The variable 'capture' is used to signal when a period has been measured.

Brian.
 

It is already in 'procedures'.

The variable declaration is under the section marked "// variables"
The edge detection is in the section called "// FUNCION INTERRUPCION"
The conversion to distance is in the section marked "void Calcular_Distancia2()"

The variable 'capture' is used to signal when a period has been measured.

Brian.

thnk you

but i dont no what is the purpose of tOld , tNew or how do measure the distance ?
 

As far as i understand the code is doing these ->

1. capture type of pin of cpu is set to rising edge initially!

2. when a rising edge occurs, the code in interrupt gets the timer value to tOld by the line in code "tOld = 256*CCPR1H + CCPR1L;" and also sets the type to falling edge.

3. when a falling edge occurs,the code in interrupt gets the timer value to tNew by the line in code "tOld = 256*CCPR1H + CCPR1L;" and also sets the capture variable to 1.

4. In step 3 as the capture variable is set to 1, Calcular_Distancia2 function now starts running.
In this function tWord is calculated in line "tWord = tNew-tOld ;"
So this means tWord holds the timer value for rising edge to falling edge of the signal.

Than tWord(the time of signal) is converted to a meaningful distance by the lines,
temps =((float)tWord);
distance = ((float)temps*17*2)/1000;
distance=(int)distance/100;

5. Also in Calcular_Distancia2 function the cpu is re- set to rising edge. So this cycle from 1 to 5 is repeated forever.

Thats all it does really.

But "if(distance3 !=distance4)" control statement is useless, because these variables are never modified in the code you supplied.
 
thank u toooo much adelmon

but when the rising edge is occurred the will from what to what ?

from rising(0----1) or from rising to falling ? and when the timer stop , the line that force timer to stop ?
 

thank u toooo much adelmon

but when the rising edge is occurred the will from what to what ?

from rising(0----1) or from rising to falling ? and when the timer stop , the line that force timer to stop ?


The signal is measured from rising (1) to falling(0) edges according to code. So the code measures the time that signal is at logic 1 state.

The line that forces the timer interrupt to stop is " PIE1.CCP1IE = 0; " in Calcular_Distancia2 function. (actually timer is not stopped. only the interrupt of timer is disabled)

but this is temporary until the distance calculations are done.

After the calculations are done the timer interrupt is restarted with the line "PIE1.CCP1IE = 1;" in Calcular_Distancia2 function.


Just a hint -> " PIE1.CCP1IE = 0; " line should have been at interrupt function at the place where "capture = 1;" is set. Setting " PIE1.CCP1IE = 0; " in Calcular_Distancia2 function is not correct and may create strange behaviour in distance calculations. Because until the interrupt is disabled cpu will always call interrupt function on rising and falling edges. So after a valid time value of signal is measured you should disable further updates of timer values, until already obtained signal time is processed.
 
thank u adelmon toooooooo much your comment make me understand :)
thanks a lot my friend
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top