cancel
Showing results for 
Search instead for 
Did you mean: 

problems with interrupts in stm32 - timer interrupts and external interrupts

arsalankeivanrad
Associate II

i have an issue with my timer interrupt that every 500 us happends and external interrupts (12EXTI) .
when the timer interrupt happend , the external interrupts never execute.
i dont know why this is happening.
i mean it supposed to pending for other interrupts but it looks like this is not happening

5 REPLIES 5
LCE
Principal

Let me guess... oops, not possible without further info. ;)

So please:

- which STM32?

- show some relevant source code (interrupt enable, priorities, handlers)

stm32f446


void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)


{

// Set the counter value to zero

htim3.Instance->CNT = 0;

n2 = 0;

HAL_ADC_Start(&hadc2);

HAL_ADC_Start(&hadc3);


if (HAL_ADC_PollForConversion(&hadc2, 1000) == HAL_OK)

{


adc_value2 = HAL_ADC_GetValue(&hadc2);

}

if (HAL_ADC_PollForConversion(&hadc3, 1000) == HAL_OK)

{


adc_value3 = HAL_ADC_GetValue(&hadc3);

}

if( adc_value3 > data_max )

{

n2 = 1;

adc_value3 = adc_value2 ;

}

if(n2 == 0)

{

adc_value3 = adc_value3/2 ;

}

//Filter Part


data_dac = ( 2.3/3 )* data;

HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, data_dac);

if (data <= ( limitPercentage1 * data_max ) )

{

if ( ( HAL_GPIO_ReadPin(GPIOB, RELAY_GPIO_PIN) == GPIO_PIN_RESET ) && ( HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_4) == GPIO_PIN_SET ) )

{

//ON RELAY

HAL_GPIO_WritePin(GPIOB, RELAY_GPIO_PIN, GPIO_PIN_SET); //A0

//OFF RED LED

HAL_GPIO_WritePin(GPIOB, LED_GPIO_PIN, GPIO_PIN_RESET);

//Give permission to operator to reset micro

HAL_GPIO_WritePin(GPIOB , GPIO_PIN_5 , GPIO_PIN_SET); //A1

}

}

else

{

//HAL_Delay( lowestDelay1 * 1000 );

//Turn off Relay

HAL_GPIO_WritePin(GPIOB, RELAY_GPIO_PIN, GPIO_PIN_RESET); //A0

//ON RED LED

HAL_GPIO_WritePin(GPIOB, LED_GPIO_PIN, GPIO_PIN_SET);

//Get permission off operator to reset micro

HAL_GPIO_WritePin(GPIOB , GPIO_PIN_5 , GPIO_PIN_RESET);

}


// Set the counter value to zero

htim3.Instance->CNT = 0;

}


void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)

{
/* Prevent unused argument(s) compilation warning */
UNUSED(GPIO_Pin);

while(keyOk < 3 )
{
lowestDelay = 0;
setGain = 0;
gainStatus = 0;

if (HAL_GPIO_ReadPin(GPIOC, GAIN_CONTROL_PIN_1) == GPIO_PIN_SET) {
gainStatus++;
setGain = 10;
}

else if (HAL_GPIO_ReadPin(GPIOB, GAIN_CONTROL_PIN_2) == GPIO_PIN_SET) {
gainStatus++;
setGain = 25;
}

else if (HAL_GPIO_ReadPin(GPIOC, GAIN_CONTROL_PIN_3) == GPIO_PIN_SET) {
gainStatus++;
setGain = 50;
}

else if (HAL_GPIO_ReadPin(GPIOC, GAIN_CONTROL_PIN_4) == GPIO_PIN_SET) {
gainStatus++;
setGain = 100;
}
else {
setGain = 300;

}
// Check if at least two GPIOs are set to 1
if (gainStatus < 2)
{
keyOk++;
setGain = setGain ;
if((keyOk > 2) & (n == 0))
{
keyOk = 2;
}
}
else
{
keyOk--;
if(keyOk < 0)
{
keyOk = 0 ;
}
}
// Calculate the delay percentage based on the active switches
limitPercentage = 0;
if (HAL_GPIO_ReadPin(GPIOC, LIMIT_CONTROL_PIN_1)== GPIO_PIN_SET)
{
limitPercentage += 10;
}
if (HAL_GPIO_ReadPin(GPIOC, LIMIT_CONTROL_PIN_2)== GPIO_PIN_SET)
{
limitPercentage += 20;
}
if (HAL_GPIO_ReadPin(GPIOA, LIMIT_CONTROL_PIN_3)== GPIO_PIN_SET)
{
limitPercentage += 40;
}
if (HAL_GPIO_ReadPin(GPIOA, LIMIT_CONTROL_PIN_4)== GPIO_PIN_SET)
{
limitPercentage += 80;
}

// Check if the sum exceeds 90%
if (limitPercentage <= 90)
{
keyOk++;
limitPercentage = limitPercentage ;
if((keyOk > 2) & (n == 0))
{
keyOk = 2;
}
}
else
{
keyOk--;
if(keyOk < 0)
{
keyOk = 0 ;
}
}
if (HAL_GPIO_ReadPin(GPIOA, DELAY_CONTROL_PIN_1)== GPIO_PIN_SET ) lowestDelay = 1;
if (HAL_GPIO_ReadPin(GPIOA, DELAY_CONTROL_PIN_2)== GPIO_PIN_SET ) lowestDelay = (lowestDelay == 0) ? 2 : lowestDelay;
if (HAL_GPIO_ReadPin(GPIOB, DELAY_CONTROL_PIN_3)== GPIO_PIN_SET ) lowestDelay = (lowestDelay == 0) ? 4 : ((lowestDelay < 4) ? lowestDelay : 4);
if (HAL_GPIO_ReadPin(GPIOB, DELAY_CONTROL_PIN_4)== GPIO_PIN_SET ) lowestDelay = (lowestDelay == 0) ? 8 : ((lowestDelay < 8) ? lowestDelay : 8);


if ( keyOk == 2)
{
keyOk = 1;
n = 1;

}
else if ( keyOk < 2)
{
keyOk = 0 ;
}
}
keyOk = 2 ;


//Set potentiometer Based on GPIO That connect by DIP switches
if (HAL_GPIO_ReadPin(GPIOC, GAIN_CONTROL_PIN_1) == GPIO_PIN_SET)
{
// Set the MCP41010 resistance to 3.7 kohms (0x70).
MCP41010_SetResistance(&hspi1, 0x70);
}
else if (HAL_GPIO_ReadPin(GPIOB, GAIN_CONTROL_PIN_2) == GPIO_PIN_SET)
{
// Set the MCP41010 resistance to 5 kohms (0xA0).
MCP41010_SetResistance(&hspi1, 0xA0);
}
else if (HAL_GPIO_ReadPin(GPIOC, GAIN_CONTROL_PIN_3) == GPIO_PIN_SET)
{
// Set the MCP41010 resistance to 7 kohms (0xD0).
MCP41010_SetResistance(&hspi1, 0xD0);
}
else if (HAL_GPIO_ReadPin(GPIOC, GAIN_CONTROL_PIN_4) == GPIO_PIN_SET)
{
// Set the MCP41010 resistance to 8.5 kohms (0xF0).
MCP41010_SetResistance(&hspi1, 0xF0);
}

limitPercentage1 = limitPercentage ;
lowestDelay1 = lowestDelay ;
HAL_ADC_Start(&hadc1);
HAL_ADC_Start(&hadc2);


if (HAL_ADC_PollForConversion(&hadc2, 1000) == HAL_OK) {

//m1 = HAL_ADC_GetValue(&hadc2);
HAL_GPIO_WritePin(GPIOB , GPIO_PIN_7 , GPIO_PIN_SET); // A3
//HAL_Delay(5);
while(1)
{
//m2 = HAL_ADC_GetValue(&hadc2);
//HAL_Delay(5);
if ( m2 > m1 )
{
m1 = m2;
n1 = 0;
}
else
{
n1++;
}
if (n1>=2)
{
// Read the value of setGain and Compare the range of m1 for each value
if(setGain == 10)
{
if( ( 100 < m1 ) && ( m1 < 200 ) )
{
//HAL_TIM_Base_Start_IT(&htim3);
break;
}
else
{
HAL_GPIO_WritePin(GPIOB , GPIO_PIN_8 , GPIO_PIN_SET); //RESET MICRO USING MAX6962
}
}
if(setGain == 25)
{
if( ( 100 < m1 ) & ( m1 < 200 ) )
{
//HAL_TIM_Base_Start_IT(&htim3);

break;
}
else
{
HAL_GPIO_WritePin(GPIOB , GPIO_PIN_8 , GPIO_PIN_SET); //RESET MICRO USING MAX6962
}
}
if(setGain == 50)
{
if( ( 100 < m1 ) & ( m1 < 200 ) )
{
//HAL_TIM_Base_Start_IT(&htim3);
break;
}
else
{
HAL_GPIO_WritePin(GPIOB , GPIO_PIN_8 , GPIO_PIN_SET); //RESET MICRO USING MAX6962
}
}
if(setGain == 100)
{
if( ( 100 < m1 ) & ( m1 < 200 ) )
{
//HAL_TIM_Base_Start_IT(&htim3);
break;
}
else
{
HAL_GPIO_WritePin(GPIOB , GPIO_PIN_8 , GPIO_PIN_SET); //RESET MICRO USING MAX6962
}
}
if(setGain == 300)
{
if( ( 100 < m1 ) & ( m1 < 200 ) )
{
//HAL_TIM_Base_Start_IT(&htim3);
break;
}
else
{
HAL_GPIO_WritePin(GPIOB , GPIO_PIN_8 , GPIO_PIN_SET); //RESET MICRO USING MAX6962
}
}
}
}
// 3 times toggle LED
HAL_GPIO_WritePin(GPIOB, LED_GPIO_PIN, GPIO_PIN_SET);
for (int q=0;q<60000;q++);
HAL_GPIO_WritePin(GPIOB, LED_GPIO_PIN, GPIO_PIN_RESET);
for (int q=0;q<60000;q++);
//HAL_Delay(300);
HAL_GPIO_WritePin(GPIOB, LED_GPIO_PIN, GPIO_PIN_SET);
for (int q=0;q<60000;q++) ;
//HAL_Delay(300);
HAL_GPIO_WritePin(GPIOB, LED_GPIO_PIN, GPIO_PIN_RESET);
for (int q=0;q<60000;q++);
// HAL_Delay(300);
HAL_GPIO_WritePin(GPIOB, LED_GPIO_PIN, GPIO_PIN_SET);
for (int q=0;q<60000;q++);
// HAL_Delay(300);
HAL_GPIO_WritePin(GPIOB, LED_GPIO_PIN, GPIO_PIN_RESET);
for (int q=0;q<60000;q++);
// HAL_Delay(300);

}
}

static void MX_TIM3_Init(void)

{

 

/* USER CODE BEGIN TIM3_Init 0 */

 

/* USER CODE END TIM3_Init 0 */

 

TIM_ClockConfigTypeDef sClockSourceConfig = {0};

TIM_MasterConfigTypeDef sMasterConfig = {0};

 

/* USER CODE BEGIN TIM3_Init 1 */

 

/* USER CODE END TIM3_Init 1 */

htim3.Instance = TIM3;

htim3.Init.Prescaler = 1000-1;

htim3.Init.CounterMode = TIM_COUNTERMODE_UP;

htim3.Init.Period = 45-1;

htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;

if (HAL_TIM_Base_Init(&htim3) != HAL_OK)

{

Error_Handler();

}

sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)

{

Error_Handler();

}

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)

{

Error_Handler();

}

HAL_NVIC_SetPriority(TIM3_IRQn, 0 , 0);

HAL_NVIC_EnableIRQ(TIM3_IRQn);

 

/* USER CODE BEGIN TIM3_Init 2 */

 

/* USER CODE END TIM3_Init 2 */

 

}

static void MX_GPIO_Init(void)

{

GPIO_InitTypeDef GPIO_InitStruct = {0};

/* USER CODE BEGIN MX_GPIO_Init_1 */

/* USER CODE END MX_GPIO_Init_1 */

 

/* GPIO Ports Clock Enable */

__HAL_RCC_GPIOH_CLK_ENABLE();

__HAL_RCC_GPIOC_CLK_ENABLE();

__HAL_RCC_GPIOA_CLK_ENABLE();

__HAL_RCC_GPIOB_CLK_ENABLE();

__HAL_RCC_GPIOD_CLK_ENABLE();

 

/*Configure GPIO pin Output Level */

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_11|GPIO_PIN_12, GPIO_PIN_RESET);

 

/*Configure GPIO pin Output Level */

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);

 

/*Configure GPIO pin Output Level */

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET);

 

/*Configure GPIO pins : PC3 PC4 PC5 */

GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5;

GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;

GPIO_InitStruct.Pull = GPIO_PULLDOWN;

HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

 

/*Configure GPIO pins : PB1 PB2 PB13 PB14

PB15 */

GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_13|GPIO_PIN_14

|GPIO_PIN_15;

GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;

GPIO_InitStruct.Pull = GPIO_PULLDOWN;

HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

 

/*Configure GPIO pins : PA9 PA10 PA11 PA12 */

GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12;

GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;

GPIO_InitStruct.Pull = GPIO_PULLDOWN;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

 

/*Configure GPIO pins : PC11 PC12 */

GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

 

/*Configure GPIO pin : PD2 */

GPIO_InitStruct.Pin = GPIO_PIN_2;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

 

/*Configure GPIO pins : PB3 PB5 PB6 PB7 */

GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

 

/*Configure GPIO pin : PB4 */

GPIO_InitStruct.Pin = GPIO_PIN_4;

GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

GPIO_InitStruct.Pull = GPIO_NOPULL;

HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

 

/* EXTI interrupt init*/

HAL_NVIC_SetPriority(EXTI1_IRQn, 1, 0);

HAL_NVIC_EnableIRQ(EXTI1_IRQn);

 

HAL_NVIC_SetPriority(EXTI2_IRQn, 1, 0);

HAL_NVIC_EnableIRQ(EXTI2_IRQn);

 

HAL_NVIC_SetPriority(EXTI3_IRQn, 1, 0);

HAL_NVIC_EnableIRQ(EXTI3_IRQn);

 

HAL_NVIC_SetPriority(EXTI4_IRQn, 1, 0);

HAL_NVIC_EnableIRQ(EXTI4_IRQn);

 

HAL_NVIC_SetPriority(EXTI9_5_IRQn, 1, 0);

HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);

 

HAL_NVIC_SetPriority(EXTI15_10_IRQn, 1, 0);

HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);

 

/* USER CODE BEGIN MX_GPIO_Init_2 */

/* USER CODE END MX_GPIO_Init_2 */

}

 

LCE
Principal

There is this nice little forum option "</>" for source code input, which is unfortunately hidden in the 2nd row, so first click this "...", then you see "</>".

Otherwise the code is not readable that well. And please only post what might actually be needed.

Here's the NVIc Config for External interrupts

/* EXTI interrupt init*/

HAL_NVIC_SetPriority(EXTI1_IRQn, 1, 0);

HAL_NVIC_EnableIRQ(EXTI1_IRQn);

HAL_NVIC_SetPriority(EXTI2_IRQn, 1, 0);

HAL_NVIC_EnableIRQ(EXTI2_IRQn);

HAL_NVIC_SetPriority(EXTI3_IRQn, 1, 0);

HAL_NVIC_EnableIRQ(EXTI3_IRQn);

HAL_NVIC_SetPriority(EXTI4_IRQn, 1, 0);

HAL_NVIC_EnableIRQ(EXTI4_IRQn);

HAL_NVIC_SetPriority(EXTI9_5_IRQn, 1, 0);

HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);

HAL_NVIC_SetPriority(EXTI15_10_IRQn, 1, 0);

HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
Here's The Callback Function for Timer 3 interrupt:
void
HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

// Set the counter value to zero

n2 = 0;

HAL_ADC_Start(&hadc2);

HAL_ADC_Start(&hadc3);

//if (HAL_ADC_PollForConversion(&hadc2, 1000) == HAL_OK)

adc_value2 = HAL_ADC_GetValue(&hadc2);

//if (HAL_ADC_PollForConversion(&hadc3, 1000) == HAL_OK)

adc_value3 = HAL_ADC_GetValue(&hadc3);

if( adc_value3 > data_max )

{

n2 = 1;

adc_value3 = adc_value2 ;

}

if(n2 == 0)

{

adc_value3 = adc_value3/2 ;

}

//Filter Part

data_dac = ( 2.3/3 )* data;

HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, data_dac);

if (data <= ( limitPercentage1 * data_max ) )

{

if ( ( HAL_GPIO_ReadPin(GPIOB, RELAY_GPIO_PIN) == GPIO_PIN_RESET ) && ( HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_4) == GPIO_PIN_SET ) )

{

//ON RELAY

HAL_GPIO_WritePin(GPIOB, RELAY_GPIO_PIN, GPIO_PIN_SET); //A0

//OFF RED LED

HAL_GPIO_WritePin(GPIOB, LED_GPIO_PIN, GPIO_PIN_RESET);

//Give permission to operator to reset micro

HAL_GPIO_WritePin(GPIOB , GPIO_PIN_5 , GPIO_PIN_SET); //A1

}

}

else

{

//HAL_Delay( lowestDelay1 * 1000 );

//Turn off Relay

HAL_GPIO_WritePin(GPIOB, RELAY_GPIO_PIN, GPIO_PIN_RESET); //A0

//ON RED LED

HAL_GPIO_WritePin(GPIOB, LED_GPIO_PIN, GPIO_PIN_SET);

//Get permission off operator to reset micro

HAL_GPIO_WritePin(GPIOB , GPIO_PIN_5 , GPIO_PIN_RESET);

}

}

Here's Timer3 Config:

htim3
.Instance = TIM3;

htim3.Init.Prescaler = 1000-1;

htim3.Init.CounterMode = TIM_COUNTERMODE_UP;

htim3.Init.Period = 9000-1;

htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;

if (HAL_TIM_Base_Init(&htim3) != HAL_OK)

{

Error_Handler();

}

sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)

{

Error_Handler();

}

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)

{

Error_Handler();

}

HAL_NVIC_SetPriority(TIM3_IRQn, 0 , 0);

HAL_NVIC_EnableIRQ(TIM3_IRQn);