AnsweredAssumed Answered

Offset error or conversion error?

Question asked by capasso.ornella on Oct 10, 2012
Latest reply on Oct 10, 2012 by capasso.ornella
I want to check if the ADC acquires right, so I wrote this code:
  while (1)
  {
        /* convert the ADC value (from 0 to 0xFFF) to a voltage value (from 0V to 3.3V)*/
    for(i=0; i<3; i++)
    ADC3ConvertedVoltage[i] = ADC3ConvertedValue[i] *3300/0xFFF;
 
        if( ADC3ConvertedVoltage[0]>Soglia){
        STM_EVAL_LEDOn(LED4);
             
}
        else{
            STM_EVAL_LEDOff(LED4);
}
the LED lights up if the sample> threshold and turns off if it is smaller!
but does not work well ....
The results are the following
threshold = 1000 if the LED lights up when the input I put 0,898 volts
threshold = 2000 if the LED lights up when the input I put 1,797 volts
threshold = 3000 if the LED lights up when the input I put 2,698 volts
I thought that there is an error or the conversion from hexadecimal or an offset error of the ADC, what do you think?
this is the main:
/**
  ******************************************************************************
  * @file    ADC3_DMA/main.c
  * @author  MCD Application Team
  * @version V1.0.0
  * @date    19-September-2011
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */
 
/* Includes ------------------------------------------------------------------*/
#include "stm32f4_discovery.h"
#include <stdio.h>
#include <main.h>
 
/** @addtogroup STM32F4_Discovery_Peripheral_Examples
  * @{
  */
 
/** @addtogroup ADC_ADC3_DMA
  * @{
  */
 
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define ADC3_DR_ADDRESS     ((uint32_t)0x4001224C)
 
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* You can monitor the converted value by adding the variable "ADC3ConvertedValue"
   to the debugger watch window */
__IO uint16_t ADC3ConvertedValue[3] = {0,0,0};  //Vettore che contiene i valori convertiti dai tre canali
__IO uint32_t ADC3ConvertedVoltage[3];                  //Vettore sul quale effettuare le operazioni
__IO uint32_t Soglia=3000;
static __IO uint32_t TimingDelay;                               // Variabile per il Time Delay
int i=0, j=0;                                                                       //Variabili per cicli
uint32_t v=0,mv=0;                              //Variabili d'appoggio
 
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void ADC3_CH_0_1_2_DMA_Config(void);    //Funzione di configurazione ADC e DMA
 
void Delay(__IO uint32_t nTime);            ////Funzione di configurazione Delay
 
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f4xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f4xx.c file
     */
  /* ADC3 configuration *******************************************************/
  /*  - Enable peripheral clocks                                              */
  /*  - DMA2_Stream0 channel2 configuration                                   */
  /*  - Configure ADC Channel12 pin as analog input                           */
  /*  - Configure ADC3 Channel12                                              */
     
    ADC3_CH_0_1_2_DMA_Config();
 
  /* Start ADC3 Software Conversion */
  ADC_SoftwareStartConv(ADC3);
     
          
  /* Initializza i Led mountati sulla STM32F4-Discovery board */
  STM_EVAL_LEDInit(LED4);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED5);
  STM_EVAL_LEDInit(LED6);
 
  /* accende i LED4 e LED5 */
  //STM_EVAL_LEDOn(LED4);
  //STM_EVAL_LEDOn(LED5);
     
      //if (SysTick_Config(SystemCoreClock / 1000)) /**???????????*****/
  //{
    /* Capture error */
    //while (1);
  //}
     
 
  while (1)
  {
        /* convert the ADC value (from 0 to 0xFFF) to a voltage value (from 0V to 3.3V)*/
    for(i=0; i<3; i++)
    ADC3ConvertedVoltage[i] = ADC3ConvertedValue[i] *3300/0xFFF;
 
        if( ADC3ConvertedVoltage[0]>Soglia){
        STM_EVAL_LEDOn(LED4);
             
}
        else{
            STM_EVAL_LEDOff(LED4);
}
         
  //uint8_t text[50];
  
  //for(j=0; j<3; j++)
  //{
    //v=(ADC3ConvertedVoltage[j])/1000;
    //mv = (ADC3ConvertedVoltage[j]%1000)/100;
  
   // sprintf((char*)text,"   ADC = %d,%d V   ",v,mv);
    //LCD_DisplayStringLine(LINE(6+j),text);
  }
}
 
/*FUNZIONI START*/
void Delay(__IO uint32_t nTime)
{
  TimingDelay = nTime;
 
  while(TimingDelay != 0)
        {
            TimingDelay_Decrement();
}
         
}
 
/**
  * @brief  Decrements the TimingDelay variable.
  * @param  None
  * @retval None
  */
void TimingDelay_Decrement(void)
{
  if (TimingDelay != 0x00)
  {
    TimingDelay--;
  }
}
 
/*****FUBZIONI END*******/
 
 
/**
  * @brief  ADC3 channel12 with DMA configuration
  * @param  None
  * @retval None
  */
void ADC3_CH_0_1_2_DMA_Config(void)
{
  ADC_InitTypeDef       ADC_InitStructure;
  ADC_CommonInitTypeDef ADC_CommonInitStructure;
  DMA_InitTypeDef       DMA_InitStructure;
  GPIO_InitTypeDef      GPIO_InitStructure;
   
  /* Enable ADC3, DMA2 and GPIO clocks ****************************************/
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOA, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);
   
  /* DMA2 Stream0 channel2 configuration **************************************/
  DMA_InitStructure.DMA_Channel = DMA_Channel_2;
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC3_DR_ADDRESS;
  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC3ConvertedValue[0];
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  DMA_InitStructure.DMA_BufferSize = 3;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//Disable
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;    //Disable  
  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  DMA_Init(DMA2_Stream0, &DMA_InitStructure);
  DMA_Cmd(DMA2_Stream0, ENABLE);
       
    /********************************************/
       
      /* DMA2 Stream1 channel2 configuration MODIFICATO **************************************/
    
       
       
    /********************************************/
   
  /* Configure ADC3 Channel7 pin as analog input ******************************/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2;               /*MODIFICATO*/
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
   
  /* ADC Common Init **********************************************************/
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
  ADC_CommonInit(&ADC_CommonInitStructure);
   
  /* ADC3 Init ****************************************************************/
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;//DISABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConvEdge_None;//ADC_ExternalTrigConv_T1_CC1;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = 3; //Perchè effettuo ttue e tre le conversioni dall'ADC3
  ADC_Init(ADC3, &ADC_InitStructure);
   
  /* ADC3 regular channel7 configuration *************************************/
 /****MODIFICATO*****/
  ADC_RegularChannelConfig(ADC3, ADC_Channel_0, 1, ADC_SampleTime_3Cycles);   /****MODIFICATO*****/
  ADC_RegularChannelConfig(ADC3, ADC_Channel_1, 2, ADC_SampleTime_3Cycles);  /****MODIFICATO*****/
  ADC_RegularChannelConfig(ADC3, ADC_Channel_2, 3, ADC_SampleTime_3Cycles);  /****MODIFICATO*****/
    
 /* Enable DMA request after last transfer (Single-ADC mode) */
  ADC_DMARequestAfterLastTransferCmd(ADC3, ENABLE);
   
  /* Enable ADC3 DMA */
  ADC_DMACmd(ADC3, ENABLE);
   
  /* Enable ADC3 */
  ADC_Cmd(ADC3, ENABLE);
}
 
#ifdef  USE_FULL_ASSERT
 
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 
  /* Infinite loop */
  while (1)
  {
  }
}
#endif
 
/**
  * @}
  */
 
/**
  * @}
  */
 
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
the code has been developed starting from the example ADC3_DMA
the evaluation board is the discovery STM32F4



Outcomes