3 line code snippet explaination

Status
Not open for further replies.

P.Copper

Member level 5
Joined
Mar 18, 2013
Messages
82
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
1,807
Hi all,

can anyone please explain to me what does these 3 lines of code mean. thanks
Code:
  for (i=0; i<75; i++)


    V_peak = (V_peak > V) ? V_peak : V;
    V_rms = 0.299*(V_peak - 825);

V is output from ADC
 
Last edited:

Hi all,

can anyone please explain to me what does these 3 lines of code mean. thanks
Code:
  for (i=0; i<75; i++)


    V_peak = (V_peak > V) ? V_peak : V;
    V_rms = 0.299*(V_peak - 825);
explanation:
V_peak = (V_peak > V) ? V_peak : V;
will be executed 75 times.
What this line means to is,
if(V_peak > V)
V_peak = V_peak;
else
V_peak = v;

In 3RD line,
V_rms = 0.299*(V_peak - 825);
V_rms is calculated from a derived formula. I guess it's an ADC calculation
 

If code is
Code:
 for (i=0; i<75; i++) ;
this is just a loop created to make a delay but better way to create the same code as -
Code:
 for (i=0; i<75; i++)
{
}

the next line code like used is Value = condition ? Value1 : Value2 operator this mean if( condition is true) the use Value = Value1 other wise Value = Value2
In your case it seem that it is used to find out the peak value of voltage -

the third line is V_rms = 0.299*(V_peak - 825); which use the V_peak from above operation and computes the RMS value .....my guess here is 8.25 had come because I think the circuit that you are using have shift for that many count in ADC value..... So I think the circuit that you are using have some kind of level shifting implemented in the hardware mostly using OPAMP....

Kindly please note that I am assuming there is a gap after for loop and semicolon after for loop...... other wise it means differently as explained in previous mathespbe post.....

Good Luck
 
Last edited:

The first 2 lines will get the highest value of V and put it in V-Peak. This repeats 75 times.

The 3rd line calculates a value base on this peak. The equation and numbers used (0.299 and 825) are clearly very application specific, so can't comment on this. Except that the variable is called V_rms, so somehow it might be an Rms voltage calculation.

- - - Updated - - -

If code is
Code:
 for (i=0; i<75; i++) ;
this is just a loop created to make a delay but better way to create the same code as -
Code:
 for (i=0; i<75; i++)
{
}

you're misinterpreting the code again Milind. There's no " ; " after the for.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…