This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Unsigned int long long valu get trancated

Other Parts Discussed in Thread: MSP430G2955

                  

    Hi..

       We have started our development using MSP430G2955 ,we have six anolog signals and we try to sample and accumulate the count using ADC10.The sampling rate is 625 uSec and the maximum sample count is 2000 samples. We used the 2.5v  Vreference and the signal level for ADC channel is 0.68V at full voltage (290VAC )/at full current (5A) . when we apply the full voltage (290VAC) then we didn’t get the expected output but upto 120VAC we got the  approx 120VAC. This behavior is same for the current, when we apply the full current (5A) then we didn’t get the expected output but upto 2.32A we got the approx value though ADC measurement.

   We have tried the same code on both CCS5.4 & IAR compiler and we also cross checked our hardware but there is no issue in our hardware .

                    In watch sometime we have observed the value of  isquare_temp variable  and value of  isq_sum_v1 variable get truncated but both the variables are unsigned  int long long and by actual calculation our value after squared and accumalated the 2000 samples ( if we consider the max value of v1_current_sample=511  and max samples =2000)  is 522242000 which is within the limit of unsined int long long.

Example: 

 full signal level ADC is 1023 .

LSHIFT=512;

v1_current_sample=1023-512;     ………..               (    v1_current_sample=v1_current_sample-LSHIFT)

v1_current_sample=511;

isquare_temp=(511*511);                                                                                                                            

isq_sum_v1+= isquare_temp;                            ( addition is 2000 times becauseour max sample count is 2000)

             

Pl refer the below code and kindly help to resolved our query  .       

///////////////////////////////////////////////////////////////////////// code

///////////////////////////////////////// variable definition in main

# define MAX_ADC_SAMPLE_COUNT    2000

unsigned  int long long isquare_temp=0;

unsigned int long long isq_sum_v1=0;

unsigned int long long isq_sum_v2=0;

 unsigned int long long isq_sum_i2=0;

 unsigned int long long isq_sum_v3=0;

 unsigned int long long isq_sum_i3=0;

unsigned int long long isq_sum_i1=0;

 

signed int i1_current_sample;

signed int i1_old_sample;

signed int i2_old_sample;

 signed int i3_old_sample;

 signed int v1_current_sample;

 signed int v2_current_sample;

 signed int v3_current_sample;

//////////////////////////////////////////////////////////////////////////

Note: first we Turn on the ADC in main

//////////////////////////////////

 

#pragma vector=ADC10_VECTOR

__interrupt void ADC10_ISR (void)

{

                  Sample_counter++;

v1_current_sample=ADCbuf[2];//  voltage Vr  samples

v2_current_sample=ADCbuf[3];// voltage Vy samples

v3_current_sample=ADCbuf[1];// voltage Vb  samples

i1_current_sample=ADCbuf[8];// current Ir samples;

i2_current_sample=ADCbuf[9];// current Iysamples;

i3_current_sample=ADCbuf[0];// current Ib samples;

////////////////////////////////////////////////////////////////

LSHIFT=512;

///////////////////////////////////////////////Remove Dc/////////////////////////

v1_current_sample=v1_current_sample-LSHIFT;

v2_current_sample=v2_current_sample-LSHIFT;

v3_current_sample=v3_current_sample-LSHIFT;

 

i1_current_sample=i1_current_sample-LSHIFT;

i2_current_sample=i2_current_sample-LSHIFT;

i3_current_sample=i3_current_sample-LSHIFT;

i2_current_sample=-i2_current_sample;

/////////////////////////////////////////////////// square

isquare_temp=(unsigned int long long ) (v1_current_sample*v1_current_sample);

isq_sum_v1+=isquare_temp;

       

                   isquare_temp=(unsigned int long long ) (v2_current_sample*v2_current_sample);

                   isq_sum_v2+=isquare_temp;

       

                  isquare_temp=(unsigned int long long ) (v3_current_sample*v3_current_sample);

                  isq_sum_v3+=isquare_temp;

       

       isquare_temp=(unsigned int long long ) (v1_current_sample-v2_current_sample)*(v1_current_sample-v2_current_sample);

       isq_sum_v12+=isquare_temp;

  

      isquare_temp=(unsigned int long long ) (v3_current_sample-v2_current_sample)*(v3_current_sample-v2_current_sample);

       isq_sum_v32+=isquare_temp;   

    

       isquare_temp=(unsigned int long long ) (v3_current_sample-v1_current_sample)*(v3_current_sample-v1_current_sample);

       isq_sum_v31+=isquare_temp;

                         

        isquare_temp = (unsigned int long long)((i1_current_sample)*(i1_current_sample));

        isq_sum_i1+=isquare_temp;

       

        isquare_temp=(unsigned int long long )((i2_current_sample)*(i2_current_sample));

        isq_sum_i2+=isquare_temp;

       

         isquare_temp=(unsigned int long long )((i3_current_sample)*(i3_current_sample));

        isq_sum_i3+=isquare_temp;      

      

         isquare_temp=(unsigned int long long )((i2_current_sample)*(i2_current_sample));

        isq_sum_i2+=isquare_temp;

       

         isquare_temp=(unsigned int long long )((i3_current_sample)*(i3_current_sample));

        isq_sum_i3+=isquare_temp;

 

 

ADC10CTL0 &=~ENC;                    // disable conversions

__no_operation();

ADC10CTL0 |=ENC;                    // Enable conversions

    If (isample_counter==(MAX_ADC_SAMPLE_COUNT))//MAx count

{

     TA0CCR0=0;                    //remember to make ADC timer off   

  // Note : we again turn ON this ADC timer  in main after done  the processing  on accumalted samples      

                                           

}

 

}

  • hhh ggg said:
    isquare_temp=(unsigned int long long ) (v1_current_sample*v1_current_sample);

    v1_current_sample is an integer, v1_current_sample*v1_current_sample the result will be also an integer, (unsigned int long long ) converts (truncated) integer result to long long. Write;

    isquare_temp = (unsigned int long long) ((unsigned int long long)v1_current_sample*(unsigned int long long)v1_current_sample);

  • hhh ggg said:
    isquare_temp=(511*511);          

    Since constants are short int by default, 511*511 is truncated to 16 bit, even if isquare_temp might be long and large enough for a 32 bit result. That's the C language standard.

    To get a long result from the multiplication, you'll have to turn at least one of the parameters into a long first (the compiler will then auto-cast the other to long too, giving a long result form the multiplication)

    isquare_temp = (511*511L)

  • Leo Bosch said:
    isquare_temp = (unsigned int long long) ((unsigned int long long)v1_current_sample*(unsigned int long long)v1_current_sample);

    Only one of the parameters needs to be cast to long long. The other one is automatically expanded and the result will also be long long then.

**Attention** This is a public forum