USART简介
- USART:通用同步/异步收发器(两种模式可切换)
STM32F103系列提供5路串口,包含3个 USART和2个UART 。
RXD为数据接收引脚,TXD为数据发送引脚。
串口号 | RXD | TXD |
USART1 | PA10 | PA9 |
USART2 | PA3 | PA2 |
USART3 | PB11 | PB10 |
在PC机上用串口调试助手发送接收数据时,默认是USART1串口,也可在下面的重定义函数中修改成USART2或者USART3。
//重定义fputc函数
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);//循环发送,直到发送完毕
USART1->DR = (u8) ch;
return ch;
}
两个单片机进行通信时,收发引脚要交叉连接,如下如
USART配置
u8 uart1buffer[MAX_USART1_REC_LEN]; //接收缓冲,最大USART_REC_LEN个字节.
u16 receive_num1=0; //接收状态标记
void uart_init(u32 bound){
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能
USART1,GPIOA时钟
//USART1_TX GPIOA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9
//USART1_RX GPIOA.10初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
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_Tx|USART_Mode_Rx; //收发模式
USART_Init(USART1, &USART_InitStructure); //初始化串口1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断
USART_Cmd(USART1, ENABLE); //使能串口1
}
void USART1_IRQHandler(void) //串口1中断服务程序
{
u8 Res;
#if SYSTEM_SUPPORT_OS //如果SYSTEM_SUPPORT_OS为真,则需要支持OS.
OSIntEnter();
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是 0x0d 0x0a结尾)
{
Res =USART_ReceiveData(USART1); //读取接收到的数据
uart1buffer[receive_num1]=Res;
if(receive_num1<MAX_USART1_REC_LEN-1)
receive_num1++;
//uart2sendchar(Res);//收到的数据直接转发到2口
}
#if SYSTEM_SUPPORT_OS //如果SYSTEM_SUPPORT_OS为真,则需要支持OS.
OSIntExit();
#endif
}
注:有人说接收引脚要设置成浮空输入,我暂且搁置,设置成上拉输入也行,之后再实验实验。
USART实例程序(收发数据)
例如用USART2进行两个单片机之间的数据收发(比如发送整型数字)。
发送方:
数据要求,-400 -- 400数据循环发送,有帧头帧尾校验,发送长度固定为4位。
short dat=0;
while(1){
if(dat<400)
dat++;
else
dat=-400;
delay_ms(3);
bufSend[0]=0xaa;
bufSend[1]=(dat>>8)&0xff;
bufSend[2]=(dat>>0)&0xff;
bufSend[3]=0xa5;
uart2sendbuffer(bufSend,4);
}
接收方:
void USART2_IRQHandler(void) //串口2中断服务程序
{
u8 res,i=0;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收到数据
{
res =USART_ReceiveData(USART2); //读取接收到的数据
for(i=0;i<MAX_USART2_REC_LEN-1;i++)
uart2buffer[i]=uart2buffer[i+1];
uart2buffer[i]=res;
if((uart2buffer[0]==0xaa)&&(uart2buffer[3]==0xa5))
{//检验帧头帧尾
data=(short)(uart2buffer[1]*256L+uart2buffer[2]);
}
}
}