Continue to Site

Welcome to EDAboard.com

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

[SOLVED] problem in multiplication after AD conversion

Status
Not open for further replies.

ghattas.akkad

Member level 1
Member level 1
Joined
Apr 30, 2013
Messages
39
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Visit site
Activity points
1,555
hello i am using p18f4520 to design a watt meter
after reading the voltage at AN0 and AN1 i was able to display them separately on the LCD
but whenever i multiply them together
the displayed value is always 0 i don't understand why

this is the code i am using
each acquisition ResultV and ResultI can be displayed separately but when multiplying to InstP it shows 0

Code:
while (1)
	{	

	Delay100TCYx(4);
	count=count+1;

	ADCON0 = 0b11000001; 										//  A/D on, RC clock, select RA0
	ADCON0bits.GO = 1;											//  start A/D conversion
	while (ADCON0bits.NOT_DONE);								//  wait until A/D conversion is complete
	ResultV = ((unsigned short long) 250*ADRES+125+256)/512;


	ADCON0 = 0b11000101; 										//  A/D on, RC clock, select RA1
	ADCON0bits.GO = 1;											//  start A/D conversion
	while (ADCON0bits.NOT_DONE);								//  wait until A/D conversion is complete
	ResultI = ((unsigned short long) 250*ADRES+125+256)/512;	
	
	InstP = ((unsigned short long)(ResultV * ResultI));


	//if (count==100){
	Bin2AscE(InstP, Digits);									//  convert voltage to ASCII
	DispVarStr(&Digits[2], Ln2Ch6, 1);							//  display digit before DP
	DispVarStr(&Digits[3], Ln2Ch8, 2);							//  display 2 digits after DP
	//count=0;
	//InstP=0;
 

Attachments

  • PowerMeter.zip
    25.9 KB · Views: 86
Last edited:

Not knowing which compiler you are using I might be wrong but is 'ADRES' returning the right value, the PIC has two ADC result registers ADRESH and ADRESL, are you sure ADRES is returning their combined value.

You probably also want to use a floating point variable for the ResultV and ResultI calculations, I'm not sure what a "short long" represents. What data type is "InstP"?

Brian.
 
InstP is unsigned int
ADRES is returning the correct value since i am using 10bit conversion and right justification
when i display the ResultV and ResultI separately it shows the correct values but only goes to 0 when adding the multiply statement

- - - Updated - - -

Not knowing which compiler you are using I might be wrong but is 'ADRES' returning the right value, the PIC has two ADC result registers ADRESH and ADRESL, are you sure ADRES is returning their combined value.

You probably also want to use a floating point variable for the ResultV and ResultI calculations, I'm not sure what a "short long" represents. What data type is "InstP"?

Brian.
i tried replacing the ResultV*ResultI with ResultV+ResultI and the answer is shown correctly
the problem is with the multiplication but i dont know why
i am using c18 mplab compiler
this is a code version of voltage and Amps measurement is displayed simultaneously
the problem is whenever i use the multiply

- - - Updated - - -

ok fixed it apparently i was multiplying the ADRES conversion formula by 10 and the multiplier overflowed so i divided the resulting AD conversion results by 10 and it worked fine
 

Attachments

  • AV simultan.zip
    25.8 KB · Views: 98
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top