cancel
Showing results for 
Search instead for 
Did you mean: 

issue on ADC value left align

Lzhiy.1
Associate II

I've designed an FOC driver borad using STM32F302.

The ADC that samples the resistor current is set left-align in the generated code by MotorControl Workbench 5.4.7.

Does ADC have to be left-aligned?Due to this,the ADC value is always out of bound

1 ACCEPTED SOLUTION

Accepted Solutions
cedric H
ST Employee

In FOC, you have to read positive and negative current. It is also true for single shunt implementation. Your Opamp output must be polarized at 1.65V when no current is present. The PhaseOffset is done for that purpose and should be in the middle of the dynamic.

Your issue does not come from the left alignment but by the usage of int16_t . If you consider that you will sample always positive current, then you can make use of uint16_t instead of int16_t, then your 50000 can be stored.

Cedric

View solution in original post

3 REPLIES 3
cedric H
ST Employee

Hello @Lzhiy.1​ ,

ADC value by itself can not be out of bound because of the left alignment.

ADC is 12 bits. you can safely store its value on a 16 bit register by adding 0000.

Now, you have to take care that if you do so, your full scale is now 0xFFF0 or 65520.

Regards

Cedric

in fuction R1F30X_GetPhaseCurrents(single shunt,the same as the three shunt code),the code directly read the ADC value which has a 4 bit left-shift,and check saturation.But int16 ranges from -32768 to 32767.

The op-amp circuit I design makes sure the sample value ranges from 0~4096.For example,the ADC value is 3125,and left shifts 4 bit,the ADCx->JDR1 = 3125 * 16 = 50000.It's out of bound in this function. So I don't know why it's cofigured to left alignment.

wAux = ( int32_t )( ADCx->JDR1 );
wAux -= ( int32_t )( pHandle->PhaseOffset );
 
  /* Check saturation */
  if ( wAux > -INT16_MAX )
  {
    if ( wAux < INT16_MAX )
    {
    }
    else
    {
      wAux = INT16_MAX;
    }
  }
  else
  {
    wAux = -INT16_MAX;
  }

cedric H
ST Employee

In FOC, you have to read positive and negative current. It is also true for single shunt implementation. Your Opamp output must be polarized at 1.65V when no current is present. The PhaseOffset is done for that purpose and should be in the middle of the dynamic.

Your issue does not come from the left alignment but by the usage of int16_t . If you consider that you will sample always positive current, then you can make use of uint16_t instead of int16_t, then your 50000 can be stored.

Cedric