smallsnail 发表于 2009-3-27 14:12:00

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

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的值在相对应引脚输出高低电平呢?
不明白,请各位指教。

ifree64 发表于 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位是用来设置是否输出高电平的。

cdh 发表于 2012-5-9 10:01:08

如2楼所说。库的GPIO.h也有解释的,看2楼所贴。不过貌似库的解释不太完整,我根据自己的理解补充下。错了别打脸{:lol:}

/**
* @briefGPIO modes
*
* Bits definitions:
* - Bit 7: 0 = INPUT mode
*         1 = OUTPUT mode
* - Bit 60 = 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)
*/
页: [1]
查看完整版本: 关于STM8固件库GPIO初始化函数的一个小小疑问