OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 7666|回复: 7

GPIO的上拉下拉输入模式

[复制链接]

145

主题

445

帖子

0

精华

高级会员

Rank: 4

积分
823
金钱
823
注册时间
2014-1-15
在线时间
350 小时
发表于 2014-11-28 10:52:39 | 显示全部楼层 |阅读模式
5金钱


左图是寄存器,可以看到上拉下拉输入都是0x10,说明设置寄存器时只能填0X10,而库里面则有上拉下拉两个可以定义。。。

最佳答案

查看完整内容[请看2#楼]

是的  上拉下拉输入模式都是设置0x01    但具体是上拉还是下拉  是通过对端口 置位(上拉)和复位(下拉)设置的 库函数的上下拉在GPIOx_Init里完成   寄存器的则要自己配置  (GPIOx_ODR)
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

3

主题

2165

帖子

0

精华

资深版主

Rank: 8Rank: 8

积分
5773
金钱
5773
注册时间
2013-11-22
在线时间
1211 小时
发表于 2014-11-28 10:52:40 | 显示全部楼层
是的  上拉下拉输入模式都是设置0x01   
但具体是上拉还是下拉  是通过对端口 置位(上拉)和复位(下拉)设置的
库函数的上下拉在GPIOx_Init里完成   寄存器的则要自己配置  (GPIOx_ODR)
回复

使用道具 举报

58

主题

6294

帖子

1

精华

资深版主

Rank: 8Rank: 8

积分
11537
金钱
11537
注册时间
2014-4-1
在线时间
1314 小时
发表于 2014-11-28 15:15:57 | 显示全部楼层
不太明白,LZ是什么意思?
回复

使用道具 举报

20

主题

232

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
335
金钱
335
注册时间
2013-6-1
在线时间
0 小时
发表于 2014-11-28 16:05:41 | 显示全部楼层
GPIO_Mode_IPD = 0x28,
GPIO_Mode_IPU = 0x48,
这个两个的低位都是1000,即配置CNFy=10,MODEx=00(上下拉输入) 
高位一个是0010一个是0100,详情请见stm32f10x_gpio.c这个文件中的
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct) GPIO初始化函数,
在函数的最后可以看到以下代码:
        /* Reset the corresponding ODR bit */
        if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
        {
          GPIOx->BRR = (((uint32_t)0x01) << pinpos);
        }
        else
        {
          /* Set the corresponding ODR bit */
          if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
          {
            GPIOx->BSRR = (((uint32_t)0x01) << pinpos);
          }
        }
如果是IPD则配置端口位清除寄存器BRR,如果是IPU则配置端口位设置/清除寄存器BSRR。
说明高位只是用来区分设置的模式是IPU还是IPD,从而用来初始化IO端口的输出用的。
所以0010和0100是决定初始化时IO端口的输出电平是高还是低,则高位起得是一个区分作用。
回复

使用道具 举报

145

主题

445

帖子

0

精华

高级会员

Rank: 4

积分
823
金钱
823
注册时间
2014-1-15
在线时间
350 小时
 楼主| 发表于 2014-11-28 17:01:29 | 显示全部楼层
回复【3楼】xuande:
---------------------------------
比如:不论设置PA1为上拉输入还是下拉输入,使用寄存器时都是将相应为设置为0x10;而使用库函数时可单独设置为上拉输入或下拉输入。
回复

使用道具 举报

145

主题

445

帖子

0

精华

高级会员

Rank: 4

积分
823
金钱
823
注册时间
2014-1-15
在线时间
350 小时
 楼主| 发表于 2014-11-28 17:03:54 | 显示全部楼层
回复【4楼】Scy:
---------------------------------
谢谢,对库函数的具体内容很陌生,不知道怎么分析,分析来分析去都跑到assert_param去了
回复

使用道具 举报

58

主题

6294

帖子

1

精华

资深版主

Rank: 8Rank: 8

积分
11537
金钱
11537
注册时间
2014-4-1
在线时间
1314 小时
发表于 2014-11-28 17:11:54 | 显示全部楼层
回复【5楼】Di诺:
---------------------------------

你意思是说,寄存器只能设置为“要么不拉,要么上下一起拉”?

恐怕哪里搞错了。

库函数能做到的,寄存器一定能做到。寄存器更接地气。
回复

使用道具 举报

12

主题

68

帖子

0

精华

初级会员

Rank: 2

积分
176
金钱
176
注册时间
2016-9-20
在线时间
56 小时
发表于 2016-11-8 17:57:18 | 显示全部楼层
[mw_shl_code=c,true]void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
{
  uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;
  uint32_t tmpreg = 0x00, pinmask = 0x00;
  /* Check the parameters */
  assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
  assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));  
  
/*---------------------------- GPIO Mode Configuration -----------------------*/
  currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F);
  if ((((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00)
  {
    /* Check the parameters */
    assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
    /* Output mode */
    currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed;
  }
/*---------------------------- GPIO CRL Configuration ------------------------*/
  /* Configure the eight low port pins */
  if (((uint32_t)GPIO_InitStruct->GPIO_Pin & ((uint32_t)0x00FF)) != 0x00)
  {
    tmpreg = GPIOx->CRL;
    for (pinpos = 0x00; pinpos < 0x08; pinpos++)
    {
      pos = ((uint32_t)0x01) << pinpos;
      /* Get the port pins position */
      currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
      if (currentpin == pos)
      {
        pos = pinpos << 2;
        /* Clear the corresponding low control register bits */
        pinmask = ((uint32_t)0x0F) << pos;
        tmpreg &= ~pinmask;
        /* Write the mode configuration in the corresponding bits */
        tmpreg |= (currentmode << pos);
        /* Reset the corresponding ODR bit */
        if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
        {
          GPIOx->BRR = (((uint32_t)0x01) << pinpos);
        }
        else
        {
          /* Set the corresponding ODR bit */
          if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
          {
            GPIOx->BSRR = (((uint32_t)0x01) << pinpos);
          }
        }
      }
    }
    GPIOx->CRL = tmpreg;
  }
/*---------------------------- GPIO CRH Configuration ------------------------*/
  /* Configure the eight high port pins */
  if (GPIO_InitStruct->GPIO_Pin > 0x00FF)
  {
    tmpreg = GPIOx->CRH;
    for (pinpos = 0x00; pinpos < 0x08; pinpos++)
    {
      pos = (((uint32_t)0x01) << (pinpos + 0x08));
      /* Get the port pins position */
      currentpin = ((GPIO_InitStruct->GPIO_Pin) & pos);
      if (currentpin == pos)
      {
        pos = pinpos << 2;
        /* Clear the corresponding high control register bits */
        pinmask = ((uint32_t)0x0F) << pos;
        tmpreg &= ~pinmask;
        /* Write the mode configuration in the corresponding bits */
        tmpreg |= (currentmode << pos);
        /* Reset the corresponding ODR bit */
        if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
        {
          GPIOx->BRR = (((uint32_t)0x01) << (pinpos + 0x08));
        }
        /* Set the corresponding ODR bit */
        if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
        {
          GPIOx->BSRR = (((uint32_t)0x01) << (pinpos + 0x08));
        }
      }
    }
    GPIOx->CRH = tmpreg;
  }
}
[/mw_shl_code]
这是GPIO_Init()函数,ODR还是要自己配置的吧?库函数的配置方式求解啊、、
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-5-24 21:33

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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