cancel
Showing results for 
Search instead for 
Did you mean: 

ADC help configuring interrupt and triggering STM32f4

fiulala
Associate II
Posted on May 04, 2015 at 12:58

Hello, I want to calculate distances using ultrasounds. I configured correctly an ultrasound transmitter to make pulses of 40kHz. Now I'm trying to configure STM32f4 to read data from the ultrasound receiver.

I'm new on STM32f4 and I don't know how to configure properly the ADC interrupt and triggering. The pulses will arrive to the receiver intermittently (for example pulses arriving during 1 ms and the next pulses arrive 1 second later). I want to trigger ADC read the pulses using while saving them in a buffer, interrupt ADC,use a program (already done) to calculate the distance and when i finish trigger ADC again. My premature code is this:

#include ''stm32f4xx.h''
#include ''stm32f4xx_rcc.h''
#include ''stm32f4xx_gpio.h''
#include ''stm32f4xx_adc.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* ADC Channel 11 -> PC1 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
/**************************************************************************************/
void ADC_Configuration(void)
{
ADC_CommonInitTypeDef ADC_CommonInitStructure;
ADC_InitTypeDef ADC_InitStructure;
// ADC structure configuration
ADC_DeInit(); // Reset all parameters to their default values
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_TRGO;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1, &ADC_InitStructure); 
// ADC common structure configuration
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; // independent mode
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; // f(ADC3)=84/4=48MHz
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; // disable DMA_MODE
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; // there are 5 clock cycles between 2 samplings
ADC_CommonInit(&ADC_CommonInitStructure);
/* ADC1 regular channel 11 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_144Cycles); // PC1 es el sample time (cicles que estara llegint dades?)
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
}
/**************************************************************************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the DMA Stream IRQ Channel */
NVIC_InitStruct.NVIC_IRQChannel = ADC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**************************************************************************************/
#define BUFFERSIZE 128
uint16_t ADCConvertedValues[BUFFERSIZE];
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
ADC_Configuration();
NVIC_Configuration();
}

Please help, anything will be helpful #stm32f4-adc
1 REPLY 1
Chris1
Senior III
Posted on May 05, 2015 at 18:17

One approach is to sample the analog ultrasonic signal repetitively with the ADC, starting at the time when the transducer is pulsed and continuing for a time equivalent to your desired maximum range.  The analog sample buffer is then processed to find the echo of interest.

To accomplish this, the ADC can be triggered by a timer (for example TIM3) configured to update at the desired rate, or it could be done with continuous mode and DMA (refer to ADC_DMA Standard peripheral Library example).