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] Overflow Error , RMS value shown wrong .

Status
Not open for further replies.

Raady Here

Full Member level 5
Full Member level 5
Joined
Jun 8, 2013
Messages
242
Helped
26
Reputation
52
Reaction score
26
Trophy points
28
Location
India
Visit site
Activity points
1,571
Hi ,

I am measuring AC Voltage and I am calculating rms value for ADC.

I have an array which is having 128 samples of my signal.
while squaring number i get error.


Code:
        unsigned long int calval=0;
	unsigned int loop;
	float adcbufval;
	for(loop=0;loop<128;loop++)
	{
		printf("adcval %d = %d\t ",loop,adc_temp[loop]);
		calval = (adc_temp[loop])*(adc_temp[loop]);
		printf("\t %ld \n",calval);


               // calval += (adc_temp[loop])*(adc_temp[loop]);
               // adcbufval =(((sqrt(calval/samples))/4096)*OPV);
	       //printf("\n\r %f",adcbufval);
               //mesuredvoltage= adcbufval * fluke;        // fluke =249.62 as calculated
               // Printf("Volts = %f",measuredvolts);        // shown on UART.
	}


output :

adcval 1 = 168 28224
adcval 2 = 32 1024
adcval 3 = -88 7744
adcval 4 = -211 44521 // sqr(211) 44521 , it fine here
adcval 5 = -314 33060 // sqr(314) 98596-65536 = 33060 instead of 98596.
adcval 6 = -416 41984
adcval 7 = -522 10340
adcval 8 = -655 35809
adcval 9 = -773 7705
adcval 10 = -889 3889


Though I defined 'calval' as unsigned long int (range 0-4,294,967,295), it get over flowed at 65536 value.
In normal c complier its working fine.
Any suggestions, help.

Edit :
using dsPIC30f5011 / MPLAB 8.8x

Regards,
Raady.
 

You missed to tell the type of adc_temp[]. I guess it's signed int (16-Bit), which pretty explaines the problem.

Refer to a C text book regarding implicite type conversion. The type of a product is defined by the largest type involved in the multiplication. You need to type cast one multiplicand to 32-bit before.
 

Yes i have used 'adc_temp' as signed int ,
I have done as you mentioned.

Code:
for(loop=0;loop<128;loop++)
	{
		printf("adcval %d = %d\t\t",loop,adc_temp[loop]);
		calval = ((unsigned long int)(adc_temp[loop])*(adc_temp[loop]));
		printf("\t\t\t %ld \n",calval);

The output shows fine for positive numbers but negative numbers take the 2's complement,

adcval 88 = -47 -6158175
adcval 89 = -151 -19769071
adcval 90 = -240 -31399680
adcval 91 = -331 -43275271

Let me know where I was actually wrong !
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top