cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f4 interrupt

haythem
Associate II
Posted on March 20, 2014 at 16:29

Hi, I'm still start in STM32F4 I am looking for how to use the ADC interrupt that will allow me to read sensor values ​​and the interruption with TIM that will allow me to read the values ​​back by an encoder

11 REPLIES 11
Posted on March 20, 2014 at 16:54

Review Example code provided in the Firmware Libraries

STM32F4-Discovery_FW_V1.1.0\Project\Peripheral_Examples\TIM_TimeBase

STM32F4xx_DSP_StdPeriph_Lib_V1.3.0\Project\STM32F4xx_StdPeriph_Examples\TIM\TIM_TimeBase

There are also several ADC examples, perhaps not generating EOC interrupts.

And I've posted a lot of examples on the forum, though I'd recommend Google over the board search
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
haythem
Associate II
Posted on March 20, 2014 at 17:07

Hi thank you for your reply, I want a trick to run the fontion ''readsensor'' in parallel with (while (1))

int main(void)

{

  ADC1_CH12_DMA_Config();

  ADC_SoftwareStartConv(ADC1);

readsensor();

  while (1)

  {

if (val[0]>4000)

{

GPIO_SetBits(GPIOD, GPIO_Pin_12);

}

else 

{

GPIO_ResetBits(GPIOD, GPIO_Pin_12);

}

  }

}

Posted on March 20, 2014 at 17:28

Configure the DMA to interrupt at HT/TC events.

Not sure what DMA/Channel/Stream you're using.

Enable the NVIC, Enable IRQ on DMA, create IRQHandler()

[DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/3%20channels%20on%20ADC1%20and%20DMA&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=504]https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex_mx_stm32%2F3%20channels%20on%20ADC1%20and%20DMA&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=504

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
haythem
Associate II
Posted on March 20, 2014 at 23:58

void NVIC_Configuration(void) {

    NVIC_InitTypeDef NVIC_InitStructure;

 

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

 

    /* Enable DMA1 channel1 IRQ Channel */

    NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;

    NVIC_Init(&NVIC_InitStructure);

}

er : ..\main.c(589): error:  #20: identifier ''DMA1_Channel1_IRQn'' is undefined

Posted on March 21, 2014 at 00:15

er : ..\main.c(589): error:  #20: identifier ''DMA1_Channel1_IRQn'' is undefined

Yeah, because that's not how it's done, I'll play this game but you'll need to post complete code, not out of context fragments.

DMA1_Stream1_IRQn ?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on March 21, 2014 at 04:24

// STM32F4-Discovery ADC1 3CH DMA IRQ - sourcer32@gmail.com
#include ''stm32f4_discovery.h''
//******************************************************************************
#define SAMPLES (3 * 2) // 3 SAMPLES, TWICE
volatile uint16_t ADCConvertedValues[SAMPLES];
//******************************************************************************
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the ADC1 gloabal Interrupt */
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 GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC, ENABLE);
/* Configure ADC1 Channel8 pin as analog input PB0 */
/* Configure ADC1 Channel9 pin as analog input PB1 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure ADC1 Channel12 pin as analog input PC2 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
//******************************************************************************
void ADC1_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
DMA_InitTypeDef DMA_InitStructure;
/* Enable peripheral clocks */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* DMA2_Stream0 channel0 configuration */
DMA_DeInit(DMA2_Stream0);
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADCConvertedValues[0];
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = SAMPLES;
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_Circular;
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);
/* DMA2_Stream0 enable */
DMA_Cmd(DMA2_Stream0, ENABLE);
/* Enable DMA Stream Half Transfer and Transfer Complete interrupts */
DMA_ITConfig(DMA2_Stream0, DMA_IT_HT, ENABLE);
DMA_ITConfig(DMA2_Stream0, DMA_IT_TC, ENABLE);
/* ADC Common Init */
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);
/* ADC1 Init */
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = ENABLE; // Multiple Channels
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; // Keep Sampling
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 3;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel configuration ******************************/
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_480Cycles); // PB0
ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 2, ADC_SampleTime_480Cycles); // PB1
ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 3, ADC_SampleTime_480Cycles); // PC2 (BANKC)
/* Enable DMA request after last transfer (Single-ADC mode) */
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConv(ADC1);
}
//******************************************************************************
void DMA2_Stream0_IRQHandler(void)
{
/* Test on DMA Stream Half Transfer interrupt */
if (DMA_GetITStatus(DMA2_Stream0, DMA_IT_HTIF0))
{
static int i = 0;
/* Clear DMA Stream Half Transfer interrupt pending bit */
DMA_ClearITPendingBit(DMA2_Stream0, DMA_IT_HTIF0);
/* Toggle LED3 */
if ((i++ % 1000) == 0)
STM_EVAL_LEDToggle(LED3); /* Toggle Red LED */
/* Add code to process First Half of Buffer */
}
/* Test on DMA Stream Transfer Complete interrupt */
if (DMA_GetITStatus(DMA2_Stream0, DMA_IT_TCIF0))
{
static int i = 0;
/* Clear DMA Stream Transfer Complete interrupt pending bit */
DMA_ClearITPendingBit(DMA2_Stream0, DMA_IT_TCIF0);
/* Toggle LED4 */
if ((i++ % 1000) == 0)
STM_EVAL_LEDToggle(LED4); /* Toggle Green LED */
/* Add code to process Second Half of Buffer */
}
}
//******************************************************************************
int main(void)
{
GPIO_Configuration();
STM_EVAL_LEDInit(LED3); // Red
STM_EVAL_LEDOn(LED3);
STM_EVAL_LEDInit(LED4); // Green
STM_EVAL_LEDOff(LED4);
NVIC_Configuration();
ADC1_Configuration();
while(1); /* Infinite loop */
}
/**************************************************************************/
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf(''Wrong parameters value: file %s on line %d

'', file, line) */
while (1)
{}
}
#endif
/**************************************************************************/

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
haythem
Associate II
Posted on March 22, 2014 at 17:39

Thank you very much for your help now am looking for a trick to run me two function in parallel (which are not binding on either the adc if a tim) I sanded used the NVIC but I is not how the configured

Posted on March 23, 2014 at 02:10

Ok, so you need to manage things in interrupt handlers, and a foreground while loop, OR you need to invest some time/effort investigating the use of an RTOS

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..