AnsweredAssumed Answered

character array interfering with DMA interrupt

Question asked by lu.timothy on Oct 14, 2016
Latest reply on Oct 25, 2016 by lu.timothy
Hey guys,

I have written a code to measure ADC values (PC1 to PC5) continuously using DMA interrupt, and after referring to several other topics here on the forum, I managed to get it working!

My next step is to send the values to PC via USART. The problem is, when I declared a character array for sprintf to be used in USART later, the DMA interrupt stopped being triggered. 

The problem only occurs when Line 7 of the main function is added. Once it is commented out the code runs perfectly fine again. Also, if it is just a character and not a character array it is fine too.

I really can't figure out why this is happening and I would appreciate it if someone tells me my mistake.

Here is my code:
#include "stm32f4_discovery.h"
#include "stm32f4xx_adc.h"
#include "stm32f4xx_dma.h"
#include "stm32f4xx_usart.h"
#define Res 12 //ADC resolution in bits; default = 12
#define Vref 3 //ADC reference voltage; default = 3
#define BUFFERSIZE 5 //size of ADC buffer
volatile uint16_t ADCBuffer[BUFFERSIZE]; //DMA buffer
uint16_t ADCValue[BUFFERSIZE]; //store converted values
int counter = 0; //iteration counter
//clock control
void RCC_config(void)
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
//GPIO configuration
void GPIO_config(void)
    GPIO_InitTypeDef GPIO_InitStructure;
    //ADC input
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5; //(PC1,2,3,4,5)
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; //analog measurements
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //no pull up or down
    GPIO_Init(GPIOC, &GPIO_InitStructure);
//ADC configuration
void ADC_config(void)
    ADC_CommonInitTypeDef ADC_CommonInitStructure;
    ADC_InitTypeDef ADC_InitStructure;
    //ADC common configuration
    ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; //each ADC channel measured successively
    ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; //DMA
    ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; //sampling delay
    //ADC configuration
    ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; //12 bit resolution = 4096 max value; 6b,8b,10b,or 12b
    ADC_InitStructure.ADC_ScanConvMode = ENABLE; //disable for single channel; enable for multiple channels
    ADC_InitStructure.ADC_NbrOfConversion = 5; //number of channels
    ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //enable for continuous;
    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
    ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_Init(ADC1, &ADC_InitStructure);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_3Cycles);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 2, ADC_SampleTime_3Cycles);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 3, ADC_SampleTime_3Cycles);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 4, ADC_SampleTime_3Cycles);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 5, ADC_SampleTime_3Cycles);
    ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE); //Enable DMA request after last transfer
    ADC_DMACmd(ADC1, ENABLE); //Enable ADC1 DMA
    ADC_Cmd(ADC1, ENABLE); //enable ADC1
//DMA configuration
void DMA_config(void)
    DMA_InitTypeDef DMA_InitStructure;
    DMA_InitStructure.DMA_Channel = DMA_Channel_0; //select DMA channel 0
    DMA_InitStructure.DMA_BufferSize = BUFFERSIZE; // 1 for each channel
    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //circular buffer,continuous
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //direction
    DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; //helps DMA run faster
    DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; //1/4,2/4,3/4 or full
    DMA_InitStructure.DMA_Priority = DMA_Priority_High; //high priority
    DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
    DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)ADCBuffer; //target address
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //increment after writing to address
    DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR; //source address
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //no increment for source address
    //ADC1 is connected to DMA2_Stream0 and DMA2_Stream4
    DMA_Init(DMA2_Stream0, &DMA_InitStructure);
    DMA_ITConfig(DMA2_Stream0, DMA_IT_TC, ENABLE); //Enable DMA Stream Transfer Complete interrupt
    DMA_Cmd(DMA2_Stream0, ENABLE); //enable DMA2 stream0
//NVIC configuration
void NVIC_config(void)
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //enable interrupt
    NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn; //DMA stream channel 0
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //main interrupt priority
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //secondary interrupt priority
//interrupt routine
void DMA2_Stream0_IRQHandler(void)
{   int j = 0; //iteration index for buffer data transfer
    if (DMA_GetITStatus(DMA2_Stream0, DMA_IT_TCIF0)) //interrupt triggered when DMA transfer complete
        for (j = 0; j < BUFFERSIZE; j++)
        ADCValue[j] = ADCBuffer[j]; //transfer data from buffer to data register
        DMA_ClearITPendingBit(DMA2_Stream0, DMA_IT_TCIF0); //clear DMA interrupt flag to start data streaming again
int main(void)
    uint16_t Sensor1 = 0; //sensor measurement in millivolts
    uint16_t Sensor2 = 0;
    uint16_t Sensor3 = 0;
    uint16_t Sensor4 = 0;
    uint16_t Sensor5 = 0;
    int i = 0; //index
    //char text[5]; //character array that is causing problem
    RCC_config(); //clock control
    GPIO_config(); //GPIO configuration
    DMA_config(); //DMA configuration
    NVIC_config(); //NVIC configuration
    ADC_config(); //ADC configuration
    ADC_SoftwareStartConv(ADC1); //start ADC software conversion
    while(1) //infinite loop
        //convert measurement to millivolts
        Sensor1 = (ADCValue[i] + 1)/pow(2, Res) * Vref * 1000;
        Sensor2 = (ADCValue[i] + 1)/pow(2, Res) * Vref * 1000;
        Sensor3 = (ADCValue[i] + 1)/pow(2, Res) * Vref * 1000;
        Sensor4 = (ADCValue[i] + 1)/pow(2, Res) * Vref * 1000;
        Sensor5 = (ADCValue[i] + 1)/pow(2, Res) * Vref * 1000;
        counter++; //increment iteration counter
        i = 0; //reset index