STM32F103ZET6 5串口通信问题
今天调试成功STM32F103ZET6 5串口通信,其中主要是注意点是:
1、 USATR1和USATR2,USATR3,UATR4,UATR5挂载的时钟不一样,第一个挂载在
APB2上,其余四个挂载在APB1上;例如:
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //USART1时钟配置 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); //USART2时钟配置 ; RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); //USART3时钟配置 ; RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE); //UART4时钟配置 ; RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE); //UART5时钟配置 ; 2、 如上例中红色字显示一样,UART4和UART5是不一样的,
UART:universal asynchronous receiver and transmitter通用异步收发器
USART:universal synchronous asynchronous receiver and transmitter通用同步异步收发器。一般而言,单片机中,名称为UART的接口一般只能用于异步串行通讯,而名称为USART的接口既可以用于同步串行通讯,也能用于异步串行通讯。 下面贴出我的代码: void RCC_Configuration(void); void GPIO_Configuration(void); void NVIC_Configuration(void); void USART_Configuration(void);
int main(void) {
RCC_Configuration(); //系统时钟初始化 GPIO_Configuration();//端口初始化 USART_Configuration(); NVIC_Configuration(); while(1); }
void RCC_Configuration(void) {
SystemInit();//72m
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); //USART2时钟配置 ; RCC_APB1PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //USART2,3端口复用; RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //USART1端口复用; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); //USART3时钟配置 ; RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE); //UART4时钟配置 ;
}
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE); //UART5时钟配置 ;
void GPIO_Configuration(void) {
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5; //LED1
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;//USATR5_RX GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOD,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12;//USATR5_TX GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;//UATR4_RX GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//UATR4_TX GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;//USATR3_RX GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//USATR3_TX GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;//USATR2_RX GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA,&GPIO_InitStructure); //LED
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;//USATR2_TX GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; }
void NVIC_Configuration(void) {
NVIC_InitTypeDef NVIC_InitStructure;
//USART5的中断优先级配置
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); //USART4的中断优先级配置
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); //USART3的中断优先级配置 //USART2的中断优先级配置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5; //LED0 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; GPIO_Init(GPIOE,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; GPIO_Init(GPIOB,&GPIO_InitStructure);
void USART_Configuration(void) {
USART_InitTypeDef USART_InitStructure;
//USART5的串口配置
USART_InitStructure.USART_BaudRate=9600; USART_Init(UART4,&USART_InitStructure);
USART_ITConfig(UART4,USART_IT_RXNE,ENABLE); USART_Cmd(UART4,ENABLE);
USART_ClearFlag(UART4,USART_FLAG_TC); //USART4的串口配置
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_No;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; USART_Init(USART3,&USART_InitStructure);
USART_ITConfig(USART3,USART_IT_RXNE,ENABLE); USART_Cmd(USART3,ENABLE);
USART_ClearFlag(USART3,USART_FLAG_TC); USART_Init(USART2,&USART_InitStructure);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); USART_Cmd(USART2,ENABLE);
USART_ClearFlag(USART2,USART_FLAG_TC); //USART3的串口配置
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_No;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; //USART2的串口配置
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_No;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
}
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(UART5,&USART_InitStructure);
USART_ITConfig(UART5,USART_IT_RXNE,ENABLE); USART_Cmd(UART5,ENABLE);
USART_ClearFlag(UART5,USART_FLAG_TC);
一下为中断代码:
void USART2_IRQHandler(void) {
u32 k;
if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET) {
k=USART_ReceiveData(USART2); }
void USART3_IRQHandler(void) {
u32 k;
if(USART_GetITStatus(USART3,USART_IT_RXNE)!=RESET) {
k=USART_ReceiveData(USART3);
if(k =='a')
{
GPIO_ResetBits(GPIOE,GPIO_Pin_5); USART_SendData(USART2,'1');
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);
} else {
GPIO_SetBits(GPIOE,GPIO_Pin_5); USART_SendData(USART2,'3');
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);
}
}