本文介绍了在STM32上直接使用ODR寄存器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改WriteLED()的代码以直接使用ODR寄存器.该代码应读取当前寄存器的值,然后写回修改的值,具体取决于要点亮的LED开启或关闭.

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

提供给我的示例代码

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;
}

需要更改的代码

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;
}

以上代码在输出到ODR寄存器时应该是什么样子

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

推荐答案

您需要了解C按位运算.这三个是^(异或),&(按位与),|(按位或)

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

要清除一点GPIO->ODR &= ~pin_mask;

要设置一点GPIO->ODR |= pin_mask;

这应该为您提供足够的信息.

This should give you enough information.

这篇关于在STM32上直接使用ODR寄存器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-13 13:06