unsigned int V, Itotal, Ifund;
unsigned long Swith_harmonic;
double Iharmonic, Qharmonic,a, distAngle, TrueActPwr ;
void main()
{
ADCON1 = 0x80; // Configure analog inputs and Vref
TRISA = 0xFF; // PORTA is input
TRISB = 0x3F; // Pins RB7, RB6 are outputs
TRISD = 0; // PORTD is output
do
{
V = ADC_Read(0); // channel_V is the analog channel where you give the V input;
Itotal = ADC_Read(1);
Ifund = ADC_Read(2);
V = (V%1024)*5;
Itotal = (Itotal%1024)*5;
Ifund = (Ifund%1024)*5;
Swith_harmonic = V * Itotal;
Iharmonic = sqrt((Itotal * Itotal) - (Ifund * Ifund)); // Select the C_Math library for using this function
Qharmonic = V * Iharmonic;
distAngle = asin(Qharmonic / Swith_harmonic);
TrueActPwr = Swith_harmonic * cos(distAngle);
PORTD = TrueActPwr;
} while(1);
}