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
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
int main(void)
{
int a;
unsigned char i = 0;
SystemInit(); //ÅäÖÃϵͳʱÖÓΪ 72M
USART1_Config(); //USART1 ÅäÖÃ
USART2_Config();
while(1)
{
UART2Test();
}
}
运行结果:
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
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
int main(void)
{
int a;
unsigned char i = 0;
SystemInit(); //ÅäÖÃϵͳʱÖÓΪ 72M
USART1_Config(); //USART1 ÅäÖÃ
USART2_Config();
while(1)
{
UART2Test();
}
}
运行结果:
举报