Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

applying equations on ADC samples in mikroC

Status
Not open for further replies.

hafizimran18

Member level 4
Joined
Aug 10, 2012
Messages
69
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,779
i want to apply these given equations in mikroC, please help me out Untitledk.png
 

EC.Engineer

Junior Member level 1
Joined
Apr 14, 2010
Messages
19
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,434
If you are taking V, Itotal and Ifundamental as analog inputs, then you can easily apply those equations directly in mikroC. Its all straight forward.


Code:
unsigned int V, Itotal, Ifund;
unsigned long Swith_harmonic;
double Iharmonic, Qharmonic, distAngle, TrueActPwr ;


V = ADC_Read(channel_V); // channel_V is the analog channel where you give the V input;
Itotal = ADC_Read(channel_It);
Ifund = ADC_Read(channel_If);

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

hafizimran18

Member level 4
Joined
Aug 10, 2012
Messages
69
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,779
but what about scaling? when input is at peak, adc value will be maximum i.e 1024 and thus calculations will be no more in 2 bytes i.e unsigned int type, what about that?
If you are taking V, Itotal and Ifundamental as analog inputs, then you can easily apply those equations directly in mikroC. Its all straight forward.


Code:
unsigned int V, Itotal, Ifund;
unsigned long Swith_harmonic;
double Iharmonic, Qharmonic, distAngle, TrueActPwr ;


V = ADC_Read(channel_V); // channel_V is the analog channel where you give the V input;
Itotal = ADC_Read(channel_It);
Ifund = ADC_Read(channel_If);

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);
- - - Updated - - -

but what about scaling? when input is at peak, adc value will be maximum i.e 1024 and thus calculations will be no more in 2 bytes i.e unsigned int type, what about that?
i found this thing some helpfull ,, but still there is a problem,, when using simpe multiplications i.e p=v*i;
it gives exactly 25watt power with d.c source of 5V and 5A current,, but when same source is applied using these equations it do not give 25watt,, please help,, i am in trouble

Code:
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);
}
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top