2019-07-09 09:20 PM
Dear All,
i encountered a problem with STM32F407ZGT6 in working with external interrupt.
i do this by STM32CubeMX and MDK ARM V5.
i configure a pin ( PF1 ) as external interrupt mode with rising/falling edge trigger detection. i set it internally pull down and connect it to a micro switch.
my problem: when i press the micro switch i expect that the void HAL_GPIO_EXTI_Callback(ut16 GPIO_Pin) executed two times. one time for rising edge and one time for falling edge. but it is executed three times. also when i press and hold down the micro switch, the callback routine executes repeatedly.
i see it in debug mode by inserting breakpoint in the above mentioned callback.
what is the cause of the problem and what is the solution for it?
please consider the my code following.
thanks in advance.
#include "main.h"
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 144;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin : PF1 */
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI1_IRQn, 1, 0);
HAL_NVIC_EnableIRQ(EXTI1_IRQn);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
/* USER CODE END Error_Handler_Debug */
}
#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 CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
void EXTI1_IRQHandler(void)
{
/* USER CODE BEGIN EXTI1_IRQn 0 */
/* USER CODE END EXTI1_IRQn 0 */
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_1);
/* USER CODE BEGIN EXTI1_IRQn 1 */
/* USER CODE END EXTI1_IRQn 1 */
}
void HAL_GPIO_EXTI_Callback(ut16 GPIO_Pin)
{
HAL_NVIC_DisableIRQ(EXTI1_IRQn);
if ( GPIO_Pin == GPIO_PIN_1 )
{
for ( uint16_t i1=0;i1<1000;i1++){};// de bounce
if ( HAL_GPIO_ReadPin(GPIOF,GPIO_PIN_1)==0)// falling
{
;// code for falling
}
if ( HAL_GPIO_ReadPin(GPIOF,GPIO_PIN_1)==0)// rising
{
;// code for rising
}
}
HAL_NVIC_EnableIRQ(EXTI1_IRQn);
}
2019-07-09 10:40 PM
On these parts if the interrupt source is not cleared it will keep reentering.
Suggest you scope the signal. And pass the input signal to a different output pin you can also scope.
2019-07-09 10:49 PM
thank you Clive,
please see the code:
void EXTI1_IRQHandler(void)
{
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_1);
}
void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
{
if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
{
__HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
HAL_GPIO_EXTI_Callback(GPIO_Pin);
}
}
void HAL_GPIO_EXTI_Callback(ut16 GPIO_Pin)
{
HAL_NVIC_DisableIRQ(EXTI1_IRQn);
if ( GPIO_Pin == GPIO_PIN_1 )
{
for ( uint16_t i1=0;i1<1000;i1++){};// de bounce
if ( HAL_GPIO_ReadPin(GPIOF,GPIO_PIN_1)==0)// falling
{
;// code for falling
}
if ( HAL_GPIO_ReadPin(GPIOF,GPIO_PIN_1)==0)// rising
{
;// code for rising
}
}
2019-07-09 11:49 PM
It bounces. What's so unexpected here? Look at section "BUT I ONLY PRESSED IT ONCE!":
https://www.embeddedrelated.com/showarticle/462.php
for ( uint16_t i1=0;i1<1000;i1++){};// de bounce
And that is not a de-bounce in any way.
2019-07-12 02:47 AM
thank you Piranha,
i do the debounce by integration followed by a schmitt triger in software and eliminated the switch bounce.
but another problem arises.
please consider the thread bellow:
https://community.st.com/s/question/0D50X0000B44j2XSQQ/problem-with-stm32f47-external-interrupt
Regards, Ras