AnsweredAssumed Answered

Need help for continues Scan ADC of STM32F0

Question asked by thieu.toan on Oct 8, 2012
Dear friends,
I implement continuous scan ADC1 with 8 channels 0,1,4,5,6,7,8,9 I post my code below. There is something, I don't understand that. 

If I configure: 
ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;          /*Scan from channel 0 to 18*/

The sequence data outputs of array RegularConvData_Tab[8]:

RegularConvData_Tab[0] of channel 9
RegularConvData_Tab[1] of channel 0
RegularConvData_Tab[2] of channel 1
RegularConvData_Tab[3] of channel 4
RegularConvData_Tab[4] of channel 5
RegularConvData_Tab[5] of channel 6
RegularConvData_Tab[6] of channel 7
RegularConvData_Tab[7] of channel 8

Anybody can help me why output data is not sequential from channel 0-9 for data from 0 to 7.

My code below.

__IO u16 RegularConvData_Tab[ADC1_NUMOFCHANNEL];




/*
*************************************************************************************************************************************
*                                                              LOCAL FUNCTIONS/PROCEDURES                                                                      *
*************************************************************************************************************************************
*/
void ADC1_Init_GPIOs(void)
{
     GPIO_InitTypeDef    GPIO_InitStructure;


     /* GPIOA Periph clock enable */
     RCC_AHBPeriphClockCmd(ADC1_GPIO_PORT1_CLK, ENABLE);
     
     /* Configure ADC Channel0,1,4,5,6,7 as analog input */
     GPIO_InitStructure.GPIO_Pin = ADC1_C1_OUTPUT|ADC1_C1_SENSE|ADC1_C2_OUTPUT|ADC1_C2_SENSE|ADC1_C3_OUTPUT|ADC1_C3_SENSE;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
     GPIO_Init(ADC1_GPIO_PORT1, &GPIO_InitStructure);


     /* GPIOB Periph clock enable */
     RCC_AHBPeriphClockCmd(ADC1_GPIO_PORT2_CLK, ENABLE);


     /* Configure ADC Channe8,9 as analog input */
     GPIO_InitStructure.GPIO_Pin = ADC1_C4_OUTPUT|ADC1_C4_SENSE;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
     GPIO_Init(ADC1_GPIO_PORT2, &GPIO_InitStructure);
}


void ADC1_Init_DMA(void)
{
     DMA_InitTypeDef   DMA_InitStructure;     
     
     /* DMA1 clock enable */
     RCC_AHBPeriphClockCmd(ADC1_DMA_CLK , ENABLE);




     /* DMA1 Channel1 Config */
     DMA_DeInit(ADC1_DMA_CHANNEL);
     DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_ADDRESS;
     DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RegularConvData_Tab;
     DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
     DMA_InitStructure.DMA_BufferSize = ADC1_NUMOFCHANNEL;
     DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
     DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
     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_M2M = DMA_M2M_Disable;
     DMA_Init(ADC1_DMA_CHANNEL, &DMA_InitStructure);
     /* DMA1 Channel1 enable */
     DMA_Cmd(ADC1_DMA_CHANNEL, ENABLE);


     /* ADC DMA request in circular mode */
     ADC_DMARequestModeConfig(ADC1_SCALE, ADC_DMAMode_Circular);


     /* Enable ADC_DMA */
     ADC_DMACmd(ADC1_SCALE, ENABLE);
}


void ADC1_Init(void)
{
     ADC_InitTypeDef     ADC_InitStructure;


     /* Initialize ADC structure */
     ADC_StructInit(&ADC_InitStructure);


     /* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits  */
     ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
     ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; 
     ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
     ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;     /*[15:12] = 0x00 ; [11:0] = data*/
     ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;          /*Scan from channel 0 to 18*/ // ADC_ScanDirection_Backward
     ADC_Init(ADC1_SCALE, &ADC_InitStructure); 


     /* Convert the ADC1 Channel 0,1,4,5,6,7 with  239 Cycles as sampling time */ 
     ADC_ChannelConfig(ADC1_SCALE, ADC_Channel_0 , ADC_SampleTime_239_5Cycles);
     ADC_ChannelConfig(ADC1_SCALE, ADC_Channel_1 , ADC_SampleTime_239_5Cycles);
     ADC_ChannelConfig(ADC1_SCALE, ADC_Channel_4 , ADC_SampleTime_239_5Cycles);
     ADC_ChannelConfig(ADC1_SCALE, ADC_Channel_5 , ADC_SampleTime_239_5Cycles);
     ADC_ChannelConfig(ADC1_SCALE, ADC_Channel_6 , ADC_SampleTime_239_5Cycles);
     ADC_ChannelConfig(ADC1_SCALE, ADC_Channel_7 , ADC_SampleTime_239_5Cycles);


     
     /* Convert the ADC1 Channel 8,9 with  239 Cycles as sampling time */ 
     ADC_ChannelConfig(ADC1_SCALE, ADC_Channel_8 , ADC_SampleTime_239_5Cycles);
     ADC_ChannelConfig(ADC1_SCALE, ADC_Channel_9 , ADC_SampleTime_239_5Cycles);




     /* ADC Calibration */
     ADC_GetCalibrationFactor(ADC1_SCALE);


     /* Enable ADC1 */
     ADC_Cmd(ADC1_SCALE, ENABLE);   


     /* Wait the ADCEN falg */
     while(!ADC_GetFlagStatus(ADC1_SCALE, ADC_FLAG_ADEN)); 


     /* ADC1 regular Software Start Conv */ 
     ADC_StartOfConversion(ADC1_SCALE);
}


void ADC_Scale_Init(void)
{
     /* ADC1 DeInit */  
     ADC_DeInit(ADC1_SCALE);


     /* ADC1 Periph clock enable */
     RCC_APB2PeriphClockCmd(ADC1_CLK, ENABLE);
          
     ADC1_Init_GPIOs();
     ADC1_Init_DMA();
     ADC1_Init();


     /* Test DMA1 TC flag */
    while((DMA_GetFlagStatus(DMA1_FLAG_TC1)) == RESET ); 
    
    /* Clear DMA TC flag */
    DMA_ClearFlag(DMA1_FLAG_TC1);
}

void main(void)
{
     /*ADC*/
     ADC_Scale_Init();
     
     while(1)
{
     /* Test DMA1 TC flag */
          while((DMA_GetFlagStatus(DMA1_FLAG_TC1)) == RESET ); 
          /* Clear DMA TC flag */
          DMA_ClearFlag(DMA1_FLAG_TC1);
          for (cnt = 0; cnt<8; cnt++){
               x16_to_str(RegularConvData_Tab[cnt], data_dip);
               uart_tx_puts(DEBUG_USART1, uart_tx_get_buf_ptr(DEBUG_USART1), data_dip, 2);
          }

}
}

Outcomes