AnsweredAssumed Answered

STM32F030 ADC frustration

Question asked by jasonas.matas on Oct 28, 2015
Latest reply on May 1, 2016 by bui.cuong
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 */




#include "stm32f0xx.h"
#define SAMPLES         2
#define RECEIVE         1
#define 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>

Outcomes