stm32双串口相互通信

该博客介绍了如何在STM32F103C8T6微控制器上配置并使用串口一(USART1)和串口二(USART2)进行相互通信。通过Keil4编译,博主详细展示了串口的GPIO配置、USART初始化以及数据发送和接收的函数实现。在实际操作中,串口助手能够成功实现两个串口间的信息交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Stm32f103串口一与串口而相互通信

接线:

串口一:

     USART                                          STM32

      RXD                                           PA2

      TXD                                           PA3

       GND                                          GND

 

串口二:

       USART                                        STM32

       RXD                                        PA9

       TXD                                        PA10

       GND                                       GND

该程序是用keil4来编译的,stm32f103c8t6最小系统板

串口一与串口二通过串口助手可以相互发信息

usart.c的代码为:

#include "usart2.h"
/***************************************
 * ÎļþÃû  £ºusart1.c
 * ÃèÊö    £ºÅäÖÃUSART1         
 * ʵÑéÆ½Ì¨£ºMINI STM32¿ª·¢°å »ùÓÚSTM32F103C8T6
 * Ó²¼þÁ¬½Ó£º------------------------
 *          | PA9  - USART1(Tx)      |
 *          | PA10 - USART1(Rx)      |
 *           ------------------------
 * ¿â°æ±¾  £ºST3.0.0  

**********************************************************************************/

#include "usart1.h"
#include <stdarg.h>

int a;


void USART1_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;

	/* ʹÄÜ USART1 ʱÖÓ*/
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); 

	/* USART1 ʹÓÃIO¶Ë¿ÚÅäÖà */    
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  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);   //³õʼ»¯GPIOA
	  
	/* USART1 ¹¤×÷ģʽÅäÖà */
	USART_InitStructure.USART_BaudRate = 115200;	//²¨ÌØÂÊÉèÖãº115200
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;	//Êý¾ÝλÊýÉèÖãº8λ
	USART_InitStructure.USART_StopBits = USART_StopBits_1; 	//ֹͣλÉèÖãº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);  //³õʼ»¯USART1
	USART_Cmd(USART1, ENABLE);// USART1ʹÄÜ
}


void USART2_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;

	/* ʹÄÜ USART1 ʱÖÓ*/
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 | RCC_APB2Periph_GPIOA, ENABLE); 

	/* USART1 ʹÓÃIO¶Ë¿ÚÅäÖà */    
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);    
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;	//¸¡¿ÕÊäÈë
  GPIO_Init(GPIOA, &GPIO_InitStructure);   //³õʼ»¯GPIOA
	  
	/* USART1 ¹¤×÷ģʽÅäÖà */
	USART_InitStructure.USART_BaudRate = 115200;	//²¨ÌØÂÊÉèÖãº115200
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;	//Êý¾ÝλÊýÉèÖãº8λ
	USART_InitStructure.USART_StopBits = USART_StopBits_1; 	//ֹͣλÉèÖãº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(USART2, &USART_InitStructure);  //³õʼ»¯USART1
//	// ?????????
//	NVIC_Configuration();
//  USART_ITConfig( USART2, USART_IT_RXNE, ENABLE );                                        /* ???????? */

	USART_Cmd(USART2, ENABLE);// USART1ʹÄÜ
}

 /*·¢ËÍÒ»¸ö×Ö½ÚÊý¾Ý*/
 void UART2SendByte(unsigned char SendData)
{	   
        USART_SendData(USART2,SendData);
        while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);	    
}  

/*½ÓÊÕÒ»¸ö×Ö½ÚÊý¾Ý*/
unsigned char UART2GetByte(unsigned char* GetData)
{   	   
        if(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)
        {  return 0;//ûÓÐÊÕµ½Êý¾Ý 
		}
        *GetData = USART_ReceiveData(USART2); 
        return 1;//ÊÕµ½Êý¾Ý
}
/********************************************/

 /*·¢ËÍÒ»¸ö×Ö½ÚÊý¾Ý*/
 void UART1SendByte(unsigned char SendData)
{	   
        USART_SendData(USART1,SendData);
        while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);	    
}  

/*½ÓÊÕÒ»¸ö×Ö½ÚÊý¾Ý*/
unsigned char UART1GetByte(unsigned char* GetData)
{   	   
        if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
        {  return 0;//ûÓÐÊÕµ½Êý¾Ý 
		}
        *GetData = USART_ReceiveData(USART1); 
        return 1;//ÊÕµ½Êý¾Ý
}
/*½ÓÊÕÒ»¸öÊý¾Ý£¬ÂíÉÏ·µ»Ø½ÓÊÕµ½µÄÕâ¸öÊý¾Ý*/
void UART2Test(void)
{
       unsigned char i = 0;
				unsigned char a = 0;
       while(1)
       {    
		   if(UART2GetByte(&i))
        {
           USART_SendData(USART1,i);
					// USART_SendData(USART1,'b');
			
        }
				else if(UART1GetByte(&a)){
						USART_SendData(USART2,a);
	   }
   }     
}

//void USART2_IRQHandler( void )                                                                          /* ??1?????? */
//{
//	unsigned char res;

//	if ( USART_GetITStatus(USART2,USART_IT_RXNE) != RESET )                                      /* ????(?????????0x0d 0x0a??) */
//	{
//		res = USART_ReceiveData(USART2);                                                      /* ???????? */
//		USART_SendData(USART2,res);
//	}

//}


  
#if 1
#pragma import(__use_no_semihosting)             
               
struct __FILE 
{ 
	int handle; 

}; 

FILE __stdout;       

void _sys_exit(int x) 
{ 
	x = x; 
} 
//fgetcÖØ¶¨Ïò
int fputc(int ch, FILE *f)
{
	
		while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);
		USART_SendData(USART2,(uint8_t)ch);
	

	
	return ch;
}
#endif 


 

main.c主函数的代码:


/**************************************
 * ÎļþÃû  £ºmain.c
 * ÃèÊö    £ºÍ¨¹ý´®¿Úµ÷ÊÔÈí¼þ£¬Ïò°å×Ó·¢ËÍÊý¾Ý£¬°å×Ó½ÓÊÕµ½Êý¾Ýºó£¬Á¢¼´»Ø´«¸øµçÄÔ¡£         
 * ʵÑéÆ½Ì¨£ºMINI STM32¿ª·¢°å »ùÓÚSTM32F103C8T6
 * ¿â°æ±¾  £ºST3.0.0  																										  

*********************************************************/

#include "stm32f10x.h"
#include "usart1.h"
#include "usart2.h"
#include <stdio.h>

int main(void)
{  
		int a;
	  unsigned char i = 0;   
    SystemInit();	//ÅäÖÃϵͳʱÖÓΪ 72M 
   
	  USART1_Config(); //USART1 ÅäÖÃ 		
    USART2_Config();
  

	  while(1)
		{
       
			
			
			 UART2Test();
			
		}
}




运行结果:

 

 

我这里放一份参考代码自行下载:https://download.csdn.net/download/HHHSSD/16126675

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值