cancel
Showing results for 
Search instead for 
Did you mean: 

Coding Error

raisa_razak
Associate III

This is a portion of my product code, but i got some problem. When an external interrupt occurs i want to start a timer and increment one variable and when timer reaches 30 seconds i want to display the variable via UART. I will provide my code.

when i give the external interrupt my timer is working for 30 seconds but the variable is not getting incremented and displaying variable = 1 after 30 seconds via uart.

 

/* USER CODE BEGIN PV */

uint8_t ex_interrupt = 0;

uint32_t variable = 0;

uint32_t seconds = 0;

uint8_t uartBuffer[50];

/* USER CODE END PV */

 

/* Initialize all configured peripherals */

MX_GPIO_Init();

MX_USART2_UART_Init();

MX_TIM3_Init();

/* USER CODE BEGIN 2 */

 

/* USER CODE END 2 */

 

/* Infinite loop */

/* USER CODE BEGIN WHILE */

while (1)

{

if(ex_interrupt == 1)

{

ex_interrupt = 0;

if (HAL_TIM_Base_Start_IT(&htim3) != HAL_OK)

{

/* Starting Error */

Error_Handler();

}

variable = variable + 1;

}

if(seconds == 30)

{

HAL_TIM_Base_Stop_IT(&htim3);

sprintf((char *)uartBuffer, "VARIABLE: %lu\r\n", variable);

HAL_UART_Transmit(&huart2, uartBuffer, strlen((char *)uartBuffer), 100);

seconds = 0;

variable = 0;

}

/* USER CODE END WHILE */

 

/* USER CODE BEGIN 3 */

}

 

/* USER CODE BEGIN PV */

extern seconds;

/* USER CODE END PV */

 

void TIM3_IRQHandler(void)

{

/* USER CODE BEGIN TIM3_IRQn 0 */

 

/* USER CODE END TIM3_IRQn 0 */

HAL_TIM_IRQHandler(&htim3);

/* USER CODE BEGIN TIM3_IRQn 1 */

seconds = seconds + 1;

/* USER CODE END TIM3_IRQn 1 */

}

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

 Hi,

Thank you for your reply. The problem was sorted.

You only need to set a flag, then it will work properly.

 

Here is my updated code:

 

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)

{

ex_interrupt = 1;

flag = 1;

}

if(ex_interrupt == 1)

{

ex_interrupt = 0;

if (HAL_TIM_Base_Start_IT(&htim3) != HAL_OK)

{

/* Starting Error */

Error_Handler();

}

variable = variable + 1;

}

if(flag == 1)

{

variable = variable + 1;

}

if(seconds == 30)

{

HAL_TIM_Base_Stop(&htim3);

sprintf((char *)uartBuffer, "VARIABLE: %lu\r\n", variable);

HAL_UART_Transmit(&huart2, uartBuffer, strlen((char *)uartBuffer), 100);

seconds = 0;

variable = 0;

flag = 0;

}

 

View solution in original post

2 REPLIES 2
Sarra.S
ST Employee

Hello @raisa_razak

 

I think you are not properly handling the interrupt. When the external interrupt occurs, you are starting the timer and incrementing the variable, but you are not resetting the seconds variable to 0. This means that (seconds == 30) will never be true.

Also, make sure that the external interrupt is properly configured and that it is actually triggering the code inside the if statement.

Maybe use EXTI (External interrupt) and then in EXTI_Callback, increment your variable 

Something like this : 

 

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
  // Check if the interrupt is from the correct pin
  if (GPIO_Pin == EXT_INT_PIN)
  {
    // Increment the count variable
    variable++;
  }
}

 

The timer has to be configured to generate an interrupt every second and the interrupt handler checks if 30 seconds have elapsed and sends the count variable via UART if it has. 

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

 Hi,

Thank you for your reply. The problem was sorted.

You only need to set a flag, then it will work properly.

 

Here is my updated code:

 

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)

{

ex_interrupt = 1;

flag = 1;

}

if(ex_interrupt == 1)

{

ex_interrupt = 0;

if (HAL_TIM_Base_Start_IT(&htim3) != HAL_OK)

{

/* Starting Error */

Error_Handler();

}

variable = variable + 1;

}

if(flag == 1)

{

variable = variable + 1;

}

if(seconds == 30)

{

HAL_TIM_Base_Stop(&htim3);

sprintf((char *)uartBuffer, "VARIABLE: %lu\r\n", variable);

HAL_UART_Transmit(&huart2, uartBuffer, strlen((char *)uartBuffer), 100);

seconds = 0;

variable = 0;

flag = 0;

}