AnsweredAssumed Answered

STM32F302 Nucleo issue using HALL with ADC in differential mode

Question asked by urlep.evgen on May 25, 2015
Hi,
Im using NUCLEO 32F302R8T. The source code is created using STM32CubeMx and adjusted for test purposes.
It seems that there I have an issue using A/D in the differential mode. The test is limited to test the channels ADC_CHANNEL_1 and ADC_CHANNEL_2. First the channels are configured as single ended mode , then recompiled for differential mode.
  In single ended mode the results are as expected, at Channel1 input voltage 1.66V gives 2049 kv , Channel2 input voltage 5mV gives  6kv.
 In the differential mode the results are at the same input voltage 3063 kv, i would expect that result would be around 2040 kv. What I am doing wrong. The sampling is continious with DMA.

Any idea would be greatly apprechiated.

best regards, Evgen


#include "adc.h"


#include "gpio.h"
#include "dma.h"


/* USER CODE BEGIN 0 */
#define ADC_NBR_OF_CONVERSION 5
uint32_t ADC1ConvertedValues[MAX_DMA_SIZE]; 
uint32_t ADC1Avg[ADC_NBR_OF_CONVERSION];
float  ADC1Values[ADC_NBR_OF_CONVERSION];


uint32_t t1,t2,dt;


/* USER CODE END 0 */


ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;


/* ADC1 init function */
void MX_ADC1_Init(void)
{
  ADC_ChannelConfTypeDef sConfig;


    /**Common config 
    */
  hadc1.Instance = ADC1;
  hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC;
  hadc1.Init.Resolution = ADC_RESOLUTION12b;
  hadc1.Init.ScanConvMode = ENABLE;
  hadc1.Init.ContinuousConvMode = ENABLE;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.NbrOfConversion = ADC_NBR_OF_CONVERSION;
  hadc1.Init.DMAContinuousRequests = DISABLE;
  hadc1.Init.EOCSelection = EOC_SINGLE_CONV;
  hadc1.Init.LowPowerAutoWait = DISABLE;
  hadc1.Init.Overrun = OVR_DATA_OVERWRITTEN;
  HAL_ADC_Init(&hadc1);


    /**Configure Regular Channel 
    */
  sConfig.Channel = ADC_CHANNEL_1;
  sConfig.Rank = 1;
  sConfig.SingleDiff =ADC_DIFFERENTIAL_ENDED; // ADC_SINGLE_ENDED; //;
  sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  sConfig.OffsetNumber = ADC_OFFSET_NONE;
  sConfig.Offset = 0;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig);




    /**Configure Regular Channel 
    */
  sConfig.Channel = ADC_CHANNEL_2;
  sConfig.Rank = 2;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig);


    /**Configure Regular Channel 
    */
  sConfig.Channel = ADC_CHANNEL_6;
  sConfig.Rank = 3;
     
  HAL_ADC_ConfigChannel(&hadc1, &sConfig);
     
    /**Configure Regular Channel 
    */
  sConfig.Channel = ADC_CHANNEL_7;
  sConfig.Rank = 4;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig);


    /**Configure Regular Channel 
    */
  sConfig.Channel = ADC_CHANNEL_11;
  sConfig.Rank = 5;
     
  HAL_ADC_ConfigChannel(&hadc1, &sConfig);


     hadc1.Instance->DIFSEL&=0x7ff;
     
     
      t1=SysTick->VAL;
  // HAL_ADCEx_Calibration_Start(&hadc1,ADC_DIFFERENTIAL_ENDED);
     HAL_ADCEx_Calibration_Start(&hadc1,ADC_SINGLE_ENDED); //
      t2=SysTick->VAL;
      if (t1<t2)
           dt=SysTick->LOAD+t2-t1;
      else
          dt=t1-t2;
      // is 928 us
}


void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
{


  GPIO_InitTypeDef GPIO_InitStruct;
  if(hadc->Instance==ADC1)
  {
  /* USER CODE BEGIN ADC1_MspInit 0 */


  /* USER CODE END ADC1_MspInit 0 */
    /* Peripheral clock enable */
    __ADC1_CLK_ENABLE();
  
    /**ADC1 GPIO Configuration    
    PC0     ------> ADC1_IN6
    PC1     ------> ADC1_IN7
    PA0     ------> ADC1_IN1
    PA1     ------> ADC1_IN2
    PA4     ------> ADC1_IN5
    PB0     ------> ADC1_IN11 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);


    GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);


    GPIO_InitStruct.Pin = GPIO_PIN_0;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);


    /* Peripheral DMA init*/
  
    hdma_adc1.Instance = DMA1_Channel1;
    hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
    hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
    hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
    hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
    hdma_adc1.Init.Mode = DMA_CIRCULAR;
    hdma_adc1.Init.Priority = DMA_PRIORITY_HIGH;
    HAL_DMA_Init(&hdma_adc1);


    __HAL_LINKDMA(hadc,DMA_Handle,hdma_adc1);


  /* USER CODE BEGIN ADC1_MspInit 1 */


  /* USER CODE END ADC1_MspInit 1 */
  }
}


void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
{


  if(hadc->Instance==ADC1)
  {
  /* USER CODE BEGIN ADC1_MspDeInit 0 */


  /* USER CODE END ADC1_MspDeInit 0 */
    /* Peripheral clock disable */
    __ADC1_CLK_DISABLE();
  
    /**ADC1 GPIO Configuration    
    PC0     ------> ADC1_IN6
    PC1     ------> ADC1_IN7
    PA0     ------> ADC1_IN1
    PA1     ------> ADC1_IN2
    PA4     ------> ADC1_IN5
    PB0     ------> ADC1_IN11 
    */
    HAL_GPIO_DeInit(GPIOC, GPIO_PIN_0|GPIO_PIN_1);


    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4);


    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0);


    /* Peripheral DMA DeInit*/
    HAL_DMA_DeInit(hadc->DMA_Handle);
  /* USER CODE BEGIN ADC1_MspDeInit 1 */


  /* USER CODE END ADC1_MspDeInit 1 */
  }



/* USER CODE BEGIN 1 */
void ADC_Start(void)
{
if (HAL_ADC_Start_DMA(&hadc1, (uint32_t*)ADC1ConvertedValues, MAX_DMA_SIZE) != HAL_OK)  
     return;
}

void MX_GPIO_Init(void)
{


  GPIO_InitTypeDef GPIO_InitStruct;


  /* GPIO Ports Clock Enable */
  __GPIOC_CLK_ENABLE();
  __GPIOF_CLK_ENABLE();
  __GPIOA_CLK_ENABLE();
  __GPIOB_CLK_ENABLE();


  /*Configure GPIO pin : PC13 */
  GPIO_InitStruct.Pin = GPIO_PIN_13;
  GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);


  /*Configure GPIO pins : PA2 PA3 */
  GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);


  /*Configure GPIO pin : PA5 */
  GPIO_InitStruct.Pin = GPIO_PIN_5;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);


}

void DMA1_Channel1_IRQHandler(void)
{
  /* USER CODE BEGIN DMA1_Channel1_IRQn 0 */


  /* USER CODE END DMA1_Channel1_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_adc1);
  /* USER CODE BEGIN DMA1_Channel1_IRQn 1 */
     ADC_Calc();
 // start the conversions again 
     ADC_Start();
 /* make calculations */
  /* USER CODE END DMA1_Channel1_IRQn 1 */
}

Outcomes