|
端口复用的定义 STM32有许多的内置外设(如串口、ADC、DCA等等),这些外设的外部引脚都是和GPIO复用的。也就是说,一个GPIO如果可以复用为内置外设的功能引脚,那么当这个GPIO作为内置外设使用的时候,就叫复用。详细的可以参考《STM32F103ZET6数据手册》p30的内容,表格的倒数第二栏就表示端口复用功能。 比如说,STM32的串口1的引脚对应的I/O位PA9、PA10。而PA9、PA10默认的功能都是GPIO,所以说当PA9、PA10引脚作为串口1使用的时候就是端口复用。 在配置GPIO引脚功能时需要打开对应端口(port)所在的总线时钟,例如: - #define RCC_AHB1Periph_GPIOA ((uint32_t)0x00000001)0 y& L( |/ q* ?+ U
- #define RCC_AHB1Periph_GPIOB ((uint32_t)0x00000002)
1 y0 ?, R0 }+ D/ Q - #define RCC_AHB1Periph_GPIOC ((uint32_t)0x00000004)
3 R* l% r1 F" T+ ]% D4 Y - #define RCC_AHB1Periph_GPIOD ((uint32_t)0x00000008)
7 o9 p' F3 N% W3 d - #define RCC_AHB1Periph_GPIOE ((uint32_t)0x00000010)% O. r4 H. \4 N
- #define RCC_AHB1Periph_GPIOF ((uint32_t)0x00000020): _) F9 O9 h6 A! r% `
- #define RCC_AHB1Periph_GPIOG ((uint32_t)0x00000040)- G& ?4 L' f; C8 A) ^; a
- #define RCC_AHB1Periph_GPIOH ((uint32_t)0x00000080)
: ]8 f' j! ~+ A! _ - #define RCC_AHB1Periph_GPIOI ((uint32_t)0x00000100)
7 x# P0 L ?4 i% C$ [ - #define RCC_AHB1Periph_GPIOJ ((uint32_t)0x00000200)
6 V. P+ H, W; c - #define RCC_AHB1Periph_GPIOK ((uint32_t)0x00000400)
1 P d, t2 |1 \; f& `: R - #define RCC_AHB1Periph_CRC ((uint32_t)0x00001000)! \ ?- |0 b. L0 i( K2 I
- #define RCC_AHB1Periph_FLITF ((uint32_t)0x00008000)' M* T, j. G3 _5 S
- #define RCC_AHB1Periph_SRAM1 ((uint32_t)0x00010000)
- i5 p8 U' D7 i+ r. x( [ - #define RCC_AHB1Periph_SRAM2 ((uint32_t)0x00020000)
) ~: c" k2 b E! R4 e) z - #define RCC_AHB1Periph_BKPSRAM ((uint32_t)0x00040000), b0 q3 ]# \% P
- #define RCC_AHB1Periph_SRAM3 ((uint32_t)0x00080000)
6 N0 ^$ G4 w% u: N) v - #define RCC_AHB1Periph_CCMDATARAMEN ((uint32_t)0x00100000)( Y2 L0 I8 ]& q, h& h
- #define RCC_AHB1Periph_DMA1 ((uint32_t)0x00200000)
% O1 |6 v. `7 g: p% t! l - #define RCC_AHB1Periph_DMA2 ((uint32_t)0x00400000), S) l/ o9 }1 ?8 n0 t8 R
- #define RCC_AHB1Periph_DMA2D ((uint32_t)0x00800000)/ h; }* N/ P y3 e; ~' v9 ?
- #define RCC_AHB1Periph_ETH_MAC ((uint32_t)0x02000000), {* }4 X. T: g
- #define RCC_AHB1Periph_ETH_MAC_Tx ((uint32_t)0x04000000)! D2 ?, `' x @
- #define RCC_AHB1Periph_ETH_MAC_Rx ((uint32_t)0x08000000)
& g' T9 x X4 F3 B) K - #define RCC_AHB1Periph_ETH_MAC_PTP ((uint32_t)0x10000000)
2 P: X- Q& J# u- f - #define RCC_AHB1Periph_OTG_HS ((uint32_t)0x20000000)
9 v V* ?% ]% \9 Q6 o8 Z$ e" w - #define RCC_AHB1Periph_OTG_HS_ULPI ((uint32_t)0x40000000)
复制代码 6 Z' ]4 R6 n1 I/ G: l3 ?2 a. a
端口复用初始化过程接下来看一下端口复用初始化过程的步骤,拿串口1为例: 1、GPIO端口时钟使能。要使用到端口复用,首先是要使能端口的时钟了; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); 2、复用的外设时钟使能。比如要将PA9、PA10引脚复用成串口,必须也要使能串口时钟; ; l$ G* q% D1 p4 O
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); 3、端口模式配置。在I/O复用位内置外设功能引脚的时候,必须设置GPIO端口的模式。至于在复用功能下,GPIO的模式怎么设置,可以查看手册《STM32中文参考手册》p110的内容。这里拿USART1为例,进行配置,要配置全双工的串口1,TX引脚需要推挽复用输出,RX引脚需要浮空输入或者上拉输入; 3 W3 ~& v6 r2 U$ n) h
- <div align="left">GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9//复用推挽输出</div><div align="left">GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;</div><div align="left">GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出</div><div align="left">GPIO_Init(GPIOA, &GPIO_InitStructure);</div>7 z5 R# G' K# a; y6 ^
- <div align="left">GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10 PA.10 浮空输入</div><div align="left">GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入</div><div align="left">GPIO_Init(GPIOA, &GPIO_InitStructure);</div>
复制代码 " [/ q% [6 p2 U' i3 O5 V/ r5 s
总而言之,使用复用功能的时候至少要使能2时钟:GPIO时钟使能、复用的外设时钟使能。同时还要初始化GPIO以及复用外设功能(端口模式配置)。
0 `* `9 b J7 [# ?( R0 R9 ~4 M1 ^ |