USART初始化示例
void UART1_Init(uint32_t BaudRate)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = BaudRate;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1, &USART_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1,USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1,USART_IT_TXE, DISABLE);
USART_Cmd(USART1,ENABLE);
USART_ClearFlag(USART1, USART_FLAG_TC);
}
串口应用
中断接收
#define READMAX 128
extern uint8_t readBuf[READMAX];
extern uint16_t readReg;
uint8_t readBuf[READMAX];
uint16_t readReg = 0;
void USART1_IRQHandler(void)
{
uint8_t data;
if (USART_GetITStatus(USART1,USART_IT_RXNE) == SET) {
data = USART_ReceiveData(USART1);
if ((readReg & 0X8000) == 0) {
if((readReg & 0X4000)){
if (data == '\n'){
readReg |= 0X8000;
}else {
readReg = 0;
}
}else {
if (data == '\r') {
readReg |= 0X4000;
}else {
readBuf[(readReg & 0X3fff)] = data;
readReg++;
if ((readReg & 0X3fff) > READMAX-1) {
readReg = 0;
}
}
}
}
}
}
发送字符串
void UART1_SendString(uint8_t *string)
{
while (*string) {
USART_ClearFlag(USART1, USART_FLAG_TC);
USART_SendData(USART1, *string);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {
}
string++;
}
}
USART相关函数
USART_Init
void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct);
USARTx:x 可以是 1,2 或者 3,来选择 USART 外设
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_Mode_Rx ((uint16_t)0x0004)
#define USART_Mode_Tx ((uint16_t)0x0008)
#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)
USART_ITConfig
void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState);
使能或者失能指定的 USART 中断
#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) //LIN 中断检测中断
#define USART_IT_CTS ((uint16_t)0x096A) //CTS 中断
#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) //
typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;
USART_GetITStatus
ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT);
检查指定的 USART 中断发生与否
USART_Cmd
void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState);
使能或者失能USART外设
USART_ClearFlag
void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG);
清除 USARTx 的待处理标志位
#define USART_FLAG_CTS ((uint16_t)0x0200)
#define USART_FLAG_LBD ((uint16_t)0x0100)
#define USART_FLAG_TXE ((uint16_t)0x0080)
#define USART_FLAG_TC ((uint16_t)0x0040)
#define USART_FLAG_RXNE ((uint16_t)0x0020)
#define USART_FLAG_IDLE ((uint16_t)0x0010)
#define USART_FLAG_ORE ((uint16_t)0x0008)
#define USART_FLAG_NE ((uint16_t)0x0004)
#define USART_FLAG_FE ((uint16_t)0x0002)
#define USART_FLAG_PE ((uint16_t)0x0001)
USART_GetFlagStatus
FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG);
检查指定的 USART 标志位设置与否
USART_SendData
void USART_SendData(USART_TypeDef* USARTx, uint16_t Data);
USART_ReceiveData
uint16_t USART_ReceiveData(USART_TypeDef* USARTx);
串口中断补充
NVIC_IRQChannel,中断源,板子不同USART中断个数不一样
USART1_IRQn = 37, /*!< USART1 global Interrupt */
USART2_IRQn = 38, /*!< USART2 global Interrupt */
中断函数名
EXPORT USART1_IRQHandler [WEAK]
EXPORT USART2_IRQHandler [WEAK]
EXPORT USART3_IRQHandler [WEAK]