AnsweredAssumed Answered

STM32F4 ADC TIM Triggered

Question asked by g.dominik on Oct 29, 2013
Latest reply on Oct 29, 2013 by g.dominik
Hello,

i'm trying to regulate the Samplefrequence of the ADC3 via TIM2 OC2.
Case, ADC Continuous Mode = Enable: Works
Case, ADC Continuous Mode = Disable: no Conversation

Does anyone see what i'm doing wrong?

/* Includes ------------------------------------------------------------------*/
 
#include "global.h"
 
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define BUFFER_SIZE 16
 
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
__IO uint16_t uhADCxConvertedValue[BUFFER_SIZE] = { 0, };
__IO uint32_t uwADCxConvertedVoltage = 0;
__IO uint8_t state = 0;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
 
void init_tim2() {
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_OCInitTypeDef TIM_OCInitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
    TIM_TimeBaseStructure.TIM_Period = 10;
    TIM_TimeBaseStructure.TIM_Prescaler = 4200 - 1;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV4;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
 
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = 10;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
    TIM_OC2Init(TIM2, &TIM_OCInitStructure);
 
    TIM_Cmd(TIM2, ENABLE);
}
 
void init_dma(void) {
    DMA_InitTypeDef DMA_InitStructure;
 
    ADC_DeInit();
    DMA_DeInit(DMA2_Stream0);
 
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
 
    DMA_InitStructure.DMA_Channel = DMA_Channel_2;
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &ADC3->DR;
    DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) uhADCxConvertedValue;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
    DMA_InitStructure.DMA_BufferSize = BUFFER_SIZE;
    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_Normal;
    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);
 
    DMA_ITConfig(DMA2_Stream0, DMA_IT_TC, ENABLE);
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}
 
void init_adc() {
    GPIO_InitTypeDef GPIO_InitStructure;
    ADC_InitTypeDef ADC_InitStructure;
    ADC_CommonInitTypeDef ADC_CommonInitStructure;
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);
 
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
 
    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);
 
    ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
    ADC_InitStructure.ADC_ScanConvMode = DISABLE;
    ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
    ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;
    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2;
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_NbrOfConversion = 1;
    ADC_Init(ADC3, &ADC_InitStructure);
 
    ADC_RegularChannelConfig(ADC3, ADC_Channel_1, 1, ADC_SampleTime_15Cycles);
 
    ADC_DMARequestAfterLastTransferCmd(ADC3, ENABLE);
 
    ADC_DMACmd(ADC3, ENABLE);
 
    ADC_Cmd(ADC3, ENABLE);
}
 
int main(void) {
    SystemInit();
 
    init_uart2(38400);
    init_uart2_IT();
    ms_delay(3000);
 
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
    GPIO_InitStructure.GPIO_Pin |= GPIO_Pin_13;
    GPIO_InitStructure.GPIO_Pin |= GPIO_Pin_14;
    GPIO_InitStructure.GPIO_Pin |= GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOD, &GPIO_InitStructure);
 
    init_tim2();
    GPIO_SetBits(GPIOD, GPIO_Pin_12);
    char buffer[10];
    while (1) {
        putString("--\n", 3);
        GPIO_ToggleBits(GPIOD, GPIO_Pin_13);
        init_dma();
        init_adc();
        ADC_SoftwareStartConv(ADC3);
        state = 0;
        while (state == 0)
            ;
        GPIO_ToggleBits(GPIOD, GPIO_Pin_13);
        uint16_t i;
        for (i = 0; i < BUFFER_SIZE; i++) {
            uwADCxConvertedVoltage = uhADCxConvertedValue[i] * 3300 / 0xFFF;
            myitoa(uwADCxConvertedVoltage, buffer, 9);
            putString(buffer, 10);
            putString("\n", 3);
        }
 
        ms_delay(500);
    }
}
 
void DMA2_Stream0_IRQHandler() {
    DMA_ClearITPendingBit(DMA2_Stream0, DMA_FLAG_TCIF0);
    state = 1;
    GPIO_ToggleBits(GPIOD, GPIO_Pin_14);
}

Outcomes