STM32标准库源码分析——GPIO_Init()

源函代码分步拆解

/*---------------------------- GPIO Mode Configuration -----------------------*/
1  currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F);
2  if ((((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00)
3  { 
4    /* Check the parameters */
5    assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
6    /* Output mode */
7    currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed;
  }

1行:保留二进制后四位, GPIO_Mode可以取的值有:

typedef enum
{ GPIO_Mode_AIN = 0x0,             //0000 0000
  GPIO_Mode_IN_FLOATING = 0x04,    //0000 0100
  GPIO_Mode_IPD = 0x28,            //0010 1000
  GPIO_Mode_IPU = 0x48,            //0100 1000
  GPIO_Mode_Out_OD = 0x14,         //0001 0100
  GPIO_Mode_Out_PP = 0x10,         //0001 0000
  GPIO_Mode_AF_OD = 0x1C,		   //0001 1100
  GPIO_Mode_AF_PP = 0x18           //0001 1000
}GPIOMode_TypeDef;

转换为八位二进制之后可以发现二进制后四位才是我们要写进寄存器的值(注意:四种输出模式还没有加上速度,所以最后两位为00)。所以GPIO_Mode & 0x0F 将前四位置0,后四位保留。

2~7行:为输出模式添加速度,因为只有四种输出模式的前四位为 0001 ,所以使用GPIO_Mode & 0x10 来判断是否为输出模式。

/*---------------------------- GPIO CRL Configuration ------------------------*/
  /* Configure the eight low port pins */
1  if (((uint32_t)GPIO_InitStruct->GPIO_Pin & ((uint32_t)0x00FF)) != 0x00)
2  {
3    tmpreg = GPIOx->CRL;
4    for (pinpos = 0x00; pinpos < 0x08; pinpos++)
5    {
6      pos = ((uint32_t)0x01) << pinpos;
7      /* Get the port pins position */
8      currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
9      if (currentpin == pos)
10      {
11        pos = pinpos << 2;
12        /* Clear the corresponding low control register bits */
13        pinmask = ((uint32_t)0x0F) << pos;
14        tmpreg &= ~pinmask;
15        /* Write the mode configuration in the corresponding bits */
16        tmpreg |= (currentmode << pos);
17        /* Reset the corresponding ODR bit */
18        if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
19        {
20          GPIOx->BRR = (((uint32_t)0x01) << pinpos);
21        }
22        else
23        {
24          /* Set the corresponding ODR bit */
25          if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
26          {
27            GPIOx->BSRR = (((uint32_t)0x01) << pinpos);
28          }
29        }
30      }
31    }
32    GPIOx->CRL = tmpreg;
  }

这一部分代码配置 GPIO_Pin_0 ~ GPIO_Pin_7
1行: 判断引脚值是否合理
4~9行:判断为几号引脚,因为引脚的宏定义如下图,通过循环对GPIO_Pin的后八位每位都进行一次 & 1 操作,如果该位为1则 & 1 后值为 1 ,如GPIO_Pin_1 = 0000 0010,第二次循环时pinpos = 1,所以pos = 0000 0010,GPIO_Pin & pos = 0000 0010,即 currentpin = 0000 0010 , currentpin == pos 即GPIO_Pin右边第二位为1,所以为GPIO_Pin_1。

#define GPIO_Pin_0                 ((uint16_t)0x0001)  //0000 0000 0000 0001
#define GPIO_Pin_1                 ((uin00t16_t)0x0002)  //0000 0000 0000 0010
#define GPIO_Pin_2                 ((uint16_t)0x0004)  //0000 0000 0000 0100 
#define GPIO_Pin_3                 ((uint16_t)0x0008)  //0000 0000 0000 1000
#define GPIO_Pin_4                 ((uint16_t)0x0010)  //0000 0000 0001 0000
#define GPIO_Pin_5                 ((uint16_t)0x0020)  //0000 0000 0010 0000
#define GPIO_Pin_6                 ((uint16_t)0x0040)  //0000 0000 0100 0000
#define GPIO_Pin_7                 ((uint16_t)0x0080)  //0000 0000 1000 0000
#define GPIO_Pin_8                 ((uint16_t)0x0100)  //0000 0001 0000 0000
#define GPIO_Pin_9                 ((uint16_t)0x0200)  //0000 0010 0000 0000
#define GPIO_Pin_10                ((uint16_t)0x0400)  //0000 0100 0000 0000
#define GPIO_Pin_11                ((uint16_t)0x0800)  //0000 1000 0000 0000
#define GPIO_Pin_12                ((uint16_t)0x1000)  //0001 0000 0000 0000
#define GPIO_Pin_13                ((uint16_t)0x2000)  //0010 0000 0000 0000
#define GPIO_Pin_14                 ((uint16_t)0x4000) //0100 0000 0000 0000
#define GPIO_Pin_15                ((uint16_t)0x8000)  //1000 0000 0000 0000
#define GPIO_Pin_All               ((uint16_t)0xFFFF)  //1111 1111 1111 1111

11行:因为CRL寄存器4位为一组,所以 pos = pinpos << 2 = pinpos * 4
13~14行:将相应的 4 位设置为 0
(uint32_t) 0x0F = 0000 … 0000 0000 1111
pinmask = 0x0F << pos = 0000 …0000 1111 0000 … 0000
~ pinmask = 1111 …1111 0000 1111 … 1111
tmpreg &= ~ pinmask = xxxx … xxxx 0000 xxxx … xxxx
16行:将相应的4位设置为配置的模式
18~27行:根据官方参考手册可知,如下表 20 ,如果模式设置为上拉输入时要将BSRR寄存器相应的位置1,设置为下拉输入时要将BRR寄存器相应的位置1。

在这里插入图片描述
32行:将配置好的tmpreg写入CRL寄存器。

/*---------------------------- 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;
  }

这一部分配置GPIO_Pin_8 ~ GPIO_Pin_15 ,配置过程和上面的基本一样

第一章 硬件篇..............................................................................................................9 1.1 ALIENTEK MiniSTM32开发板简介...................................................................10 1.2 ALIENTEK MiniSTM32开发板硬件详解.......................................................16 1.2.1 MCU.................................................................................................................16 1.2.2 EEPROM..........................................................................................................17 1.2.3 温度传感器.....................................................................................................18 1.2.4 按键.................................................................................................................19 1.2.5 液晶显示模块.................................................................................................19 1.2.6 红外接收头.....................................................................................................20 1.2.7 PS/2...................................................................................................................20 1.2.8 LED..................................................................................................................21 1.2.9 SD卡................................................................................................................21 1.2.10 无线模块.......................................................................................................22 1.2.11 SPI FLASH.....................................................................................................23 1.2.12 USB串口、USB、电源................................................................................23 1.3 ALIENTEK MiniSTM32开发板使用注意事项..................................................25 第二章 软件篇............................................................................................................26 2.1 摘要.....................................................................................................................27 2.2 RVMDK3.80A简介...............................................................................................27 2.3 新建RVMDK工程...............................................................................................27 2.4 软件仿真...............................................................................................................36 2.5 程序下载...............................................................................................................42 2.6 在线调试...............................................................................................................48 2.7 SYSTEM文件夹介绍...........................................................................................51 2.7.1 delay文件夹....................................................................................................51 2.7.2 sys文件夹........................................................................................................54 2.7.3 usart文件夹介绍.............................................................................................65 2.8 RVMDK使用技巧..............................................................................................67 II 2.8.1 文本美化.........................................................................................................67 2.8.2 代码编辑技巧.................................................................................................71 2.8.3 调试技巧.........................................................................................................76 第三章 实战篇............................................................................................................81 3.1 跑马灯实验...........................................................................................................82 3.1.1 STM32 IO简介................................................................................................83 3.1.2 硬件设计.........................................................................................................85 3.1.3 软件设计.........................................................................................................86 3.1.4 仿真与下载.....................................................................................................89 3.2 按键输入实验.......................................................................................................92 3.2.1 STM32 IO口简介............................................................................................93 3.2.2 硬件设计.........................................................................................................93 3.2.3 软件设计.........................................................................................................94 3.2.4 仿真与下载.....................................................................................................99 3.3 串口实验.............................................................................................................103 3.3.1 STM32串口简介...........................................................................................104 3.3.2 硬件设计.......................................................................................................106 3.3.3 软件设计.......................................................................................................107 3.3.4 仿真与下载...................................................................................................110 3.4 外部中断实验.....................................................................................................112 3.4.1 STM32外部中断简介...................................................................................113 3.4.2 硬件设计.......................................................................................................113 3.4.3 软件设计.......................................................................................................113 3.4.4 下载与测试...................................................................................................116 3.5 独立看门狗(IWDG)实验..............................................................................117 3.5.1 STM32独立看门狗简介...............................................................................119 3.5.2 硬件设计.......................................................................................................121 3.5.3 软件设计.......................................................................................................121 3.5.4 下载与测试...................................................................................................123 3.6 窗口门狗(WWDG)实验................................................................................124 3.6.1 STM32窗口看门狗简介...............................................................................125 3.6.2 硬件设计.......................................................................................................127 III 3.6.3 软件设计.......................................................................................................127 3.6.4 下载与测试...................................................................................................128 3.7 定时器中断实验.................................................................................................130 3.7.1 STM32通用定时器简介...............................................................................131 3.7.2 硬件设计.......................................................................................................135 3.7.3 软件设计.......................................................................................................135 3.7.4 下载与测试...................................................................................................137 3.8 PWM输出实验....................................................................................................138 3.8.1 PWM简介......................................................................................................138 3.8.2 硬件设计.......................................................................................................140 3.8.3 软件设计.......................................................................................................140 3.8.4 下载与测试...................................................................................................142 3.9 OLED显示实验..................................................................................................143 3.9.1 OLED简介....................................................................................................144 3.9.2 硬件设计.......................................................................................................150 3.9.3 软件设计.......................................................................................................151 3.9.4 下载与测试...................................................................................................163 3.10 TFTLCD显示实验............................................................................................164 3.10.1 TFTLCD简介..............................................................................................165 3.10.2 硬件设计.....................................................................................................169 3.10.3 软件设计.....................................................................................................170 3.10.4 下载与测试.................................................................................................177 3.11 RTC实时时钟实验............................................................................................179 3.11.1 STM32 RTC时钟简介.................................................................................180 3.11.2 硬件设计.....................................................................................................185 3.11.3 软件设计.....................................................................................................185 3.11.4 下载与测试.................................................................................................193 3.12 待机唤醒实验...................................................................................................195 3.12.1 STM32待机模式简介.................................................................................196 3.12.2 硬件设计.....................................................................................................200 3.12.3 软件设计.....................................................................................................200 3.12.4 下载与测试.................................................................................................203 IV 3.13 ADC实验...........................................................................................................204 3.13.1 STM32 ADC简介........................................................................................205 3.13.2 硬件设计.....................................................................................................210 3.13.3 软件设计.....................................................................................................211 3.13.3 下载与测试.................................................................................................214 3.14内部温度传感器实验........................................................................................215 3.14.1 STM32 内部温度传感器简介....................................................................216 3.14.2 硬件设计.....................................................................................................216 3.14.3 软件设计.....................................................................................................216 3.14.4 下载与测试.................................................................................................219 3.15 DMA实验..........................................................................................................221 3.15.1 STM32 DMA简介.......................................................................................222 3.15.2 硬件设计.....................................................................................................225 3.15.3 软件设计.....................................................................................................225 3.15.4 下载与测试.................................................................................................229 3.16 IIC实验..............................................................................................................231 3.16.1 IIC简介........................................................................................................232 3.16.2 硬件设计.....................................................................................................232 3.16.3 软件设计.....................................................................................................233 3.16.4 下载与测试.................................................................................................243 3.17 SPI 实验............................................................................................................245 3.17.1 SPI 简介......................................................................................................246 3.17.2 硬件设计.....................................................................................................247 3.17.3 软件设计.....................................................................................................248 3.17.4 下载与测试.................................................................................................259 3.18 触摸屏实验.......................................................................................................262 3.18.1 触摸屏简介.................................................................................................263 3.18.2 硬件设计.....................................................................................................264 3.18.3 软件设计.....................................................................................................264 3.18.4 下载与测试.................................................................................................277 3.19 无线通信实验.................................................................................................279 3.19.1 NRF24L01无线模块简介...........................................................................280 V 3.19.2 硬件设计.....................................................................................................280 3.19.3 软件设计.....................................................................................................282 3.19.4 下载与测试.................................................................................................291 3.20 SD卡实验.......................................................................................................293 3.20.1 SD卡简介....................................................................................................294 3.20.2 硬件设计.....................................................................................................296 3.20.3 软件设计.....................................................................................................297 3.20.4 下载与测试.................................................................................................316 3.21 红外遥控实验.................................................................................................318 3.21.1红外遥控简介..............................................................................................319 3.21.2 硬件设计.....................................................................................................320 3.21.3 软件设计.....................................................................................................321 3.21.4 下载与测试.................................................................................................326 3.22.1 DS18B20简介.............................................................................................329 3.22.2 硬件设计.....................................................................................................330 3.22.3 软件设计.....................................................................................................331 3.22.4 下载与测试.................................................................................................336 3.23 PS2鼠标实验..................................................................................................337 3.23.1 PS/2简介......................................................................................................338 3.23.2 硬件设计.....................................................................................................340 3.23.3 软件设计.....................................................................................................341 3.23.4 下载与测试.................................................................................................353 3.24 汉字显示实验...................................................................................................354 3.24.1 汉字显示原理简介.....................................................................................355 3.24.2 硬件设计.....................................................................................................359 3.24.3 软件设计.....................................................................................................359 3.24.4 下载与测试.................................................................................................395 3.25 图片显示实验...................................................................................................397 3.25.1 图片显示原理简介.....................................................................................398 3.25.2 硬件设计.....................................................................................................398 3.25.3 软件设计.....................................................................................................399 3.25.4 下载与测试.................................................................................................434 VI 3.26 触控USB鼠标实验.........................................................................................435 3.26.1 USB简介.....................................................................................................436 3.26.2 硬件设计.....................................................................................................436 3.26.3 软件设计.....................................................................................................437 3.26.4 下载与测试.................................................................................................440 3.27 USB读卡器实验...............................................................................................442 3.27.1 USB读卡器简介.........................................................................................443 3.27.2 硬件设计.....................................................................................................443 3.27.3 软件设计.....................................................................................................443 3.27.4 下载与测试.................................................................................................446 3.28 综合测试实验...................................................................................................448 3.28.1 系统启动.....................................................................................................449 3.28.2 电子图书.....................................................................................................450 3.28.3 数码相框.....................................................................................................454 3.28.4 拼图游戏.....................................................................................................457 3.28.5 触摸画板.....................................................................................................465 3.28.6 系统时间.....................................................................................................468 3.28.7 鼠标画板.....................................................................................................469 3.28.8 USB连接..................................................................................................472 3.28.9 红外遥控.....................................................................................................475 3.28.10 无线传书...................................................................................................477
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值