STM32笔记---USART3配置及收发数据

前段时间某个项目需要多串口收发数据,因此尝试了一番,参考了正点原子官方例程,现在记录下
代码:
USART3.c中先添加头文件:

#include "sys.h"
#include "usart.h"	
#include <stdarg.h>
#include "stdio.h"	 	 
#include "string.h"

并定义:

//串口接收缓存区 	
u8 USART3_RX_BUF[USART3_MAX_RECV_LEN]; 				//接收缓冲,最大USART3_MAX_RECV_LEN个字节.
u8  USART3_TX_BUF[USART3_MAX_SEND_LEN]; 			//发送缓冲,最大USART3_MAX_SEND_LEN字节

USART3初始化:

void usart3_init(u32 bound){
	//GPIO端口配置
	USART_InitTypeDef USART_InitStructure;    
	NVIC_InitTypeDef NVIC_InitStructure;     
	GPIO_InitTypeDef GPIO_InitStructure;    
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE); 
 	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);    

   // USART3_Rx (PB.11)       
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;    
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;    
   GPIO_Init(GPIOB, &GPIO_InitStructure);    

   // USART3_Tx (PB.10)   
   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);
   
   //USART3 NVIC配置    
   NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//抢占优先级0
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   NVIC_Init(&NVIC_InitStructure);    
   
   //USART3 初始化设置 
   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;    


   //初始化 USART3     
   USART_Init(USART3, &USART_InitStructure);
  // 开启串口接收中断   
   USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);    
   // 使能USART3     
   USART_Cmd(USART3, ENABLE);         
}

USART3 printf函数:

//串口3,printf 函数
//确保一次发送数据不超过USART3_MAX_SEND_LEN字节
void u3_printf(char* fmt,...)  
{  
	u16 i,j; 
	va_list ap; 
	va_start(ap,fmt);
	vsprintf((char*)USART3_TX_BUF,fmt,ap);
	va_end(ap);
	i=strlen((const char*)USART3_TX_BUF);		//此次发送数据的长度
	for(j=0;j<i;j++)							//循环发送数据
	{
	  while(USART_GetFlagStatus(USART3,USART_FLAG_TC)==RESET); //循环发送,直到发送完毕   
		USART_SendData(USART3,USART3_TX_BUF[j]); 
	} 
}

USART3 中断函数:

void USART3_IRQHandler(void)
{
//your coding				 											 
}   

USART3 头文件:

#ifndef __USART_H
#define __USART_H	 
#include "sys.h"  
#define USART3_MAX_RECV_LEN		600					//最大接收缓存字节数
#define USART3_MAX_SEND_LEN		600					//最大发送缓存字节数
#define USART3_RX_EN 			1					//0,不接收;1,接收.

extern u8  USART3_RX_BUF[USART3_MAX_RECV_LEN]; 		//接收缓冲,最大USART3_MAX_RECV_LEN字节
extern u8  USART3_TX_BUF[USART3_MAX_SEND_LEN]; 		//发送缓冲,最大USART3_MAX_SEND_LEN字节
extern vu16 USART3_RX_STA;   						//接收数据状态

void usart3_init(u32 bound);				//串口3初始化 
void u3_printf(char* fmt,...);
#endif
以下是配置USART3的参数,使用MDB协议的示例代码: ```c #include "stm32f4xx.h" void init_USART3(void) { GPIO_InitTypeDef GPIO_InitStruct; USART_InitTypeDef USART_InitStruct; NVIC_InitTypeDef NVIC_InitStruct; // Enable the clock for the USART3 peripheral and GPIOB RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); // Configure the GPIO pins for USART3 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOB, &GPIO_InitStruct); // Connect the USART3 pins to the alternate function GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3); // PB10 -> Tx GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3); // PB11 -> Rx // Configure the USART3 parameters USART_InitStruct.USART_BaudRate = 115200; USART_InitStruct.USART_WordLength = USART_WordLength_8b; USART_InitStruct.USART_StopBits = USART_StopBits_1; USART_InitStruct.USART_Parity = USART_Parity_No; USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART3, &USART_InitStruct); // Enable the USART3 receive interrupt USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); // Configure the NVIC for USART3 NVIC_InitStruct.NVIC_IRQChannel = USART3_IRQn; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruct); // Enable the USART3 peripheral USART_Cmd(USART3, ENABLE); } void USART3_IRQHandler(void) { if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) { // Read the received byte from USART3 uint8_t data = USART_ReceiveData(USART3); // Process the received byte using MDB protocol // ... // Clear the USART3 receive interrupt flag USART_ClearITPendingBit(USART3, USART_IT_RXNE); } } ``` 在`init_USART3()`函数中,我们首先启用了USART3和GPIOB的时钟,并针对GPIOB的PB10和PB11两个引脚配置了GPIO初始化结构体`GPIO_InitStruct`,然后将其连接到USART3的备用功能上,并配置USART3的初始化结构体`USART_InitStruct`,包括波特率、数据位、停止位、校验位、硬件流控制和模式等。在此之后,我们还启用了USART3的接收中断,并为USART3配置了NVIC中断向量,最后启用了USART3外设。 在`USART3_IRQHandler()`中断处理函数中,我们检查USART3是否接收到了数据,并读取该数据。接下来,我们使用MDB协议处理接收到的字节,该协议用于连接售货机的外设,如硬币器和纸币器等。最后,我们清除了USART3的接收中断标志位。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

没时间解释了快上车

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值