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.

Problem int32 over flow

Status
Not open for further replies.

raman00084

Full Member level 6
Joined
Nov 29, 2010
Messages
362
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,298
Activity points
4,006
Im having the problem in reading the adc for the ac signal
it is working for the 800mv above the 800mv
im reading only 800mv.

i think the problem in int32 overflow due to the squaring the int16 adc value.
im using ccs c complier for the pic18f
is there any option to use 64bit multiplication in ccs for 8 bit microcontroller

following is the code.


Code:
float read_adc_AC(unsigned char adc_inChannel,int16 samples,unsigned int16 in_usADCzero)
{
   unsigned int16 usADCValue = 0 ;// usAccADC = 0;
   unsigned int16 usLoop = 0 ;
    int32 ulAccuADCvalue = 0;
    int32 tmpadc = 0;
   float ulTemp = 0;
   float ftemp = 0;

   set_adc_channel( adc_inChannel);
 for(usLoop = 0 ; usLoop <samples; usLoop ++)
   {
     
      usADCValue = read_adc() - in_usADCzero ;
    ulAccuADCvalue += (int32)(usADCValue*usADCValue);
   delay_us(155);

   }
  ulTemp = sqrt(ulAccuADCvalue /samples);
 
//!   return (ulTemp*(4.880)) ; 
return (ulTemp) ; 
  
}
 

the above code is used for the ac current measurement on hall effect sensor acs712/20A
 

I assume, the ADC values have only 12 significant bits, so you won't need 64 bit arithmetic.

Your code has however a "popular" fault. It's squaring a 16-bit value with 16 bit result. Review a C programming text book about type conversion rules in case of doubt.
Code:
(int32)(usADCValue*usADCValue);
must be changed into
Code:
((int32)usADCValue*usADCValue);
 
Hi,

An 32 bit overflow doesn't result in a fixed valud, it is wrapped around instead.

So I don't think it is an 32 bit overflow.

Klaus
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top