Skip to main content
manishbaing2789
Associate III
July 30, 2013
Question

how to set samping rate of adc using stm32f4 as 1000 samples/seconds

  • July 30, 2013
  • 2 replies
  • 637 views
Posted on July 30, 2013 at 09:36

Hello I want to set sampling rate 1000 samples/seconds.I am using ADC with FreeRTOS following are ADC.C and main.c files.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

ADC.c

#include ''stm32f4xx.h''

#include ''FreeRTOS.h''

#include ''task.h''

#include <math.h>

#include <stdio.h>

#include <stdlib.h>

adcInit(){

    ADC_InitTypeDef ADC_InitStructure;

    GPIO_InitTypeDef GPIO_InitStructure;

    ADC_CommonInitTypeDef ADC_CommonInitStructure;

    

    

           //Enable ADC1

    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;

    //Initializing GPIO

    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;

    ADC_CommonInit(&ADC_CommonInitStructure);

    //ADC1 Init

    ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;

    ADC_InitStructure.ADC_ScanConvMode = DISABLE;

    ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;

    ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;

    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;

    ADC_InitStructure.ADC_NbrOfConversion = 1;

    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 ADC1

    ADC_Cmd(ADC1, ENABLE);

    

    NVIC_InitTypeDef NVIC_InitStructure;

    NVIC_InitStructure.NVIC_IRQChannel = ADC_IRQn;

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);

}

main.c.......................>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

#include ''stm32f4xx.h''

#include ''FreeRTOS.h''

#include ''task.h''

#include <math.h>

#include <stdio.h>

#include <stdlib.h>

#define MAX_COUNT 3

float adcValue[10000];

float adcVoltage[10000];

uint32_t count=0;

uint32_t ADCx = ADC1;

uint32_t USARTx = USART1;

/*uint32_t USART_BaudRate = 9600;

//uint32_t ADC_Resolution_Xb = ADC_Resolution12b;

uint32_t ADC_SampleTime_XCycles = ADC_SampleTime_3Cycles ;

uint32_t ADC_Channel_X = ADC_Channel_1 ;

*/

static void AdcTask(void *pvParameters){

    ADC_SoftwareStartConv(ADCx);

       ADC_ITConfig(ADCx,ADC_IT_EOC,ENABLE);

    while(1){

        ADC_GetFlagStatus(ADCx,ADC_FLAG_EOC);        

        //Wait Until Transmit Finishes

            while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);

        USART_SendData(USARTx,adcVoltage);

    }

}

initx(){

    adcInit();

    usartInit();

}

void ADC_IRQHandler(void){

    adcValue[count++] = (float)ADC_GetConversionValue(ADCx);

         

}

int main(void){

    initx();

    

    xTaskCreate(AdcTask, (signed char*)''AdcTask'', 128, NULL, tskIDLE_PRIORITY+1, NULL);

    vTaskStartScheduler();

return 0;

}
    This topic has been closed for replies.

    2 replies

    Tesla DeLorean
    Guru
    July 30, 2013
    Posted on July 30, 2013 at 13:28

    That would seem to take a bunch of work to fix.

    - No range checking on array

    - No conversion to voltage

    - Does not wait on EOC

    - Does not output value to USART properly

    - GPIO Clock/Bank mismatched

    - ADC Channel wrong (PA5 = 5, PC5 = 15)

    You have a couple of choices, you could start a conversion in a 1ms tick (like one derived from SysTick), or you could set up a timer to trigger the ADC with the periodicity required.
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    manishbaing2789
    Associate III
    July 30, 2013
    Posted on July 30, 2013 at 13:39

    Thanks a lot.First I will solve all issues related to  basic code.and will came up  with updated code.