Using the ODR register directly On STM32

2020-05-10 08:35发布

Modify the code for WriteLED() to use the ODR register directly. The code should read the current value of the register and then write back a modified value depending on what LEDs are to be turns on or off.

Example Code Given to me

void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
/* Check the parameters */
assert_param(IS_GPIO_PIN(GPIO_Pin));
GPIOx->ODR ˆ= GPIO_Pin;
}

Code That Needs to be Altered

WriteLED (uint8_t LED, uint8_t State)
{
  // Check for correct state
 if ((State != LED_OFF) && (State != LED_ON))
  {
    return;
  }

  // Turn on/off the LED
  switch (LED)
    {
    case 'L':
      HAL_GPIO_WritePin (LD4_GPIO_Port, LD4_Pin, State);
      break;
    case 'T':
      HAL_GPIO_WritePin (LD3_GPIO_Port, LD3_Pin, State);
      break;
    case 'B':
      HAL_GPIO_WritePin (LD6_GPIO_Port, LD6_Pin, State);
      break;
    case 'R':
      HAL_GPIO_WritePin (LD5_GPIO_Port, LD5_Pin, State);
      break;
    }

  return;
}

What should the above code look like when outputting to ODR register

标签: c stm32 gpio
1条回答
淡お忘
2楼-- · 2020-05-10 09:04

You need to understand C bitwise operations. The three are ^ (exclusive OR), & (bitwise and), | (bitwise or)

To clear a bit GPIO->ODR &= ~pin_mask;

To set a bit GPIO->ODR |= pin_mask;

This should give you enough information.

查看更多
登录 后发表回答