cancel
Showing results for 
Search instead for 
Did you mean: 

Button push not changing LED status

HPate.12
Associate II

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.

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

> 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.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

4 REPLIES 4
mƎALLEm
ST Employee

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 ...

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.

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.

Do you get a hardfault? put a break point in the hardfault interrupt handler..

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.
TDK
Guru

> 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.

If you feel a post has answered your question, please click "Accept as Solution".