STM32两个串口同时工作,一个串口发,另一个串口收。
STM32双串口同时工作两个串口的电路结构代码usart.cusart.hmian.c
实现效果
STM32双串口同时工作
由于最近一个国创项目的需要,所以使用STM32两个串口来同时工作。STM32F1一共有5个串口,因为实际电路的原因我这里使用的是串口一与串口三,其中串口三用来接收,串口一用来发送。
两个串口的电路结构
废话少说,直接上原理图。 由上面两部分原理图可知:串口一的引脚TX为PA9,RX为PA10;串口三的引脚TX为PB10,RX为PB11。 按照以上描述,将串口一与串口三都连接上电脑。
代码
usart.c
#include "sys.h"
#include "usart.h"
#if 1
#pragma import(__use_no_semihosting)
struct __FILE
{
int handle
;
};
FILE __stdout
;
_sys_exit(int x
)
{
x
= x
;
}
int fputc(int ch
, FILE
*f
)
{
while((USART1
->SR
&0X40)==0);
USART1
->DR
= (u8
) ch
;
return ch
;
}
#endif
#if EN_USART3_RX
u8 USART_RX_BUF
[USART_REC_LEN
];
u16 USART_RX_STA
=0;
void uart1_init(u32 bound
){
GPIO_InitTypeDef GPIO_InitStructure
;
USART_InitTypeDef USART_InitStructure
;
NVIC_InitTypeDef NVIC_InitStructure
;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1
|RCC_APB2Periph_GPIOA
, ENABLE
);
GPIO_InitStructure
.GPIO_Pin
= GPIO_Pin_9
;
GPIO_InitStructure
.GPIO_Speed
= GPIO_Speed_50MHz
;
GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_AF_PP
;
GPIO_Init(GPIOA
, &GPIO_InitStructure
);
GPIO_InitStructure
.GPIO_Pin
= GPIO_Pin_10
;
GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_IN_FLOATING
;
GPIO_Init(GPIOA
, &GPIO_InitStructure
);
NVIC_InitStructure
.NVIC_IRQChannel
= USART1_IRQn
;
NVIC_InitStructure
.NVIC_IRQChannelPreemptionPriority
=3 ;
NVIC_InitStructure
.NVIC_IRQChannelSubPriority
= 3;
NVIC_InitStructure
.NVIC_IRQChannelCmd
= ENABLE
;
NVIC_Init(&NVIC_InitStructure
);
USART_InitStructure
.USART_BaudRate
= bound
;
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
);
USART_ITConfig(USART1
, USART_IT_RXNE
, ENABLE
);
USART_Cmd(USART1
, ENABLE
);
}
void uart3_init(u32 bound
){
GPIO_InitTypeDef GPIO_InitStructure
;
USART_InitTypeDef USART_InitStructure
;
NVIC_InitTypeDef NVIC_InitStructure
;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB
, ENABLE
);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3
,ENABLE
);
GPIO_InitStructure
.GPIO_Pin
= GPIO_Pin_10
;
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_11
;
GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_IN_FLOATING
;
GPIO_Init(GPIOB
, &GPIO_InitStructure
);
NVIC_InitStructure
.NVIC_IRQChannel
= USART3_IRQn
;
NVIC_InitStructure
.NVIC_IRQChannelPreemptionPriority
=2 ;
NVIC_InitStructure
.NVIC_IRQChannelSubPriority
= 3;
NVIC_InitStructure
.NVIC_IRQChannelCmd
= ENABLE
;
NVIC_Init(&NVIC_InitStructure
);
USART_InitStructure
.USART_BaudRate
= bound
;
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
);
}
void USART1_IRQHandler(void)
{
u8 Res
;
if(USART_GetITStatus(USART1
, USART_IT_RXNE
) != RESET
)
{
Res
=USART_ReceiveData(USART1
);
if((USART_RX_STA
&0x8000)==0)
{
if(USART_RX_STA
&0x4000)
{
if(Res
!=0x0a)USART_RX_STA
=0;
else USART_RX_STA
|=0x8000;
}
else
{
if(Res
==0x0d)USART_RX_STA
|=0x4000;
else
{
USART_RX_BUF
[USART_RX_STA
&0X3FFF]=Res
;
USART_RX_STA
++;
if(USART_RX_STA
>(USART_REC_LEN
-1))USART_RX_STA
=0;
}
}
}
}
}
void USART3_IRQHandler(void)
{
u8 Res
;
if(USART_GetITStatus(USART3
, USART_IT_RXNE
) != RESET
)
{
Res
=USART_ReceiveData(USART3
);
if((USART_RX_STA
&0x8000)==0)
{
if(USART_RX_STA
&0x4000)
{
if(Res
!=0x0a)USART_RX_STA
=0;
else USART_RX_STA
|=0x8000;
}
else
{
if(Res
==0x0d)USART_RX_STA
|=0x4000;
else
{
USART_RX_BUF
[USART_RX_STA
&0X3FFF]=Res
;
USART_RX_STA
++;
if(USART_RX_STA
>(USART_REC_LEN
-1))USART_RX_STA
=0;
}
}
}
}
}
#endif
usart.h
#ifndef __USART_H
#define __USART_H
#include "stdio.h"
#include "sys.h"
#define USART_REC_LEN 200
#define EN_USART3_RX 1
extern u8 USART_RX_BUF
[USART_REC_LEN
];
extern u16 USART_RX_STA
;
void uart1_init(u32 bound
);
void uart3_init(u32 bound
);
void USART1_IRQHandler(void);
void USART3_IRQHandler(void);
#endif
mian.c
#include "delay.h"
#include "sys.h"
#include "usart.h"
int main(void)
{
u8 t
,len
;
u16 times
=0;
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2
);
uart1_init(9600);
uart3_init(9600);
printf("\r\n111111\r\n\r\n");
while(1)
{
if(USART_RX_STA
&0x8000)
{
len
=(USART_RX_STA
&0x3f)+1;
for(t
=0;t
<len
;t
++)
{
USART_SendData(USART1
, USART_RX_BUF
[t
-1]);
while(USART_GetFlagStatus(USART1
,USART_FLAG_TC
)!=SET
);
}
if(t
==USART_REC_LEN
) t
=0;
printf("\r\n\r\n");
USART_RX_STA
=0;
times
++;
if(times
++ ==10)
{
times
=0;
LED0
=!LED0
;
}
delay_ms(10);
}
}
}
实现效果
com3连接串口三,负责将信息从电脑传送给STM32。 com4连接串口一,负责接收STM32从串口1发送过来的信息。 至此实验结束,如有错误,请大家指出。