Ξ长征 发表于 2017-12-8 10:22:01

新手问一下简单的问题,关于GPIO

STM8S103F3P6IO口 D4 , 我现在要用一个引脚驱动继电器,高电平有效.
GPIO_Init(GPIOD, GPIO_PIN_4, GPIO_MODE_OUT_PP_HIGH_SLOW);

我程序现在是可以正常操作这个引脚的,可是我发现,设备一上电,一执行初始化这个语句,
这个引脚默认就是高电平.我的继电器就会动作,虽然我可以在之后把它设成低电平, 但是刚上电还是会跳一些

有没有一设置就是默认低电平啊,

我看好多设置IO脚初始化完后默认就是低电平,为什么我的不是?

我把这个引脚直接接电阻再接个LED,再接地,默认就是亮的.
除非初始化完以后,我程序置低.




toofree 发表于 2017-12-8 11:10:48

IO一般默认是输入,你可以先写输出寄存器对应位为0,然后再设置IO输出方向。
这样,一切到IO输出方向,IO立马变为0输出,不会让继电器动作的。

anywill 发表于 2017-12-8 11:18:31

本帖最后由 anywill 于 2017-12-8 11:19 编辑

引脚默认就是低电平
建议直接用cubemx设置引脚为低,再上电试试

Ξ长征 发表于 2017-12-8 14:27:32

toofree 发表于 2017-12-8 11:10
IO一般默认是输入,你可以先写输出寄存器对应位为0,然后再设置IO输出方向。
这样,一切到IO输出方向,IO立 ...

不行,
       GPIO_WriteLow(GPIOD, GPIO_PIN_4);
   GPIO_Init(GPIOD, GPIO_PIN_4, GPIO_MODE_OUT_PP_HIGH_SLOW);

这么设以后,执行完 引脚还是高电平


这是函数, STM8的东西有点乱,有的是寄存局,有的是函数



/**
* @briefInitializes the GPIOx according to the specified parameters.
* @paramGPIOx : Select the GPIO peripheral number (x = A to I).
* @paramGPIO_Pin : This parameter contains the pin number, it can be any value
*         of the @ref GPIO_Pin_TypeDef enumeration.
* @paramGPIO_Mode : This parameter can be a value of the
*         @Ref GPIO_Mode_TypeDef enumeration.
* @retval None
*/

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));
   
/* Reset corresponding bit to GPIO_Pin in CR2 register */
GPIOx->CR2 &= (uint8_t)(~(GPIO_Pin));

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

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

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

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

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

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

Ξ长征 发表于 2017-12-8 14:36:28

啊,去一百多个论坛发问,终于有个靠谱的回答了,啊搞定了.
GPIO_MODE_OUT_PP_LOW_SLOW
GPIO_MODE_OUT_PP_HIGH_SLOW
页: [1]
查看完整版本: 新手问一下简单的问题,关于GPIO