How come the DMA delivers a 16bit value when the ADC is 12bit?How to initialize all members of an array to the same value?PIC32MX dma not triggered by adcSTM32F4 ADC DMA config not workingSTM32 F3 Discovery ADC with DMA and NvicRe-running DMA transfer from ADC to memory on STM32F429ADC on AT91SAM7 with DMASTM32F303: ADC with DMA only works a few timesSTM32F3 Discovery board ADC DMA transfer not workingADC conversion triggered by 1ms timer not working, STM32F4
What is the difference between Major and Minor Bug?
How to find multiple values on the same line in any permutation using Notepad++?
Why can't an Airbus A330 dump fuel in an emergency?
Prove your innocence
Why did Khan ask Admiral James T. Kirk about Project Genesis?
Is a player able to change alignment midway through an adventure?
Is for(( ... )) ... ; a valid shell syntax? In which shells?
In French culture, does the metaphorical phrase "lui jeter des assiettes en pleine figure" convey the idea of "having a heated argument"?
Why does The Ancient One think differently about Doctor Strange in Endgame than the film Doctor Strange?
How do you harvest carrots in creative mode?
Can more than one wizard copy a spell from a spellbook?
Justifying the use of directed energy weapons
Is there any music source code for sound chips?
Different kernel versions across different machines
Algorithms vs LP or MIP
How to use "Du hast/ Du hattest'?
Best clipless pedals for sore feet?
Is using a hyperlink to close a modal a poor design decision?
Why isn't "I've" a proper response?
Would it be possible to have a GMO that produces chocolate?
Did the British navy fail to take into account the ballistics correction due to Coriolis force during WW1 Falkland Islands battle?
If all stars rotate, why was there a theory developed that requires non-rotating stars?
Defense against attacks using dictionaries
Highlight typical elements in list?
How come the DMA delivers a 16bit value when the ADC is 12bit?
How to initialize all members of an array to the same value?PIC32MX dma not triggered by adcSTM32F4 ADC DMA config not workingSTM32 F3 Discovery ADC with DMA and NvicRe-running DMA transfer from ADC to memory on STM32F429ADC on AT91SAM7 with DMASTM32F303: ADC with DMA only works a few timesSTM32F3 Discovery board ADC DMA transfer not workingADC conversion triggered by 1ms timer not working, STM32F4
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Using the StdPeriph Library for my STM32F4 Discovery board, I encountered a strange problem with the Debugger/Code. ( using the IAR Workbench )
The variable ADC3ConvertedValue shows 16bit values, whereas the ADC is only 12bit. In fact, they do correlate with the input voltage of the ADC, but I expected a value 2^12 - 1 max.
The problem has to be somewhere in my short code. I am using DMA in circular Mode from the Library-Example (I just changed the main() function)
Trying only a while loop from the example Code works fine, but as soon as I add my few lines, the value is corrupt.
__IO uint16_t ADC3ConvertedValue = 0;
__IO uint16_t ADC3ConvertedVoltage = 0;
uint32_t sum = 0;
uint16_t count = 0;
uint16_t average = 0;
float voltageSignal = 0;
void ADC3_CH12_DMA_Config(void);
int main(void)
ADC3_CH12_DMA_Config();
ADC_SoftwareStartConv(ADC3);
// ADC3ConvertedVoltage = ADC3ConvertedValue *3300/0xFFF;
for(count = 0; count < 5000; count++)
sum += ADC3ConvertedValue;
average = sum / count;
while(1)
voltageSignal = ((ADC3ConvertedValue - average) * VOLT_PER_BIT);
For clarification, this is the example Code that works ( I just changed the main function) :
/* Includes ------------------------------------------------------------------*/
#include "stm32f4_discovery.h"
#include <stdio.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 = 0;
__IO uint32_t ADC3ConvertedVoltage = 0;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void ADC3_CH12_DMA_Config(void);
/**
* @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_CH12_DMA_Config();
/* Start ADC3 Software Conversion */
ADC_SoftwareStartConv(ADC3);
while (1)
/* convert the ADC value (from 0 to 0xFFF) to a voltage value (from 0V to 3.3V)*/
ADC3ConvertedVoltage = ADC3ConvertedValue *3300/0xFFF;
/**
* @brief ADC3 channel12 with DMA configuration
* @param None
* @retval None
*/
void ADC3_CH12_DMA_Config(void)
RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);
/* DMA2 Stream0 channel0 configuration **************************************/
DMA_InitStructure.DMA_Channel = DMA_Channel_2;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC3_DR_ADDRESS;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC3ConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_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_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);
/* Configure ADC3 Channel12 pin as analog input ******************************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOC, &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 = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC3, &ADC_InitStructure);
/* ADC3 regular channel12 configuration *************************************/
ADC_RegularChannelConfig(ADC3, ADC_Channel_12, 1, ADC_SampleTime_3Cycles);
/* 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 %drn", file, line) */
/* Infinite loop */
while (1)
#endif
/**
* @
*/
/**
* @
*/
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
c overflow dma stm32f4discovery adc
|
show 7 more comments
Using the StdPeriph Library for my STM32F4 Discovery board, I encountered a strange problem with the Debugger/Code. ( using the IAR Workbench )
The variable ADC3ConvertedValue shows 16bit values, whereas the ADC is only 12bit. In fact, they do correlate with the input voltage of the ADC, but I expected a value 2^12 - 1 max.
The problem has to be somewhere in my short code. I am using DMA in circular Mode from the Library-Example (I just changed the main() function)
Trying only a while loop from the example Code works fine, but as soon as I add my few lines, the value is corrupt.
__IO uint16_t ADC3ConvertedValue = 0;
__IO uint16_t ADC3ConvertedVoltage = 0;
uint32_t sum = 0;
uint16_t count = 0;
uint16_t average = 0;
float voltageSignal = 0;
void ADC3_CH12_DMA_Config(void);
int main(void)
ADC3_CH12_DMA_Config();
ADC_SoftwareStartConv(ADC3);
// ADC3ConvertedVoltage = ADC3ConvertedValue *3300/0xFFF;
for(count = 0; count < 5000; count++)
sum += ADC3ConvertedValue;
average = sum / count;
while(1)
voltageSignal = ((ADC3ConvertedValue - average) * VOLT_PER_BIT);
For clarification, this is the example Code that works ( I just changed the main function) :
/* Includes ------------------------------------------------------------------*/
#include "stm32f4_discovery.h"
#include <stdio.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 = 0;
__IO uint32_t ADC3ConvertedVoltage = 0;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void ADC3_CH12_DMA_Config(void);
/**
* @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_CH12_DMA_Config();
/* Start ADC3 Software Conversion */
ADC_SoftwareStartConv(ADC3);
while (1)
/* convert the ADC value (from 0 to 0xFFF) to a voltage value (from 0V to 3.3V)*/
ADC3ConvertedVoltage = ADC3ConvertedValue *3300/0xFFF;
/**
* @brief ADC3 channel12 with DMA configuration
* @param None
* @retval None
*/
void ADC3_CH12_DMA_Config(void)
RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);
/* DMA2 Stream0 channel0 configuration **************************************/
DMA_InitStructure.DMA_Channel = DMA_Channel_2;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC3_DR_ADDRESS;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC3ConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_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_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);
/* Configure ADC3 Channel12 pin as analog input ******************************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOC, &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 = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC3, &ADC_InitStructure);
/* ADC3 regular channel12 configuration *************************************/
ADC_RegularChannelConfig(ADC3, ADC_Channel_12, 1, ADC_SampleTime_3Cycles);
/* 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 %drn", file, line) */
/* Infinite loop */
while (1)
#endif
/**
* @
*/
/**
* @
*/
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
c overflow dma stm32f4discovery adc
I don't understand what is the code that "works" and what is the code that "doesn't work". And I don't understand what exactly is the difference in output between the working code and the non-working code.
– MFisherKDX
Mar 27 at 17:31
This is the example code that works: while(1) ADC3ConvertedVoltage = ADC3ConvertedValue *3300/0xFFF; In the working code, the values are like they should be. 12 bit, and they represent the ADC input. Adding those few lines is messing the values up :)
– xardas_the_mage
Mar 27 at 17:37
Aren't you expected to mask off 4 bits to obtain the 12-bit value? What difference does DMA make?
– Weather Vane
Mar 27 at 17:38
1
This is the example code that works: while(1) ADC3ConvertedVoltage = ADC3ConvertedValue *3300/0xFFF; --> Are you sure that's a division/and not a bitwise or|
– MFisherKDX
Mar 27 at 17:41
One piece of code writes toADC3ConvertedVoltageand the other doesn't touch it. I'm really confused what you are doing.
– MFisherKDX
Mar 27 at 17:44
|
show 7 more comments
Using the StdPeriph Library for my STM32F4 Discovery board, I encountered a strange problem with the Debugger/Code. ( using the IAR Workbench )
The variable ADC3ConvertedValue shows 16bit values, whereas the ADC is only 12bit. In fact, they do correlate with the input voltage of the ADC, but I expected a value 2^12 - 1 max.
The problem has to be somewhere in my short code. I am using DMA in circular Mode from the Library-Example (I just changed the main() function)
Trying only a while loop from the example Code works fine, but as soon as I add my few lines, the value is corrupt.
__IO uint16_t ADC3ConvertedValue = 0;
__IO uint16_t ADC3ConvertedVoltage = 0;
uint32_t sum = 0;
uint16_t count = 0;
uint16_t average = 0;
float voltageSignal = 0;
void ADC3_CH12_DMA_Config(void);
int main(void)
ADC3_CH12_DMA_Config();
ADC_SoftwareStartConv(ADC3);
// ADC3ConvertedVoltage = ADC3ConvertedValue *3300/0xFFF;
for(count = 0; count < 5000; count++)
sum += ADC3ConvertedValue;
average = sum / count;
while(1)
voltageSignal = ((ADC3ConvertedValue - average) * VOLT_PER_BIT);
For clarification, this is the example Code that works ( I just changed the main function) :
/* Includes ------------------------------------------------------------------*/
#include "stm32f4_discovery.h"
#include <stdio.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 = 0;
__IO uint32_t ADC3ConvertedVoltage = 0;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void ADC3_CH12_DMA_Config(void);
/**
* @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_CH12_DMA_Config();
/* Start ADC3 Software Conversion */
ADC_SoftwareStartConv(ADC3);
while (1)
/* convert the ADC value (from 0 to 0xFFF) to a voltage value (from 0V to 3.3V)*/
ADC3ConvertedVoltage = ADC3ConvertedValue *3300/0xFFF;
/**
* @brief ADC3 channel12 with DMA configuration
* @param None
* @retval None
*/
void ADC3_CH12_DMA_Config(void)
RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);
/* DMA2 Stream0 channel0 configuration **************************************/
DMA_InitStructure.DMA_Channel = DMA_Channel_2;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC3_DR_ADDRESS;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC3ConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_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_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);
/* Configure ADC3 Channel12 pin as analog input ******************************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOC, &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 = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC3, &ADC_InitStructure);
/* ADC3 regular channel12 configuration *************************************/
ADC_RegularChannelConfig(ADC3, ADC_Channel_12, 1, ADC_SampleTime_3Cycles);
/* 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 %drn", file, line) */
/* Infinite loop */
while (1)
#endif
/**
* @
*/
/**
* @
*/
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
c overflow dma stm32f4discovery adc
Using the StdPeriph Library for my STM32F4 Discovery board, I encountered a strange problem with the Debugger/Code. ( using the IAR Workbench )
The variable ADC3ConvertedValue shows 16bit values, whereas the ADC is only 12bit. In fact, they do correlate with the input voltage of the ADC, but I expected a value 2^12 - 1 max.
The problem has to be somewhere in my short code. I am using DMA in circular Mode from the Library-Example (I just changed the main() function)
Trying only a while loop from the example Code works fine, but as soon as I add my few lines, the value is corrupt.
__IO uint16_t ADC3ConvertedValue = 0;
__IO uint16_t ADC3ConvertedVoltage = 0;
uint32_t sum = 0;
uint16_t count = 0;
uint16_t average = 0;
float voltageSignal = 0;
void ADC3_CH12_DMA_Config(void);
int main(void)
ADC3_CH12_DMA_Config();
ADC_SoftwareStartConv(ADC3);
// ADC3ConvertedVoltage = ADC3ConvertedValue *3300/0xFFF;
for(count = 0; count < 5000; count++)
sum += ADC3ConvertedValue;
average = sum / count;
while(1)
voltageSignal = ((ADC3ConvertedValue - average) * VOLT_PER_BIT);
For clarification, this is the example Code that works ( I just changed the main function) :
/* Includes ------------------------------------------------------------------*/
#include "stm32f4_discovery.h"
#include <stdio.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 = 0;
__IO uint32_t ADC3ConvertedVoltage = 0;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void ADC3_CH12_DMA_Config(void);
/**
* @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_CH12_DMA_Config();
/* Start ADC3 Software Conversion */
ADC_SoftwareStartConv(ADC3);
while (1)
/* convert the ADC value (from 0 to 0xFFF) to a voltage value (from 0V to 3.3V)*/
ADC3ConvertedVoltage = ADC3ConvertedValue *3300/0xFFF;
/**
* @brief ADC3 channel12 with DMA configuration
* @param None
* @retval None
*/
void ADC3_CH12_DMA_Config(void)
RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);
/* DMA2 Stream0 channel0 configuration **************************************/
DMA_InitStructure.DMA_Channel = DMA_Channel_2;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC3_DR_ADDRESS;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC3ConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_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_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);
/* Configure ADC3 Channel12 pin as analog input ******************************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOC, &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 = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC3, &ADC_InitStructure);
/* ADC3 regular channel12 configuration *************************************/
ADC_RegularChannelConfig(ADC3, ADC_Channel_12, 1, ADC_SampleTime_3Cycles);
/* 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 %drn", file, line) */
/* Infinite loop */
while (1)
#endif
/**
* @
*/
/**
* @
*/
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
c overflow dma stm32f4discovery adc
c overflow dma stm32f4discovery adc
edited Mar 27 at 17:58
xardas_the_mage
asked Mar 27 at 17:23
xardas_the_magexardas_the_mage
131 silver badge6 bronze badges
131 silver badge6 bronze badges
I don't understand what is the code that "works" and what is the code that "doesn't work". And I don't understand what exactly is the difference in output between the working code and the non-working code.
– MFisherKDX
Mar 27 at 17:31
This is the example code that works: while(1) ADC3ConvertedVoltage = ADC3ConvertedValue *3300/0xFFF; In the working code, the values are like they should be. 12 bit, and they represent the ADC input. Adding those few lines is messing the values up :)
– xardas_the_mage
Mar 27 at 17:37
Aren't you expected to mask off 4 bits to obtain the 12-bit value? What difference does DMA make?
– Weather Vane
Mar 27 at 17:38
1
This is the example code that works: while(1) ADC3ConvertedVoltage = ADC3ConvertedValue *3300/0xFFF; --> Are you sure that's a division/and not a bitwise or|
– MFisherKDX
Mar 27 at 17:41
One piece of code writes toADC3ConvertedVoltageand the other doesn't touch it. I'm really confused what you are doing.
– MFisherKDX
Mar 27 at 17:44
|
show 7 more comments
I don't understand what is the code that "works" and what is the code that "doesn't work". And I don't understand what exactly is the difference in output between the working code and the non-working code.
– MFisherKDX
Mar 27 at 17:31
This is the example code that works: while(1) ADC3ConvertedVoltage = ADC3ConvertedValue *3300/0xFFF; In the working code, the values are like they should be. 12 bit, and they represent the ADC input. Adding those few lines is messing the values up :)
– xardas_the_mage
Mar 27 at 17:37
Aren't you expected to mask off 4 bits to obtain the 12-bit value? What difference does DMA make?
– Weather Vane
Mar 27 at 17:38
1
This is the example code that works: while(1) ADC3ConvertedVoltage = ADC3ConvertedValue *3300/0xFFF; --> Are you sure that's a division/and not a bitwise or|
– MFisherKDX
Mar 27 at 17:41
One piece of code writes toADC3ConvertedVoltageand the other doesn't touch it. I'm really confused what you are doing.
– MFisherKDX
Mar 27 at 17:44
I don't understand what is the code that "works" and what is the code that "doesn't work". And I don't understand what exactly is the difference in output between the working code and the non-working code.
– MFisherKDX
Mar 27 at 17:31
I don't understand what is the code that "works" and what is the code that "doesn't work". And I don't understand what exactly is the difference in output between the working code and the non-working code.
– MFisherKDX
Mar 27 at 17:31
This is the example code that works: while(1) ADC3ConvertedVoltage = ADC3ConvertedValue *3300/0xFFF; In the working code, the values are like they should be. 12 bit, and they represent the ADC input. Adding those few lines is messing the values up :)
– xardas_the_mage
Mar 27 at 17:37
This is the example code that works: while(1) ADC3ConvertedVoltage = ADC3ConvertedValue *3300/0xFFF; In the working code, the values are like they should be. 12 bit, and they represent the ADC input. Adding those few lines is messing the values up :)
– xardas_the_mage
Mar 27 at 17:37
Aren't you expected to mask off 4 bits to obtain the 12-bit value? What difference does DMA make?
– Weather Vane
Mar 27 at 17:38
Aren't you expected to mask off 4 bits to obtain the 12-bit value? What difference does DMA make?
– Weather Vane
Mar 27 at 17:38
1
1
This is the example code that works: while(1) ADC3ConvertedVoltage = ADC3ConvertedValue *3300/0xFFF; --> Are you sure that's a division
/ and not a bitwise or |– MFisherKDX
Mar 27 at 17:41
This is the example code that works: while(1) ADC3ConvertedVoltage = ADC3ConvertedValue *3300/0xFFF; --> Are you sure that's a division
/ and not a bitwise or |– MFisherKDX
Mar 27 at 17:41
One piece of code writes to
ADC3ConvertedVoltage and the other doesn't touch it. I'm really confused what you are doing.– MFisherKDX
Mar 27 at 17:44
One piece of code writes to
ADC3ConvertedVoltage and the other doesn't touch it. I'm really confused what you are doing.– MFisherKDX
Mar 27 at 17:44
|
show 7 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55383177%2fhow-come-the-dma-delivers-a-16bit-value-when-the-adc-is-12bit%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55383177%2fhow-come-the-dma-delivers-a-16bit-value-when-the-adc-is-12bit%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I don't understand what is the code that "works" and what is the code that "doesn't work". And I don't understand what exactly is the difference in output between the working code and the non-working code.
– MFisherKDX
Mar 27 at 17:31
This is the example code that works: while(1) ADC3ConvertedVoltage = ADC3ConvertedValue *3300/0xFFF; In the working code, the values are like they should be. 12 bit, and they represent the ADC input. Adding those few lines is messing the values up :)
– xardas_the_mage
Mar 27 at 17:37
Aren't you expected to mask off 4 bits to obtain the 12-bit value? What difference does DMA make?
– Weather Vane
Mar 27 at 17:38
1
This is the example code that works: while(1) ADC3ConvertedVoltage = ADC3ConvertedValue *3300/0xFFF; --> Are you sure that's a division
/and not a bitwise or|– MFisherKDX
Mar 27 at 17:41
One piece of code writes to
ADC3ConvertedVoltageand the other doesn't touch it. I'm really confused what you are doing.– MFisherKDX
Mar 27 at 17:44