搜索
bottom↓
回复: 2

关于STM8固件库GPIO初始化函数的一个小小疑问

[复制链接]

出0入0汤圆

发表于 2009-3-27 14:12:00 | 显示全部楼层 |阅读模式
void GPIO_Init(GPIO_TypeDef* GPIOx,
               GPIO_Pin_TypeDef GPIO_Pin,
               GPIO_Mode_TypeDef GPIO_Mode)
{
  /*----------------------*/
  /* Check the parameters */
  /*----------------------*/

  assert_param(IS_GPIO_MODE_OK(GPIO_Mode));
  assert_param(IS_GPIO_PIN_OK(GPIO_Pin));

  /*-----------------------------*/
  /* Input/Output mode selection */
  /*-----------------------------*/

  if ((((u8)(GPIO_Mode)) & (u8)0x80) != (u8)0x00) /* Output mode */
  {
    if ((((u8)(GPIO_Mode)) & (u8)0x10) != (u8)0x00) /* High level */
    {
      GPIOx->ODR |= (u8)GPIO_Pin;
    } else /* Low level */
    {
      GPIOx->ODR &= (u8)(~(GPIO_Pin));
    }

    /* Set Output mode */
    GPIOx->DDR |= (u8)GPIO_Pin;
  } else /* Input mode */
  {
    /* Set Input mode */
    GPIOx->DDR &= (u8)(~(GPIO_Pin));
  }

  /*------------------------------------------------------------------------*/
  /* Pull-Up/Float (Input) or Push-Pull/Open-Drain (Output) modes selection */
  /*------------------------------------------------------------------------*/

  if ((((u8)(GPIO_Mode)) & (u8)0x40) != (u8)0x00) /* Pull-Up or Push-Pull */
  {
    GPIOx->CR1 |= (u8)GPIO_Pin;
  } else /* Float or Open-Drain */
  {
    GPIOx->CR1 &= (u8)(~(GPIO_Pin));
  }

  /*-----------------------------------------------------*/
  /* Interrupt (Input) or Slope (Output) modes selection */
  /*-----------------------------------------------------*/

  if ((((u8)(GPIO_Mode)) & (u8)0x20) != (u8)0x00) /* Interrupt or Slow slope */
  {
    GPIOx->CR2 |= (u8)GPIO_Pin;
  } else /* No external interrupt or No slope control */
  {
    GPIOx->CR2 &= (u8)(~(GPIO_Pin));
  }

}
-------------------------------------------------
这个函数是GPIO的初始化函数,在判断完设定为输出口后的红色部分是什么意思呢?为什么要根据GPIO_Pin的值在相对应引脚输出高低电平呢?
不明白,请各位指教。

阿莫论坛20周年了!感谢大家的支持与爱护!!

一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。

出0入0汤圆

发表于 2009-3-28 12:42:56 | 显示全部楼层
if ((((u8)(GPIO_Mode)) & (u8)0x10) != (u8)0x00) /* High level */
是这句话要求将io口设置为高或低电平

00036 /**
00037   * @brief GPIO modes
00038   *
00039   * Bits definitions:
00040   * - Bit 7: 0 = INPUT mode
00041   *          1 = OUTPUT mode
00042   *          1 = PULL-UP (input) or PUSH-PULL (output)
00043   * - Bit 5: 0 = No external interrupt (input) or No slope control (output)
00044   *          1 = External interrupt (input) or Slow control enabled (output)
00045   * - Bit 4: 0 = Low level (output)
00046   *          1 = High level (output push-pull) or HI-Z (output open-drain)
00047   */
00048 typedef enum
00049 {
00050   GPIO_MODE_IN_FL_NO_IT      = (u8)0b00000000,  /*!< Input floating, no external interrupt */
00051   GPIO_MODE_IN_PU_NO_IT      = (u8)0b01000000,  /*!< Input pull-up, no external interrupt */
00052   GPIO_MODE_IN_FL_IT         = (u8)0b00100000,  /*!< Input floating, external interrupt */
00053   GPIO_MODE_IN_PU_IT         = (u8)0b01100000,  /*!< Input pull-up, external interrupt */
00054   GPIO_MODE_OUT_OD_LOW_FAST  = (u8)0b10000000,  /*!< Output open-drain, low level, no slope control */
00055   GPIO_MODE_OUT_PP_LOW_FAST  = (u8)0b11000000,  /*!< Output push-pull, low level, no slope control */
00056   GPIO_MODE_OUT_OD_LOW_SLOW  = (u8)0b10100000,  /*!< Output open-drain, low level, slow slope */
00057   GPIO_MODE_OUT_PP_LOW_SLOW  = (u8)0b11100000,  /*!< Output push-pull, low level, slow slope */
00058   GPIO_MODE_OUT_OD_HIZ_FAST  = (u8)0b10010000,  /*!< Output open-drain, high-impedance level, no slope control */
00059   GPIO_MODE_OUT_PP_HIGH_FAST = (u8)0b11010000,  /*!< Output push-pull, high level, no slope control */
00060   GPIO_MODE_OUT_OD_HIZ_SLOW  = (u8)0b10110000,  /*!< Output open-drain, high-impedance level, slow slope */
00061   GPIO_MODE_OUT_PP_HIGH_SLOW = (u8)0b11110000   /*!< Output push-pull, high level, slow slope */
00062 }GPIO_Mode_TypeDef;

这里说GPIO_Mode的第4位是用来设置是否输出高电平的。

出0入0汤圆

发表于 2012-5-9 10:01:08 | 显示全部楼层
如2楼所说。库的GPIO.h也有解释的,看2楼所贴。不过貌似库的解释不太完整,我根据自己的理解补充下。错了别打脸

/**
  * @brief  GPIO modes
  *
  * Bits definitions:
  * - Bit 7: 0 = INPUT mode
  *           1 = OUTPUT mode
  * - Bit 6  0 = floating (Input) or open-drain (Output)
  *           1 = PULL-UP (input) or PUSH-PULL (output)
  * - Bit 5: 0 = No external interrupt (input) or No slope control / SLOW (output)
  *           1 = External interrupt (input) or Slow control enabled / FAST (output)
  * - Bit 4: 0 = Low level (output)
  *           1 = High level (output push-pull) or HI-Z (output open-drain)
  */
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-3-30 00:03

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表