Hello All,
I am currently implementing an emeter based on SLAA391. My voltage and current inputs are different from the ones presented in the app note, so the scale factors are different too.
The original code of SLAA391 presents macros for all scale factors ( V_RMS, I_RMS, Power, etc). I am trying to find out how to convert the my V_RMS scale factor to the value of the macro, but without success. I am saying that because I am calculating the RMS voltage and the result is different than the real value.
Basically, I am calculating my real scale factor and converting it to Q15, which I suppose is correct. Then I am calling the voltage() function.
int32_t voltage(struct phase_parms_s *phase)
{
int16_t scale;
int32_t x;
if ((phase->status & V_OVERRANGE))
return -1;
x = div_sh48(phase->V_sq_accum_logged, 26-2*ADC_BITS ,phase->sample_count_logged);
x = isqrt32(x);
scale = 14662;
x = (x >> 12) * scale;
x >>= 14;
return x;
}
Regarding the function, I honestly don't know how some parts work. I understand that the div_sh48 funcion shitfs left the accum_logged value and then devide the shifted value by sample_count_logged. But why shifting by 26-2*ADC_BITS (12 Bits)?
Other thing I don't understand is the right shifts (>>12 and >>14)? I assume they are made for precision purposes, since the isqrt() function outputs a 32 bits Q16.16 value, but I don't really understand the values.
Is there anyone that can explain these function and confirm if I am converting the scale factor correctly?
Thanks in advance,
Pedro Pinto