2025-05-13 11:38 AM - edited 2025-05-13 12:19 PM
I am working with the NUCLEO-C031C6 board and tried to to make the user push button toggle the user LED but it stays stuck with no change to the buttonStatus variable, always high (1).
I added the counter variable to see if it is continuously incrementing, however, it appears that the code does not loop and gets stuck somewhere, and thus when I press the push button, it is not updating the buttonStatus variable.
Can you please see where the issue is:
#include "stm32c0xx_hal.h"
//Blue button = PC13, BUS AHB1 EN bit 0
#define BLUBTN_Port GPIOC
#define BLUBTN_Pin GPIO_PIN_13
//Green LED = PA5, BUS AHB1 EN bit 2
#define GRNLED_Port GPIOA
#define GRNLED_Pin GPIO_PIN_5
void PA5_LED_init(void)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GRNLED_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GRNLED_Port, &GPIO_InitStruct);
}
void PC13_BLUBTN_init(void)
{
__HAL_RCC_GPIOC_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = BLUBTN_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(BLUBTN_Port, &GPIO_InitStruct);
}
void Systick_Handler (void)
{
HAL_IncTick();
}
uint8_t buttonStatus;
int counter;
int main()
{
HAL_Init();
PA5_LED_init();
PC13_BLUBTN_init();
while(1)
{
buttonStatus = HAL_GPIO_ReadPin(BLUBTN_Port, BLUBTN_Pin);
HAL_GPIO_WritePin(GRNLED_Port, GRNLED_Pin, buttonStatus);
counter++;
/*if (buttonStatus == 0)
{
//HAL_GPIO_WritePin(GRNLED_Port, GRNLED_Pin, GPIO_PIN_SET);
HAL_GPIO_TogglePin(GRNLED_Port, GRNLED_Pin);
HAL_Delay(200);
}*/
}
}
I suspect it has something to do with the clock / systick handler but not sure how to resolve this.
Solved! Go to Solution.
2025-05-13 12:52 PM
> Systick_Handler
It should be spelled SysTick_Handler. Functions are case sensitive.
The code probably gets stuck in Default_Handler when the first systick happens because of this.
2025-05-13 12:10 PM - edited 2025-05-13 12:10 PM
Hello,
First, please kindly use </> button to paste your code. Refer to this post. I've already modified your psot.
Second, you need to find where it stuck. Stop the debug and find at which line it stops. It could be something related to the RCC configuration: the clocks or the Flash wait state configs ...
2025-05-13 12:23 PM
Thank you for that.
The counter value stays at stuck at 125 (sometimes 124) and does not increment any further. I am sure where it is getting stuck.
2025-05-13 12:32 PM
Do you get a hardfault? put a break point in the hardfault interrupt handler..
2025-05-13 12:52 PM
> Systick_Handler
It should be spelled SysTick_Handler. Functions are case sensitive.
The code probably gets stuck in Default_Handler when the first systick happens because of this.