请给出stm32f103c8t6用标准库驱动串口3初始化,并实现串口3发送及串口3中断接收
时间: 2024-02-28 21:52:15 浏览: 87
基于stm32f103c8t6实现的串口命令行程序
5星 · 资源好评率100%
好的,下面是代码实现:
串口3初始化:
```c
#include "stm32f10x.h"
void UART3_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable GPIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* Enable UART clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
/* Configure USART Tx and Rx as alternate function push-pull */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* USART configuration */
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(USART3, &USART_InitStructure);
/* Enable USART3 RX interrupt */
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
/* Enable USART3 */
USART_Cmd(USART3, ENABLE);
/* NVIC configuration */
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
```
串口3发送:
```c
void UART3_SendByte(uint8_t data)
{
USART_SendData(USART3, data);
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
}
```
串口3中断接收:
```c
#include <stdio.h>
void USART3_IRQHandler(void)
{
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
{
uint8_t data = USART_ReceiveData(USART3);
printf("Received data: %c\r\n", data);
USART_ClearITPendingBit(USART3, USART_IT_RXNE);
}
}
```
需要注意的是,如果要使用printf函数,需要先初始化UART2,将其作为系统的标准输出端口。
阅读全文