OpenEdv-开源电子网

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

STM32同一组IO口可以分开初始化吗?

[复制链接]

2

主题

5

帖子

0

精华

新手上路

积分
33
金钱
33
注册时间
2014-7-27
在线时间
0 小时
发表于 2014-8-18 15:59:13 | 显示全部楼层 |阅读模式
5金钱


  GPIO_InitTypeDef GPIO_InitStructure;
  RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE); 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;   
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; 
  GPIO_Init(GPIOB, &GPIO_InitStructure); 


  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; 

  GPIO_Init(GPIOB, &GPIO_InitStructure); 

请教下 以上配置可以吗?

下面的配置会不会把上面的清除了?

我知道GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10; 是可以的。


最佳答案

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

你调用了GPIO_Init就初始化了对应的GPIO_Pin指定的IO口 第二次你调用GPIO_Init因为你的入口参数没变,还是&GPIO_InitStructure ,只是成员变量GPIO_Pin 换为 GPIO_Pin_9;  所以自然第二次初始化的入口参数跟第一次只有io口不一样,速度什么的都一样啦。。。。 C语言基础。
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

230

主题

1950

帖子

10

精华

论坛元老

Rank: 8Rank: 8

积分
4562
金钱
4562
注册时间
2010-12-14
在线时间
32 小时
发表于 2014-8-18 15:59:14 | 显示全部楼层
你调用了GPIO_Init就初始化了对应的GPIO_Pin指定的IO口
第二次你调用GPIO_Init因为你的入口参数没变,还是&GPIO_InitStructure ,只是成员变量GPIO_Pin 换为 GPIO_Pin_9; 
所以自然第二次初始化的入口参数跟第一次只有io口不一样,速度什么的都一样啦。。。。
C语言基础。
我是开源电子网?网站管理员,对网站有任何问题,请与我联系!QQ:389063473Email:389063473@qq.com
回复

使用道具 举报

0

主题

77

帖子

0

精华

初级会员

Rank: 2

积分
144
金钱
144
注册时间
2014-6-7
在线时间
7 小时
发表于 2014-8-18 16:04:42 | 显示全部楼层
当然不会,依然是或的关系
回复

使用道具 举报

2

主题

5

帖子

0

精华

新手上路

积分
33
金钱
33
注册时间
2014-7-27
在线时间
0 小时
 楼主| 发表于 2014-8-20 20:51:36 | 显示全部楼层
回复【3楼】 Admin :
---------------------------------
你好!抱歉那么晚回复!
我后来又看了下GPIO_Init函数 最下面设置时是使用的GPIOx->CRH = tmpreg;不是或GPIOx->CRH |= 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;
  }
}
回复

使用道具 举报

230

主题

1950

帖子

10

精华

论坛元老

Rank: 8Rank: 8

积分
4562
金钱
4562
注册时间
2010-12-14
在线时间
32 小时
发表于 2014-8-20 21:54:40 | 显示全部楼层
最开头就是tempreg=GPIOx->CRH;
先获取原有值了。。。。
我是开源电子网?网站管理员,对网站有任何问题,请与我联系!QQ:389063473Email:389063473@qq.com
回复

使用道具 举报

2

主题

5

帖子

0

精华

新手上路

积分
33
金钱
33
注册时间
2014-7-27
在线时间
0 小时
 楼主| 发表于 2014-8-20 22:16:02 | 显示全部楼层
回复【5楼】Admin:
---------------------------------
对哦,谢谢
回复

使用道具 举报

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

本版积分规则



关闭

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

正点原子公众号

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

GMT+8, 2025-7-2 11:45

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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