1. 时钟使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); /* USART1 GPIOA*/
2. 引脚配置
GPIO_InitTypeDef GPIO_InitStructure;
/* USART1 GPIO config */
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(macUSART_TX_PORT, &GPIO_InitStructure);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(macUSART_RX_PORT, &GPIO_InitStructure);
3. 串口模式配置
USART_InitTypeDef USART_InitStructure;
/* USART1 mode config */
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure); //配置串口1
- 以下是用到的结构体和宏定义。
typedef struct
{
uint32_t USART_BaudRate; /*!< This member configures the USART communication baud rate.
The baud rate is computed using the following formula:
- IntegerDivider = ((PCLKx) / (16 * (USART_InitStruct->USART_BaudRate)))
- FractionalDivider = ((IntegerDivider - ((u32) IntegerDivider)) * 16) + 0.5 */
uint16_t USART_WordLength; /*!< Specifies the number of data bits transmitted or received in a frame.
This parameter can be a value of @ref USART_Word_Length */
uint16_t USART_StopBits; /*!< Specifies the number of stop bits transmitted.
This parameter can be a value of @ref USART_Stop_Bits */
uint16_t USART_Parity; /*!< Specifies the parity mode.
This parameter can be a value of @ref USART_Parity
@note When parity is enabled, the computed parity is inserted
at the MSB position of the transmitted data (9th bit when
the word length is set to 9 data bits; 8th bit when the
word length is set to 8 data bits). */
uint16_t USART_Mode; /*!< Specifies wether the Receive or Transmit mode is enabled or disabled.
This parameter can be a value of @ref USART_Mode */
uint16_t USART_HardwareFlowControl; /*!< Specifies wether the hardware flow control mode is enabled
or disabled.
This parameter can be a value of @ref USART_Hardware_Flow_Control */
} USART_InitTypeDef;
#define USART_WordLength_8b ((uint16_t)0x0000)
#define USART_WordLength_9b ((uint16_t)0x1000)
#define USART_StopBits_1 ((uint16_t)0x0000)
#define USART_StopBits_0_5 ((uint16_t)0x1000)
#define USART_StopBits_2 ((uint16_t)0x2000)
#define USART_StopBits_1_5 ((uint16_t)0x3000)
/* 无校验 */
#define USART_Parity_No ((uint16_t)0x0000)
/* 偶校验 */
#define USART_Parity_Even ((uint16_t)0x0400)
/* 奇校验 */
#define USART_Parity_Odd ((uint16_t)0x0600)
#define USART_HardwareFlowControl_None ((uint16_t)0x0000)
#define USART_HardwareFlowControl_RTS ((uint16_t)0x0100)
#define USART_HardwareFlowControl_CTS ((uint16_t)0x0200)
#define USART_HardwareFlowControl_RTS_CTS ((uint16_t)0x0300)
/*
RTS(Require To Send) 发送请求,输出信号,低电平说明本设备准备好接收数据。
CTS(Clear To Send) 发送允许,输入信号,低电平说明本设备可以向对方发送数据。
*/
#define USART_Mode_Rx ((uint16_t)0x0004)
#define USART_Mode_Tx ((uint16_t)0x0008)
4. 串口中断配置(采用轮询方法可直接跳过这一步)
配置中断源
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Enable the USARTy Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
使能串口中断源
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //串口1接收中断
/*
GetFlagStatus() 检查USART标志位设置与否
GetITStatus() 检查USART中断产生与否
*/
中断响应函数
//stm32f10x_it.c
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET){
/*-----do what-----*/
}
}
- 以下是用到的宏定义。
/* 奇偶校验错误中断 */
#define USART_IT_PE ((uint16_t)0x0028)
/* 发送数据寄存器空中断 */
#define USART_IT_TXE ((uint16_t)0x0727)
/* 发送完成中断 */
#define USART_IT_TC ((uint16_t)0x0626)
/* 接收寄存器非空中断 */
#define USART_IT_RXNE ((uint16_t)0x0525)
/* 空闲线检测中断 */
#define USART_IT_IDLE ((uint16_t)0x0424)
/* 终止检测中断 */
#define USART_IT_LBD ((uint16_t)0x0846)
/* CTS变化中断 */
#define USART_IT_CTS ((uint16_t)0x096A)
/* 错误中断 */
#define USART_IT_ERR ((uint16_t)0x0060)
/* 溢出错误中断 */
#define USART_IT_ORE ((uint16_t)0x0360)
/* 噪声错误中断 */
#define USART_IT_NE ((uint16_t)0x0260)
/* 帧错误中断 */
#define USART_IT_FE ((uint16_t)0x0160)
5. 串口使能
USART_Cmd(USART1, ENABLE); //开启串口1
6. 发送与接收函数
发送字节
/* 重定向c库函数printf到USART1 */
int fputc(int ch, FILE *f)
{
/* 发送一个字节数据到USART1 */
USART_SendData(USART1, (uint8_t)ch);
/* 等待发送完毕 */
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
return (ch);
}
接收字节
/* 重定向c库函数scanf到USART1 */
int fgetc(FILE *f)
{
/* 等待串口1输入数据 */
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
return (int)USART_ReceiveData(USART1);
}
- 以下是用到的宏定义。
/* CTS Change flag (not available for UART4and UART5) */
#define USART_FLAG_CTS ((uint16_t)0x0200)
/* LIN Break detection flag */
#define USART_FLAG_LBD ((uint16_t)0x0100)
/* Transmit data register empty flag */
#define USART_FLAG_TXE ((uint16_t)0x0080)
/* Transmission complete flag */
#define USART_FLAG_TC ((uint16_t)0x0040)
/* Read data register not empty flag */
#define USART_FLAG_RXNE ((uint16_t)0x0020)
/* IDLE line detected flag */
#define USART_FLAG_IDLE ((uint16_t)0x0010)
/* Overrun error flag */
#define USART_FLAG_ORE ((uint16_t)0x0008)
/* Noise error flag */
#define USART_FLAG_NE ((uint16_t)0x0004)
/* Framing error */
#define USART_FLAG_FE ((uint16_t)0x0002)
/* Parity error */
#define USART_FLAG_PE ((uint16_t)0x0001)