cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 ADC TIM Triggered

gausa
Associate II
Posted on October 29, 2013 at 11:58

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);
}

5 REPLIES 5
Posted on October 29, 2013 at 12:57

Well you don't want continuous, as that's uncontrolled.

You probably want to be generating a pulse, or pick a different prescaler/period

TIM_OCInitStructure.TIM_Pulse = 5; // Something less than period

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
gausa
Associate II
Posted on October 29, 2013 at 13:21

Oh, sry. Those Tim Values are from testing. I also tried with Pulses 1Cycle to 50 with periodes of 10 to 10000Cycles.

gausa
Associate II
Posted on October 29, 2013 at 14:55

Is there any Examplecode for Timer triggered ADC for F4 StdPeriphLib?

I found one in F0 StdPLib but using the Method of that also didn't bring more luck.
Posted on October 29, 2013 at 17:02

I've posted at least a couple.

Are you using C++, CooCox?

[DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/stm32f207%20ADC%2BTIMER%2BDMA%20%20Poor%20Peripheral%20Library%20Examples&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=1074]https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex_mx_stm32%2Fstm32f207%20ADC%2BTIMER%2BDMA%20%20Poor%20Peripheral%20Library%20Examples&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=1074

[DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Help%20with%20simple%20ADC%20on%20STM32F4&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=1151]https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex_mx_stm32%2FHelp%20with%20simple%20ADC%20on%20STM32F4&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=1151

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
gausa
Associate II
Posted on October 29, 2013 at 17:51

Aaaaand now, i finaly present.

I'ts time to get your Popcorn out ...

Line

RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

Should be

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

*facepalm*

This took 6h :(