你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

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

[复制链接]
Ξ长征 提问时间:2017-12-8 10:22 /
STM8S103F3P6  IO口 D4 , 我现在要用一个引脚驱动继电器,高电平有效.
GPIO_Init(GPIOD, GPIO_PIN_4, GPIO_MODE_OUT_PP_HIGH_SLOW);

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

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

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

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




收藏 评论5 发布时间:2017-12-8 10:22

举报

5个回答
七哥 回答时间: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的东西有点乱,有的是寄存局,有的是函数



/**
  * @brief  Initializes the GPIOx according to the specified parameters.
  * @param  GPIOx : Select the GPIO peripheral number (x = A to I).
  * @param  GPIO_Pin : This parameter contains the pin number, it can be any value
  *         of the @ref GPIO_Pin_TypeDef enumeration.
  * @param  GPIO_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

点评

楼主分享下解决方法呢  发表于 2018-2-11 13:30

所属标签

相似问题

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版