-1
\$\begingroup\$

I am trying to blink the led(PA5) when PC13 Button is clicked. But it doesnot work.

main.c

#include "main.h"
#include "stm32l0xx_hal.h"

void SystemClock_Config(void);
static void MX_GPIO_Init(void);

int main(void)
 {
   HAL_Init();
   SystemClock_Config();
   MX_GPIO_Init();

   while (1)
    {
    }
  }

void SystemClock_Config(void)
  {

    RCC_OscInitTypeDef RCC_OscInitStruct;
    RCC_ClkInitTypeDef RCC_ClkInitStruct;

    /**Configure the main internal regulator output voltage 
    */
    __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

    /**Initializes the CPU, AHB and APB busses clocks 
    */
   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
   RCC_OscInitStruct.MSIState = RCC_MSI_ON;
   RCC_OscInitStruct.MSICalibrationValue = 0;
   RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;
   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
     {
      _Error_Handler(__FILE__, __LINE__);
     }

    /**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_MSI;
   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

    if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
   {
     _Error_Handler(__FILE__, __LINE__);
   }

   /**Configure the Systick interrupt time  */
   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

   /**Configure the Systick  */
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

static void MX_GPIO_Init(void)
  {

    GPIO_InitTypeDef GPIO_InitStruct;

   /* GPIO Ports Clock Enable */
    __HAL_RCC_GPIOC_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();

   /*Configure GPIO pin Output Level */
   HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);

  /*Configure GPIO pin : PC13 */
  GPIO_InitStruct.Pin = GPIO_PIN_13;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  /*Configure GPIO pin : PA5 */
  GPIO_InitStruct.Pin = GPIO_PIN_5;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* EXTI interrupt init*/
  HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);
}

void _Error_Handler(char *file, int line)
 {
  while(1)
  {
 }
}

stm3l0xx_it.c - interrupt file

#include "stm32l0xx_hal.h"
#include "stm32l0xx.h"
#include "stm32l0xx_it.h"

void EXTI4_15_IRQHandler(void)
 {
    HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_13);
 }

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){
    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
    HAL_Delay(500);
}
\$\endgroup\$
6
  • \$\begingroup\$ An advice: do not put delays into the IRQ handler. \$\endgroup\$ May 7, 2018 at 10:58
  • \$\begingroup\$ Thanks for your reply. I have removed HAL_Delay() from callback function; however, it still does not work. \$\endgroup\$
    – Atrahasis
    May 7, 2018 at 11:04
  • \$\begingroup\$ I'm assuming you used MxCube for this. Two things: 1. Speed of PC13 pin is not configured, 2. Have you checked your startup.s file ,is this interrupt you want to handle mentioned there? \$\endgroup\$
    – Abel Tom
    May 7, 2018 at 11:50
  • \$\begingroup\$ Hi Abel, Yes i have configured it using CubeMx. I have added speed for PC13 and double checked that EXTI4_15_IRQHandler exists in startup.s file. \$\endgroup\$
    – Atrahasis
    May 7, 2018 at 11:58
  • 1
    \$\begingroup\$ 1. No delays, 2. No EXTI for buttons 3. You forgot to enable and set the EXTI 4. Do not post the same questions here and in the SO. \$\endgroup\$ May 7, 2018 at 16:39

1 Answer 1

0
\$\begingroup\$

If you have generated the code with CubeMx you should have some confined spaces to write the code. I have generated the code with a F serie, and CubeMx generate something which for your serie should be:

void EXTI4_15_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI4_15_IRQn 0 */
-----------toggle pin----------------------
  /* USER CODE END EXTI4_15_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_13);
  /* USER CODE BEGIN EXTI4_15_IRQn 1 */
-----------toggle pin----------------------
  /* USER CODE END EXTI4_15_IRQn 1 */
}

And this code must to run OK. You could place the toggle pin function in one of this spaces.

The function:

HAL_GPIO_EXTI_Callback();

to me, in F series, is created inside the function HAL_GPIO_EXTI_IRQHandler like this:

void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
{
  /* EXTI line interrupt detected */
  if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
  {
    __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
    HAL_GPIO_EXTI_Callback(GPIO_Pin);
  }
}

and if in the L serie is thus too, you are writting in HAL drivers, and it is not necessary.

Furthermore you should to debug the code, and testing NVIC and these two pins PA5 and PC13, because this is something very simple for works badly.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.