0
\$\begingroup\$

Could you please advice me where I make the mistake, the LED is on but it does not blink.

#include "stm32l0xx_hal.h"

int main(void){
    HAL_Init();
    GPIO_InitTypeDef GPIO_InitStruct;
    __HAL_RCC_GPIOA_CLK_ENABLE();


    GPIO_InitStruct.Pin = GPIO_PIN_0;
    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);

    while(1){
        HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_0);
        HAL_Delay(100);
    }
}

This is the bare metal code which works.

#include <stdint.h>
int main(void)
{

volatile uint32_t *RCC_IOPENR = 0x0;
RCC_IOPENR = (uint32_t*)(0x40021000+0x2C);
*RCC_IOPENR |=0x1;

volatile uint32_t *GPIOA_MODER=0x0, *GPIOA_ODR=0x0;

GPIOA_MODER = (uint32_t*)0x50000000; //MODER BASE ADDRES
GPIOA_ODR = (uint32_t*)(0x50000000+0x14); //ODR BASE ADDRESS
*GPIOA_MODER = (*GPIOA_MODER & ~0x03) | 0x1; // SET MODER BIT TO 1,


while (1){

//GPIOA PORT, PIN 0 BLINK (PA0) by changing ODR register bit
*GPIOA_ODR = *GPIOA_ODR | 0x1; //ON
for(int i=0; i<50000; i++){} // DELAY
*GPIOA_ODR = *GPIOA_ODR & 0x0; //OFF
for(int i=0; i<50000; i++){} //DELAY

}
}
\$\endgroup\$
4
  • \$\begingroup\$ (1) How have you connected the LED? (2) What colour is the LED? (3) Did you use a series resistor? What value? (4) What is the supply voltage of the STM microcontroller? Pop the info into your question rather than in the comments. \$\endgroup\$
    – Transistor
    Jan 31, 2020 at 18:10
  • \$\begingroup\$ I think the problem is not in electronics, its in this code, because when i use bare metal code it blinks. \$\endgroup\$
    – Atrahasis
    Jan 31, 2020 at 18:13
  • \$\begingroup\$ Did you generate code with CubeMX tool? HAL_Delay() needs a millisecond timer interrupts, does the HAL_Init() initialize the systick timer or other timer to get periodic interrupts every millisecond? Are interrupts enabled? Is the interrupt vector hooked to the routine that increases the milliseconds variable? CubeMX would do all these for you. \$\endgroup\$
    – Justme
    Jan 31, 2020 at 18:15
  • \$\begingroup\$ No I did not generated code with CubeMx. This is the only code i wrote. I am just calling HAL_Init() . \$\endgroup\$
    – Atrahasis
    Jan 31, 2020 at 18:17

1 Answer 1

0
\$\begingroup\$

You need to configure your system clock. Cube generated code calls this function SystemClock_Config(). Also, you may want to use HAL_GPIO_WritePin() to reset your pin before initializing it, although I don't believe it is necessary.

\$\endgroup\$
1

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.