cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F030 ADC frustration

matas
Associate II
Posted on October 28, 2015 at 14:57

Hello everybody, with this code, no matter what I do, I always get 4095 (max). I've tried looking at Clive examples or IAR's examples I just can't see it. Could someone take a look and see what am I missing. I need to measure the internal reference voltage and one external channel seemed simple enough.

<code>

/* ADC measurement */

&sharpinclude ''stm32f0xx.h''

&sharpdefine SAMPLES         2

&sharpdefine RECEIVE         1

&sharpdefine TRANSMIT        0

void GPIO_init();

void ADC1_init();

void init_DMA();

void delay(uint32_t time);

uint16_t ADC1ConvertedValue[SAMPLES] = {0};

uint16_t ADC1Average = 0;

uint8_t ADCCounter = 0;

uint8_t* ADCDataOut;

uint32_t VREFINT_CAL;

uint32_t tempADC;

uint16_t Vrefint_voltage;

uint32_t ADCVref_kvant;

int main()

{

  

  GPIO_init();

  init_DMA();

  ADC1_init();

  VREFINT_CAL = (*(uint16_t*)0x1FFFF7BA);

  Vrefint_voltage = 1542*3300/4095;

  

  while(1){

      

      while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);

 

    /* Get ADC1 converted data */

    ADC1ConvertedValue[0] = ADC_GetConversionValue(ADC1);

  }

  

}

void GPIO_init(){

  GPIO_InitTypeDef GPIO_InitStructure;

  

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

  

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  

   /* Configure USART1 pins:  Rx and Tx ----------------------------*/

  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_2 | GPIO_Pin_3;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);

  

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

}

void ADC1_init(){

  ADC_InitTypeDef ADC_InitStruct;

  

  ADC_DeInit(ADC1);

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

  

  ADC_StructInit(&ADC_InitStruct);

  

  ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;

  ADC_InitStruct.ADC_ContinuousConvMode = ENABLE;

  ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;

  ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;

  ADC_InitStruct.ADC_ScanDirection = ADC_ScanDirection_Upward;

  ADC_Init(ADC1, &ADC_InitStruct);

  

  ADC_ChannelConfig(ADC1, ADC_Channel_5, ADC_SampleTime_239_5Cycles);

  ADC_ChannelConfig(ADC1, ADC_Channel_Vrefint, ADC_SampleTime_239_5Cycles);

  ADC_VrefintCmd(ENABLE);

  ADC_GetCalibrationFactor(ADC1);

  

    /* ADC DMA request in circular mode */

  ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);

  

      /* Enable ADC_DMA */

  //ADC_DMACmd(ADC1, ENABLE);

    

  ADC_Cmd(ADC1, ENABLE);

  

  while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));

  

  ADC_StartOfConversion(ADC1);

  

}

void init_DMA(){

    

  NVIC_InitTypeDef NVIC_InitStruct;

  NVIC_InitStruct.NVIC_IRQChannel = DMA1_Channel1_IRQn;

  NVIC_InitStruct.NVIC_IRQChannelPriority = 0;

  NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStruct);

  

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

  

  DMA_InitTypeDef  DMA_InitStructure;

  DMA_DeInit(DMA1_Channel1);

  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;

  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;

  DMA_InitStructure.DMA_Priority = DMA_Priority_High;

  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;

  

  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;

  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;

  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;

  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;

  DMA_InitStructure.DMA_BufferSize = SAMPLES;

  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;

  

  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADC1ConvertedValue[0];

  DMA_Init(DMA1_Channel1, &DMA_InitStructure);

  DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);

 

  DMA_Cmd(DMA1_Channel1, ENABLE); //Enable the DMA1 - Channel1

}

void DMA1_Channel1_IRQHandler(void){

  if(DMA_GetITStatus(DMA1_IT_TC1)){

  

    DMA_ClearITPendingBit(DMA1_IT_TC1);

  }

}

void delay(uint32_t time){

  for(; time > 0; time--);

}

</code>

#stm32f3-adc-vrefint-temp_sensor
Foo Bar
7 REPLIES 7
matas
Associate II
Posted on October 28, 2015 at 18:32

Okay, the external channel seems to show some hope (added some delays between measurements in the DMA interrupt), but the Vrefint still 4095.

Foo Bar
ArturasV
Senior
Posted on October 29, 2015 at 23:40

Here i made this example for you. Tested and working on stm32f051R8 mcu. Hope this helps. 

http://pastebin.com/Z6t9LZV8

matas
Associate II
Posted on October 30, 2015 at 10:29

Hey, I found the problem last night, apparently on one of the pins, that was ''TTa'', which means only accepts 3.3V, there was a 5V drop. After I removed it the ADC started working just fine. Probably the voltage leaked into the ADC which then malfunctioned.

Foo Bar
bhcuong2008
Associate II
Posted on May 01, 2016 at 13:07

Hi,

I'm also facing this issue with board STM32F3 Discovery. Values from VREFINT and Temp Sensor are always 4095. I dont know why.

Anyone who knows this issue, please help me.

Thank you very much.

Posted on May 01, 2016 at 17:51

When your question deviates significantly off-topic, into a different part, and many months ago, you should open a new thread.

Have you actually enabled the Vref/Temp sources?  Show complete and concise code example demonstrating your problem.

You want to find people who don't and haven't had this problem.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
bhcuong2008
Associate II
Posted on May 01, 2016 at 19:45

Thank you for your guidance. I will create another thread for STM32F3 about my issue.

I use CUBEMX and HAL drivers that already enables 2 bit VREFEN and TSEN in function HAL_ADC_ConfigChannel. I will post my code in that thread.

bhcuong2008
Associate II
Posted on May 01, 2016 at 20:29

Hi,

I've just posted my thread, but I don't see my thread appearing in the forum.