cstm32halstm

How to turn off LED in stm32 onn board f103c6t6?


I have started studying stm32. I'm trying to blink LED PC13 on board stm32f103c6t6. I could turn on led, but when i tried to turn it off, it didn't work. The fragment of code is below.

/* USER CODE BEGIN WHILE */
  while (1)
  {
        GPIOC->ODR &= ~GPIO_ODR_ODR13;//turn PC_13 ON
        HAL_Delay(500);
    GPIOC->ODR &= GPIO_ODR_ODR13;//turn PC_13 OFF
    HAL_Delay(500);    
/* USER CODE END WHILE */
  
  }
 

Also i have tried another way, but it havent worked at all. The LED was turnned off. The fragment of code is below.

/* USER CODE BEGIN WHILE */
  while (1)
  {
    GPIOC->BSRR &=~ GPIO_BSRR_BS13;//turn PC_13 ON
    HAL_Delay(500);
    GPIOC->BSRR &=~ GPIO_BSRR_BS13;//turn PC_13 OFF
    HAL_Delay(500);
    /* USER CODE END WHILE */
    /* USER CODE BEGIN 3 */
  }

Also i tried only HAL funnctions(WritePin, TogglePin...), but LED also didn't feel them to turn it off.

while (1)
  {
    HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET);
    HAL_Delay(500);
    HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);
    HAL_Delay(500);
        
  }

Maybe because LED is inverted it isnt't turning off casually.


Solution

  • To set the bit you need to OR

        GPIOC->ODR |= GPIO_ODR_ODR13;//turn PC_13 OFF
    

    BSRR has to be assigned not RMW

        GPIOC->BSRR = GPIO_BSRR_BR13;//turn PC_13 ON
    
        GPIOC->BSRR = GPIO_BSRR_BS13;//turn PC_13 OFF