‎2019-09-27 02:17 PM
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;
}
Solved! Go to Solution.
‎2019-09-27 02:30 PM
GPIO_InitTypeDef GPIO_InitStructure = {0};
‎2019-09-27 02:30 PM
GPIO_InitTypeDef GPIO_InitStructure = {0};
‎2019-09-27 11:42 PM
it's working now. Thanks. But I didn't understand how it is works?
‎2019-09-28 12:19 AM
Maybe actually learn C language? Read this and you will understand:
‎2019-09-28 01:00 AM
sorry, i think i couldn't explain problem clearly. With this code, here is the result.
And if ichange GPIO_PIN_12 to GPIO_PIN_15 (in line 9 and 11) i see that
‎2019-09-28 08:58 AM
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.
‎2019-09-28 10:48 AM
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. =)