资源描述
21.1 USART 寄存器结构 ..............................................................343
21.2 USART 库函数 ..................................................................344
No
函数名
描述
1
USART_DeInit
将外设USARTx寄存器重设为缺省值
2
USART_Init
根据USART_InitStruct中指定的参数初始化外设USARTx寄存器
3
USART_StructInit
把USART_InitStruct中的每一个参数按缺省值填入
4
USART_Cmd
使能或失能USART外设
5
USART_ITConfig
使能或失能指定的USART中断
6
USART_DMACmd
使能或者失能指定USART的DMA请求
7
USART_SetAddress
设置USART节点的地址
8
USART_WakeUpConfig
选择USART的唤醒方式
9
USART_ReceiverWakeUpCmd
检查USART是否处于静默模式
10
USART_LINBreakDetectLengthConfig
设置USART LIN中断检测长度
11
USART_LINCmd
使能或失能USARTx的LIN模式
12
USART_SendData
通过外设USARTx发送单个数据
13
USART_ReceiveData
返回USARTx最近接收到的数据
14
USART_SendBreak
发送中断字
15
USART_SetGuardTime
设置指定的USART保护时间
16
USART_SetPrescaler
设置USART时钟预分频
17
USART_SmartCardCmd
使能或失能指定USART的智能卡模式
18
USART_SmartCardNackCmd
使能或失能NACK传输
19
USART_HalfDuplexCmd
使能或失能USART半双工模式
20
USART_IrDAConfig
设置USART IrDA模式
21
USART_IrDACmd
使能或失能USART IrDA模式
22
USART_GetFlagStatus
检查指定的USART标志位设置与否
23
USART_ClearFlag
清除USARTx的待处理标志位
24
USART_GetITStatus
检查指定的USART中断发生与否
25
USART_ClearITPendingBit
清除USARTx的中断待处理位
26
USART_ClockInit
USART时钟配置
27
USART_ClockStructInit
USART时钟结构体缺省值初始化
21 通用同步异步收发器( USART)
通用同步异步收发器(USART)提供了一种灵活的方法来与使用工业标准NRZ 异步串行数据格式的外部设备之间进行全双工数据交换。USART利用分数波特率发生器提供宽范围的波特率选择。它支持同步单向通信和半双工单线通信。它也支持LIN(局部互连网),智能卡协议和IrDA(红外数据组织)SIR ENDEC 规范,以及调制解调器(CTS/RTS)操作。它还允许多处理器通信。使用多缓冲器配置的DMA方式,可以实现高速数据通信。
Section 21.1 USART寄存器结构描述了固件函数库所使用的数据结构,Section 21.2 固件库函数介绍了函数库里的所有函数。
21.1 USART寄存器结构
USART寄存器结构,USART_TypeDeff,在文件stm32f0x_map.h中定义如下:
typedef struct
{
vu16 SR;
u16 RESERVED1;
vu16 DR;
u16 RESERVED2;
vu16 BRR;
u16 RESERVED3;
vu16 CR1;
u16 RESERVED4;
vu16 CR2;
u16 RESERVED5;
vu16 CR3;
u16 RESERVED6;
vu16 GTPR;
u16 RESERVED7;
}USART_TypeDef;
Table 704.例举了USART 所有寄存器
Table 704. USART 寄存器
寄存器
描述
SR
USART状态寄存器
DR
USART数据寄存器
BRR
USART波特率寄存器
CR1
USART控制寄存器1
CR2
USART控制寄存器2
CR3
USART控制寄存器3
GTPR
USART保护时间和预分频寄存器
3 个USART外设声明于文件:
...
#define PERIPH_BASE ((u32)0x40000000)
#define APB1PERIPH_BASE PERIPH_BASE
#define APB2PERIPH_BASE (PERIPH_BASE + 0x10000)
#define AHBPERIPH_BASE (PERIPH_BASE + 0x20000)
#define USART1_BASE (APB2PERIPH_BASE + 0x3800)
#define USART2_BASE (APB1PERIPH_BASE + 0x4400)
#define USART3_BASE (APB1PERIPH_BASE + 0x4800)
#ifndef DEBUG
...
#ifdef _USART1
#define USART1 ((USART_TypeDef *) USART1_BASE)
#endif /*_USART1 */
#ifdef _USART2
#define USART2 ((USART_TypeDef *) USART2_BASE)
#endif /*_USART2 */
#ifdef _USART3
#define USART3 ((USART_TypeDef *) USART3_BASE)
#endif /*_USART3 */
...
#else /* DEBUG */
...
#ifdef _USART1
EXT USART_TypeDef *USART1;
#endif /*_USART1 */
#ifdef _USART2
EXT USART_TypeDef *USART2;
#endif /*_USART2 */
#ifdef _USART3
EXT USART_TypeDef *USART3;
#endif /*_USART3 */
...
#endif
使用Debug模式时,初始化指针USART1, USART2 和USART3于文件stm2f10x_lib.c :
...
#ifdef _USART1
USART1 = (USART_TypeDef *) USART1_BASE;
#endif /*_USART1 */
#ifdef _USART2
USART2 = (USART_TypeDef *) USART2_BASE;
#endif /*_USART2 */
#ifdef _USART3
USART3 = (USART_TypeDef *) USART3_BASE;
#endif /*_USART3 */
...
为了访问USART 寄存器,_USART,_USART1, _USART2和_USART3必须在文件中定义如下:
...
#define _USART
#define _USART1 #define _USART2 #define _USART3
21.2 USART库函数
USART库函数【见首页】。
【01】函数USART_DeInit
Table 706. 函数 USART_DeInit
函数名
USART_DeInit
函数原形
void USART_DeInit(USART_TypeDef* USARTx)
功能描述
将外设USARTx寄存器重设为缺省值
输入参数
USARTx:x可以是1/2/3,来选择USART 外设
输出参数
无
返回值
无
先决条件
无
被调用函数
RCC_APB2PeriphResetCmd()
RCC_APB1PeriphResetCmd()
例:
/* Resets the USART1 registers to their default reset value */
USART_DeInit(USART1);
函数原型如下:
void USART_DeInit(USART_TypeDef* USARTx)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));//三个USART,两个UART
switch (*(u32*)&USARTx)
{
case USART1_BASE:
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
break;
case USART2_BASE:
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
break;
case USART3_BASE:
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
break;
case UART4_BASE:
RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE);
break;
case UART5_BASE:
RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE);
break;
default:
break;
}
}
【02】函数USART_Init
Table 707. 函数 USART_Init
函数名
USART_Init
函数原形
void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
功能描述
根据USART_InitStruct中指定的参数初始化外设USARTx寄存器
输入参数 1
USARTx:x=1/2/3,来选择USART外设
输入参数 2
USART_InitStruct:指向结构USART_InitTypeDef的指针,包含了外设USART的配置信息。
输出参数
无
返回值
无
先决条件
无
被调用函数
无
USART_InitTypeDef structure
USART_InitTypeDef 定义于文件stm2f10x_usart.h :
typedef struct
{
u32 USART_BaudRate;
u16 USART_WordLength;
u16 USART_StopBits;
u16 USART_Parity;
u16 USART_HardwareFlowControl;
u16 USART_Mode;
u16 USART_Clock;
u16 USART_CPOL;
u16 USART_CPHA;
u16 USART_LastBit;
}USART_InitTypeDef;
Table 708. 描述了结构USART_InitTypeDef 在同步和异步模式下使用的不同成员。
Table 708. USART_InitTypeDef成员USART 模式对比
成员
异步模式
同步模式
USART_BaudRate
X
X
USART_WordLength
X
X
USART_StopBits
X
X
USART_Parity
X
X
USART_HardwareFlowControl
X
X
USART_Mode
X
X
USART_Clock
X
USART_CPOL
X
USART_CPHA
X
USART_LastBit
X
USART_BaudRate
USART_BaudRate:该成员设置了 USART传输的波特率,波特率可以由以下公式计算:
IntegerDivider = ((APBClock) / (16 * (USART_InitStruct->USART_BaudRate)))
FractionalDivider = ((IntegerDivider - ((u32) IntegerDivider)) * 16) + 0.5
其取值范围为:0 < BaudRate < 0x0044AA21
USART_WordLength :提示了在一个帧中传输或者接收到的数据位数。
Table 709. USART_WordLength 定义
USART_WordLength
描述
USART_WordLength_8b
8位数据
0x0000
USART_WordLength_9b
9位数据
0x1000
USART_StopBits: 定义了发送的停止位数目。Table 710. 给出了该参数可取的值。
Table 710. USART_StopBits 定义
USART_StopBits
描述
USART_StopBits_1
在帧结尾传输1个停止位
0x0000
USART_StopBits_0.5
在帧结尾传输0.5个停止位
0x1000
USART_StopBits_2
在帧结尾传输2个停止位
0x2000
USART_StopBits_1.5
在帧结尾传输1.5个停止位
0x3000
USART_Parity: 定义了奇偶模式。
Table 711. USART_Parity 定义
USART_Parity
描述
USART_Parity_No
奇偶失能
0x0000
USART_Parity_Even
偶模式
0x0400
USART_Parity_Odd
奇模式
0x0600
注意:奇偶校验一旦使能,在发送数据的MSB位插入经计算的奇偶位(字长9位时的第9位,字长8位时的第8位)。
USART_HardwareFlowControl:指定了硬件流控制模式使能还是失能。
Table 712. USART_HardwareFlowControl 定义
USART_HardwareFlowControl
描述
USART_HardwareFlowControl_None
硬件流控制失能
0x0000
USART_HardwareFlowControl_RTS
发送请求RTS使能
0x0100
USART_HardwareFlowControl_CTS
清除发送CTS使能
0x0200
USART_HardwareFlowControl_RTS_CTS
RTS和CTS使能
0x0300
USART_Mode :指定了使能或者失能发送和接收模式。
Table 713. USART_Mode 定义
USART_Mode
描述
USART_Mode_Rx
接收使能
0x0004
USART_Mode_Tx
发送使能
0x0008
USART_CLOCK :提示了 USART 时钟使能还是失能。
Table 714. USART_CLOCK定义
USART_CLOCK
描述
USART_Clock_Enable
时钟高电平活动
0x0800
USART_Clock_Disable
时钟低电平活动
0x0000
USART_CPOL: 指定了下 SLCK 引脚上时钟输出的极性。
Table 715. USART_CPOL 定义
USART_CPOL
描述
USART_CPOL_High
时钟高电平
0x0400
USART_CPOL_Low
时钟低电平
0x0000
USART_CPHA: 指定了下 SLCK 引脚上时钟输出的相位,和 CPOL 位一起配合来产生用户希望的时钟/数据的采样关系。
Table 716. USART_CPHA 定义
USART_CPHA
描述
USART_CPHA_1Edge
时钟第一个边沿进行数据捕获
0x0000
USART_CPHA_2Edge
时钟第二个边沿进行数据捕获
0x0200
USART_LastBit :来控制是否在同步模式下,在 SCLK 引脚上输出最后发送的那个数据字 (MSB) 对应的时钟脉冲。
Table 717. USART_LastBit 定义
USART_LastBit
描述
USART_LastBit_Disable
最后一位数据的时钟脉冲不从SCLK输出
0x0000
USART_LastBit_Enable
最后一位数据的时钟脉冲从SCLK输出
0x0100
例:
/* The following example illustrates how to configure the USART1 */
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_Odd;
USART_InitStructure.USART_HardwareFlowControl =USART_HardwareFlowControl_RTS_CTS;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_InitStructure.USART_Clock = USART_Clock_Disable;
USART_InitStructure.USART_CPOL = USART_CPOL_High;
USART_InitStructure.USART_CPHA = USART_CPHA_1Edge;
USART_InitStructure.USART_LastBit = USART_LastBit_Enable;
USART_Init(USART1, &USART_InitStructure);
函数原型如下:
void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
{
u32 tmpreg = 0x00, apbclock = 0x00;
u32 integerdivider = 0x00;
u32 fractionaldivider = 0x00;
u32 usartxbase = 0;
RCC_ClocksTypeDef RCC_ClocksStatus;
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate));
assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength));
assert_param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits));
assert_param(IS_USART_PARITY(USART_InitStruct->USART_Parity));
assert_param(IS_USART_MODE(USART_InitStruct->USART_Mode));
assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl));
/* The hardware flow control is available only for USART1, USART2 and USART3 */
assert_param(IS_USART_PERIPH_HFC(USARTx, USART_InitStruct->USART_HardwareFlowControl));
usartxbase = (*(u32*)&USARTx);
/*---------------------------- USART CR2 Configuration -----------------------*/
tmpreg = USARTx->CR2;
/* Clear STOP[13:12] bits */
tmpreg &= CR2_STOP_CLEAR_Mask;
/* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit ------------*/
/* Set STOP[13:12] bits according to USART_StopBits value */
tmpreg |= (u32)USART_InitStruct->USART_StopBits;
/* Write to USART CR2 */
USARTx->CR2 = (u16)tmpreg;
/*---------------------------- USART CR1 Configuration -----------------------*/
tmpreg = USARTx->CR1;
/* Clear M, PCE, PS, TE and RE bits */
tmpreg &= CR1_CLEAR_Mask;
/* Configure the USART Word Length, Parity and mode ----------------------- */
/* Set the M bits according to USART_WordLength value */
/* Set PCE and PS bits according to USART_Parity value */
/* Set TE and RE bits according to USART_Mode value */
tmpreg |= (u32)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |
USART_InitStruct->USART_Mode;
/* Write to USART CR1 */
USARTx->CR1 = (u16)tmpreg;
/*---------------------------- USART CR3 Configuration -----------------------*/
tmpreg = USARTx->CR3;
/* Clear CTSE and RTSE bits */
tmpreg &= CR3_CLEAR_Mask;
/* Configure the USART HFC -------------------------------------------------*/
/* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */
tmpreg |= USART_InitStruct->USART_HardwareFlowControl;
/* Write to USART CR3 */
USARTx->CR3 = (u16)tmpreg;
/*---------------------------- USART BRR Configuration -----------------------*/
/* Configure the USART Baud Rate -------------------------------------------*/
RCC_GetClocksFreq(&RCC_ClocksStatus);
if (usartxbase == USART1_BASE)
{
apbclock = RCC_ClocksStatus.PCLK2_Frequency;
}
else
{
apbclock = RCC_ClocksStatus.PCLK1_Frequency;
}
/* Determine the integer part */
integerdivider = ((0x19 * apbclock) / (0x04 * (USART_InitStruct->USART_BaudRate)));
tmpreg = (integerdivider / 0x64) << 0x04;
/* Determine the fractional part */
fractionaldivider = integerdivider - (0x64 * (tmpreg >> 0x04));
tmpreg |= ((((fractionaldivider * 0x10) + 0x32) / 0x64)) & ((u8)0x0F);
/* Write to USART BRR */
USARTx->BRR = (u16)tmpreg;
}
【03】函数USART_StructInit
Table 718. 函数 USART_StructInit
函数名
USART_StructInit
函数原形
void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
功能描述
把USART_InitStruct中的每一个参数按缺省值填入
输入参数
USART_InitStruct:指向结构USART_InitTypeDef的指针,待初始化
输出参数
无
返回值
无
先决条件
无
被调用函数
无
Table 719. 给出了USART_InitStruct 各个成员的缺省值
Table 719. USART_InitStruct 缺省值(#define Val 参看函数【2】)
成员
缺省值
#define Val
USART_BaudRate
9600
USART_WordLength
USART_WordLength_8b
USART_StopBits
USART_StopBits_1
USART_Parity
USART_Parity_No
USART_HardwareFlowControl
USART_HardwareFlowControl_None
USART_Mode
USART_Mode_Rx | USART_Mode_Tx
USART_Clock
USART_Clock_Disable
USART_CPOL
USART_CPOL_Low
USART_CPHA
USART_CPHA_1Edge
USART_LastBit
USART_LastBit_Disable
例:
/* The following example illustrates how to initialize a
USART_InitTypeDef structure */
USART_InitTypeDef USART_InitStructure;
USART_StructInit(&USART_InitStructure);
函数原型如下:
void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
{
/* USART_InitStruct members default value */
USART_InitStruct->USART_BaudRate = 9600;
USART_InitStruct->USART_WordLength = USART_WordLength_8b;
USART_InitStruct->USART_StopBits = USART_StopBits_1;
USART_InitStruct->USART_Parity = USART_Parity_No ;
USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;
}
【04】函数USART_Cmd
Table 720. 函数 USART_Cmd
函数名
USART_ Cmd
函数原形
void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
功能描述
使能或失能USART外设
输入参数 1
USARTx:x=1/2/3,来选择USART外设
输入参数 2
NewState: 外设USARTx的新状态(ENABLE或DISABLE)
输出参数
无
返回值
无
先决条件
无
被调用函数
无
例:
/* Enable the USART1 */
USART_Cmd(USART1, ENABLE);
函数原型如下:
void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the selected USART by setting the UE bit in the CR1 register */
USARTx->CR1 |= CR1_UE_Set;
}
else
{
/* Disable the selected USART by clearing the UE bit in the CR1 register */
USARTx->CR1 &= CR1_UE_Reset;
}
}
【05】函数USART_ITConfig
Table 721. 函数 USART_ITConfig
函数名
USART_ITConfig
函数原形
void USART_ITConfig(USART_TypeDef* USARTx, u16 USART_IT, FunctionalState NewState)
功能描述
使能或失能指定的USART中断
输入参数 1
USARTx:x=1/2/3,来选择USART外设
输入参数 2
USART_IT:待使能或者失能的USART中断源
输入参数 3
NewState:USARTx 中断的新状态(ENABLE 或 DISABLE)
输出参数
无
返回值
无
先决条件
无
被调用函数
无
USART_IT:输入参数 USART_IT 使能或者失能 USART 的中断。可以取下表的一个或者多个取值的组合作为该参数的值。
Table 722. USART_IT 值
Bit
USART_IT
描述
#define
#Val>>5
#Val的意义
9
USART_IT_CTS
CTS中断
0x096A
受控于CR3.bitA
黑体部分表示IT标志在SR中的位置;
蓝色部分(>>5的结果)表示IT是由哪个CRx控制的;
绿色部分表示IT在CRx中的第N位。
8
USART_IT_LBD
LIN中断检测中断
0x0846
受控于CR2.bit6
7
USART_
展开阅读全文