cancel
Showing results for 
Search instead for 
Did you mean: 

I'm trying to blink one led but two led blinks

ahmetgunduz
Associate III

Hi,

i'm trying to blink only one led. I have a problem with one of four leds. Because as you in my code, i'm trying to blink only 'green led'. But 'blue led' blinks also.

#include "stm32f4xx.h"                  // Device header
#include "stm32f4xx_hal_gpio.h"
 
int main ()
{
	RCC->AHB1ENR |= (4<<1); //initiliazing clock
	GPIO_InitTypeDef GPIO_InitStructure;	
	GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; //push-pull 
        GPIO_InitStructure.Pin = GPIO_PIN_12; //green led
	HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
	HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12); //blue led blinks also!
	return 0;
}

1 ACCEPTED SOLUTION

Accepted Solutions

GPIO_InitTypeDef GPIO_InitStructure = {0};

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

6 REPLIES 6

GPIO_InitTypeDef GPIO_InitStructure = {0};

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

it's working now. Thanks. But I didn't understand how it is works?

Maybe actually learn C language? Read this and you will understand:

https://www.tutorialspoint.com/cprogramming/c_variables.htm

sorry, i think i couldn't explain problem clearly. With this code, here is the result. 0690X000009jne6QAA.jpg

And if ichange GPIO_PIN_12 to GPIO_PIN_15 (in line 9 and 11) i see that

0690X000009jneGQAQ.jpg

The problem with the implementation is that the local/auto variables are allocated from the stack, and don't have explicit initialization. The result is that the structure can contain random junk and this subsequently gets injected into the peripheral configuration registers, undefined/unexpected behaviour can then occur.

By clearing the structure the behaviour becomes significantly more predictable. Generally it is advisable to initialize all fields in a structure that down stream code might depend upon.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

We understood the problem and Clive even solved it for you. Now, to fully understand it, read the tutorial section I gave a link for. =)