Hi again,
Im using the ADC with 12 bits, single measure, 4MHz. CPU speed 24Mhz. TPM1 to interrupt every 1ms.
LSB=732uV
Im in a trouble. Im planning to show the values that the ADC converts to digital values. I first measure power line voltage, later the current of the phase line, and then i calculate the instantaneous power and accumulate that power to have the energy. Every sine cycle i calculate th RMS voltage (Vrms) and the RMS current (Irms). I defined the variables of Vrms and Irms like "signed long" (That's 32 bit variable), i dont know to use the floating type variables so i decided to do this way.
What im doing is something like this:
*-**-**--**-*-*-*-
static unsigned long vrms,irms,buffer_energy,cont_unit_energy,max_unit;
static signed long inst_power,;
//every 1ms the TPM1 makes a interruption
Measure Voltage1;
Measure Current;
Measure Voltage2;
Voltage_mean=voltage1+voltage2>>1; //to calc the mean voltage v_m=(v1+v2)/2
voltage_mean-=offset; //substract offset value 1.5Volts=2048 LSBs
current-=offset;
voltage_mean*=gain_voltage; //The gain that the sense circuit has
curretn*=gain_current;
voltage_pow2=voltage_mean*voltage_mean; //the sum of voltage^2 of samples
current_pow2=current*current;
if(completed_one_sine_cycle){
vrms=sqrt(voltage_pow2/numberofsamples_percycle); //calculating the RMS voltage
irms=sqrt(current_pow2/numberofsamples_percycle)
}
inst_power=voltage_mean*current;
buffer_energy+=inst_power;
if(buffer_energy>max_unit){
cont_unit_energy++;
buffer_energy-=max_unit;
}
-*-*-**-**-*-*-
That's what im doing.....I want to show the "Vrms", "irms" and the "cont_unit_energy" in the LCD.
* Irms is a long number....i was planning of first calculate how much digits in decimal it has, then show 5 Arms (Amperes RMS), 0.5 Arms, the MAX value would be 9 Arm.
* Vrms, the same, but with a MAX value of 135 Vrms.
* Cont_unit_energy, it's a counter and i think im going to make a lot of "IF" statements and display somethng like this...
10 Wh, 100Wh 1KWh, and a MAX value of 1000KWh
Thanks and appreciate your help!!!!!!!!!!!!!!!!!
I forgot to specify:
I don't use floating point variables because i don't know about that. Then i have to do this:
* The ADC gives me the number of step (LSB) that the analog input has.
If i have 1.5 in my analog input then i would have an output of my ADC of 2048 in decimal.
* That value is multiply by the gain of the sense circuit that acquire the power line signal.
2048*100=204800
* Then i multiply the 204800*204800 to have the voltage^2.
The result is 41.943.040.000
----------------------------->THAT'S AN ERROR! because my signed long supports max 2.147.483.648
I wil continue to expose what im using....but it's wrong! please help me!
* There's a accumulator that storage the voltage^2 of each sample every sine cycle...
voltage_pow2+=voltage_prom^2
* Then I calc the Vrms and Irms
vrms=sqrt(voltage_pow2/numberofsamples_percycke)
* My real value would be:
vrms=vrms*LSB;
This is one of the variables that i want to display on LCD.
The method that im using is wrong! do you know anything that can help me to do this efficiently?
Thanks!