Maker Pro
Maker Pro

ADC help configuring interrupt and triggering STM32f4

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"
#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
 

Harald Kapp

Moderator
Moderator
I can't help you with the STM code, but I think you may be on the wrong track. Why would you use an ADC to digitize the received ultrasound signal? For your measurement the important parameter is the delay in time between start of the ultrasound pulse (which will be triggered by a timer (your other thread) and reception of the echo. Therefore the received signal should generated not a 40kHz ultrasound pulse but just a on/off signal to detect absence (off) or presence (on) of the ultrasound signal. Your ultrasound receiver may already create this signal for you. If not you need a simple signal conditioning circuit to create this on/off signal from the received ultrasound.

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.

No ADC required :)
(unless I completely misunderstood your project)
 
Top