完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
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(); } } 运行结果: |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1777 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1621 浏览 1 评论
1080 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
728 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1678 浏览 2 评论
1937浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
730浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
570浏览 3评论
595浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
553浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-22 23:57 , Processed in 1.033447 second(s), Total 74, Slave 58 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号