求助:STM32F的GPIO函数GPIO_Init,请大家帮忙

[复制链接]
4344|6
 楼主| wet999 发表于 2008-4-1 10:12 | 显示全部楼层 |阅读模式
  在GPIO.C函数中,有一个函数GPIO_Init,其中有几句看不懂:
  ......
  if(currentpin==pos)
   pos=pinpos<<2;
   pinmask=((u32)0x0F)<<pos;
   tmpreg&=~pinmask;
   tmpreg|=(currentmode<<pos);
  ......
   看了几天也没看懂,请大家帮助分析一下.

   
 楼主| wet999 发表于 2008-4-1 10:37 | 显示全部楼层

还是看不懂

 pos=pinpos<<2 ;左移2位,什么意思?为什么要左移2位?能说详细点吗?
 还有下面的0xOF左移POS位,什么意思?pinmask代表什么?
香水城 发表于 2008-4-1 11:18 | 显示全部楼层

在STM32固件库的源程序中有详细的注解

这一段是设置GPIO CRL寄存器,请参考STM32技术参考手册中的相关部分。


/*---------------------------- GPIO CRL Configuration ------------------------*/
  /* Configure the eight low port pins */
  if (((u32)GPIO_InitStruct->GPIO_Pin & ((u32)0x00FF)) != 0x00)
  {
    tmpreg = GPIOx->CRL;

    for (pinpos = 0x00; pinpos < 0x08; pinpos++)
    {
      pos = ((u32)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 = ((u32)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 = (((u32)0x01) << pinpos);
        }
        /* Set the corresponding ODR bit */
        if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
        {
          GPIOx->BSRR = (((u32)0x01) << pinpos);
        }
      }
    }
    GPIOx->CRL = tmpreg;
    tmpreg = 0;
  }
jessemok 发表于 2008-4-1 16:46 | 显示全部楼层

看相关寄存器说明

和操作的寄存器说明对照着看就能看懂了,要是再看不懂,估计没有能再回答你了。
chunfeng12 发表于 2008-4-1 17:41 | 显示全部楼层

呵呵

C语言基本的东东啊
americ 发表于 2008-4-1 18:03 | 显示全部楼层

ARM 寄存器 比 51 寄存器 功能复杂一点

针对某些应用,更方便了
short_long 发表于 2008-4-2 20:38 | 显示全部楼层

IO

看来楼主要加油了,呵呵
您需要登录后才可以回帖 登录 | 注册

本版积分规则

15

主题

37

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部