AnsweredAssumed Answered

ADC in Continuous conversion mode with STM32F4 Discovey

Question asked by Manish on Oct 21, 2013
Latest reply on Oct 22, 2013 by Manish
Hello I am working with STM32F4 Discovery with FreeRTOS and summon arm tool chain.I am sending ADC  data over serial Port using USB serial communication.I have write code to get ADC value in single conversion mode.

Now I have to modify that code for  conversion mode .
I have just did one thing


ADC_InitStructure.ADC_ContinuousConvMode =ENABLE; which previously DISABLE.


But till Continuous conversion not working for continuous conversion.

so What all the things I need to add or modify...?


Following are two files 1)ADC.c ------------> ADC initialization  
                                      2)main.c--------------> Application Code

.....................................................................ADC.c...............................................................
/*! ADC source File */
#include "stm32f4xx.h"
#include "FreeRTOS.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

/*! ADC Initialazion function */
adcInit(){
     
         /*! Intialsizing structure to set ADC Configuration */
         ADC_InitTypeDef ADC_InitStructure;
         
     /*! Intialsizing structure to set GPIO  Configuration */
     GPIO_InitTypeDef GPIO_InitStructure;


         /*! Intialsizing structure to set ADC  Common Configuration */
     ADC_CommonInitTypeDef ADC_CommonInitStructure;
     
     
            /*! Enable ADC1 Interface Clock */
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);


     /*! Enable the ADC1 GPIO Clock     */
           RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); 


     /*! Initializing GPIO for ADC */
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;


     /*! Configuring  ADC GPIO pins in analog mode */


     GPIO_Init(GPIOC, &GPIO_InitStructure);


     /*! ADC Common Init */


     /*! Use Independent mode */
     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;
     
     /*! 
      *  Initializes the ADCx peripheral according 
      *  to the specified ADC parameters in the ADC_CommonInitStruct
      */


     ADC_CommonInit(&ADC_CommonInitStructure);


     /*! ADC1 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;
     
     /*!
      *  Initializes the ADCx peripheral according 
      *   to the specified ADC parameters in the ADC_InitStruct. 
         */
     ADC_Init(ADC1, &ADC_InitStructure);
     
     /*! Enabling Temperature sensor */
     ADC_TempSensorVrefintCmd(ENABLE);


     /*! ADC1 regular channel16 configuration */
     ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_3Cycles);
 

     /*! Enable(Activate ) ADC1 Peripherals */
     ADC_Cmd(ADC1, ENABLE);


     
         /*! Intialsizing structure For NVIC Configuration */
     NVIC_InitTypeDef NVIC_InitStructure;
        
     /*! set parameters of NVIC_InitTypeDef structure */
     NVIC_InitStructure.NVIC_IRQChannel = ADC_IRQn;
     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
     
     /*!
      * Initializes the NVIC peripheral according
      *  to the specified parameters in the NVIC_InitStruct
      */
     NVIC_Init(&NVIC_InitStructure);


}

>>>>>>>>>>>>>>>>>>>>>>>>>>main.c<<<<<<<<<<<<<<<<<<<<<<<<<<<<<,
#include "stm32f4xx.h"
#include "stm32f4xx_conf.h"
#include "FreeRTOS.h"
#include "task.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>


#include "usbd_cdc_core.h"
#include "usbd_usr.h"
#include "usbd_desc.h"
#include "usbd_cdc_vcp.h"


#include "/home/manish/Product_codes/Final_Programming/LED-FreeRTOS_USB_serial_with_adc/libs/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_adc.h"
#define MAX_COUNT 3 
#define SIZE 1000


void AdcTask();
void initx();
void Delay( volatile uint32_t count);
void init();
void calculation_test();
__ALIGN_BEGIN USB_OTG_CORE_HANDLE  USB_OTG_dev __ALIGN_END;


uint32_t ADCx = ADC1;






void AdcTask(){


     ADC_SoftwareStartConv(ADCx);
        ADC_ITConfig(ADCx,ADC_IT_EOC,ENABLE);
}




void initx(){


    adcInit();


}
static uint32_t count = 0  ;
double val[1000];
void ADC_IRQHandler(void){
 
     if ( count >= 1000)
          return;     
      val[++count]= (float)ADC_GetConversionValue(ADCx);


     
}


 void TaskA(void *pvParameters)
{
   int A=0;
    pvParameters = pvParameters;
   //for(;;)
    //for( int m=0; m < 500 ; m++)
 while(1)
    {
      A++;
       printf("ADC_Data[%d ] =%lf\n",count,val[count]);
     
      GPIO_SetBits(GPIOD, GPIO_Pin_12);
      vTaskDelay(25);
      GPIO_ResetBits(GPIOD, GPIO_Pin_12);
      vTaskDelay(25);
      GPIO_SetBits(GPIOD, GPIO_Pin_13);
      vTaskDelay(25);
      GPIO_ResetBits(GPIOD, GPIO_Pin_13);
      vTaskDelay(25);


    }
}






int main(void)
{
     init();
    initx();
    AdcTask();
     setbuf(stdout, NULL); 
   //  printf("Manish%d\n",b);
    //calculation_test();


    xTaskCreate(TaskA, (signed char*)"TaskA", 1000, NULL, tskIDLE_PRIORITY+1, NULL);
    vTaskStartScheduler();
return 0;
}


void calculation_test() {
     //float a = 1.001;
     int iteration = 0;
  //      int n;
        for(;;) {
//     for( n=0; n < 500; n++){


          GPIO_SetBits(GPIOD, GPIO_Pin_12);
          Delay(15000);
          GPIO_ResetBits(GPIOD, GPIO_Pin_12);
          Delay(15000);


//          time_var2 = 0;


                //int j=0;
               Delay(15000);
          printf("ADC Data:  %lf\n",val);
             iteration++;
     }
}






void init() {
     GPIO_InitTypeDef  GPIO_InitStructure;


     // ---------- SysTick timer -------- //
     //if (SysTick_Config(SystemCoreClock / 1000)) {
          // Capture error
     //     while (1){};
     //}


     // ---------- GPIO -------- //
     // GPIOD Periph clock enable
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);


     // Configure PD12, PD13, PD14 and PD15 in output pushpull mode
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
     GPIO_Init(GPIOD, &GPIO_InitStructure);


     // ------------- USB -------------- //
     USBD_Init(&USB_OTG_dev,
                 USB_OTG_FS_CORE_ID,
                 &USR_desc,
                 &USBD_CDC_cb,
                 &USR_cb);
}




void Delay( volatile uint32_t count)
{
      uint32_t j=0,i=0;
     
     for(j=0;j<count;j++)
     {
          // At 60Mhz, the below loop introduces
          //delay of 10 us 
          for(i=0;i<35;i++)
               asm(" nop ");
     }
}















Outcomes