Login Join Maker Pro

# ADC help configuring interrupt and triggering STM32f4

#### victorrr

Feb 11, 2015
11
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:

Code:
#include "stm32f4xx.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_gpio.h"

/**************************************************************************************/

void RCC_Configuration(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, 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);
}

/**************************************************************************************/

{

// ADC structure configuration
ADC_DeInit(); // Reset all parameters to their default values

// ADC common structure configuration

ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; // there are 5 clock cycles between 2 samplings

/* ADC1 regular channel 11 configuration */

/* Enable ADC1 */
}

/**************************************************************************************/

void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

/* Enable the DMA Stream IRQ Channel */
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

/**************************************************************************************/

#define BUFFERSIZE 128

int main(void)
{

RCC_Configuration();

GPIO_Configuration();

NVIC_Configuration();

}

#### Harald Kapp

##### Moderator
Moderator
Nov 17, 2011
13,101

With this on/off signal you trigger an interrupt input of the microcontroller. In the interrupt service routine you read the value of the timer which initially started the ultrasound pulse (provided this timer is still running). From the difference of the known timer value at the start of the pulse and the value upon receiption of the echo and the clock frequency of the timer you calculate the delay time as dT=(timer_at_reception - timer_at_start) / timer_clock_frequency.

From there on it is easy to calculate distance using the known speed of sound in air.

(unless I completely misunderstood your project)

Replies
3
Views
900
Replies
15
Views
1K
Replies
8
Views
3K
Replies
6
Views
1K
Replies
2
Views
915