• 已解决 73482 个问题
  • 已帮助 5993 位优秀工程师

各位大佬,我想要多串口同时工作,麻烦指点一下,在线等

a2605 2019-09-17 浏览量:664

sys_config.c

#include "sys_config.h"
#include "stm32f10x.h"
#include "stm32f10x_adc.h" 
#include "stm32f10x_bkp.h" 
#include "stm32f10x_can.h" 
#include "stm32f10x_cec.h" 
#include "stm32f10x_crc.h" 
#include "stm32f10x_dac.h" 
#include "stm32f10x_dbgmcu.h"
#include "stm32f10x_dma.h"
#include "stm32f10x_exti.h" 
#include "stm32f10x_flash.h"
#include "stm32f10x_fsmc.h" 
#include "stm32f10x_gpio.h"
#include "stm32f10x_i2c.h" 
#include "stm32f10x_iwdg.h" 
#include "stm32f10x_pwr.h" 
#include "stm32f10x_rcc.h" 
#include "stm32f10x_rtc.h" 
#include "stm32f10x_sdio.h" 
#include "stm32f10x_spi.h" 
#include "stm32f10x_tim.h" 
#include "stm32f10x_usart.h" 
#include "stm32f10x_wwdg.h" 
#include "misc.h"


/@@*USART1配置*/
void USART1_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
       
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA ENABLE );
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 ENABLE );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1 TX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA &GPIO_InitStructure); //端口A;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART1 RX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA &GPIO_InitStructure); //端口A;

USART_InitStructure.USART_BaudRate = 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);//配置串口参数;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置中断组,4位抢占优先级,4位响应优先级;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //中断号;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

USART_ITConfig(USART1 USART_IT_RXNE ENABLE);
USART_Cmd(USART1 ENABLE); //使能串口;
}

void USART1_Send_Byte(u8 Data) //发送一个字节;
{
USART_SendData(USART1Data);
while( USART_GetFlagStatus(USART1 USART_FLAG_TC) == RESET );
}

void USART1_Send_String(u8 *Data) //发送字符串;
{
while(*Data)
USART1_Send_Byte(*Data++);
}

void USART1_IRQHandler(void) //中断处理函数;
{
u8 res;    
if(USART_GetITStatus(USART1 USART_IT_RXNE) == SET) //判断是否发生中断;
{
USART_ClearFlag(USART1 USART_IT_RXNE); //清除标志位;
res=USART_ReceiveData(USART1); //接收数据;
USART1_Send_Byte(res); //用户自定义;
}  
} 

/@@*USART2配置*/
void USART2_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;        

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 ENABLE );

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); 

USART_InitStructure.USART_BaudRate = 115200; 
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;
USART_Init(USART2 &USART_InitStructure);

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); 
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; 
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

USART_ITConfig(USART2 USART_IT_RXNE ENABLE);
USART_Cmd(USART2 ENABLE);
}

void USART2_Send_Byte(u8 Data) //发送一个字节;
{
USART_SendData(USART2Data);
while( USART_GetFlagStatus(USART2 USART_FLAG_TC) == RESET );
}

void USART2_Send_String(u8 *Data) //发送字符串;
{
while(*Data)
USART2_Send_Byte(*Data++);
}

void USART2_IRQHandler(void) //中断处理函数;
{
u8 res;    
if(USART_GetITStatus(USART2 USART_IT_RXNE) == SET) //判断是否发生中断;
{
USART_ClearFlag(USART2 USART_IT_RXNE); //清除标志位;
res=USART_ReceiveData(USART2); //接收数据;
USART2_Send_Byte(res); //用户自定义;
}  
} 

void UART4_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;        

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4 ENABLE );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC &GPIO_InitStructure); 
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 
GPIO_Init(GPIOC &GPIO_InitStructure); 

USART_InitStructure.USART_BaudRate = 115200; 
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;
USART_Init(UART4 &USART_InitStructure);

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); 
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

USART_ITConfig(UART4 USART_IT_RXNE ENABLE);
USART_Cmd(UART4 ENABLE);
}

void UART4_Send_Byte(u8 Data)
{
USART_SendData(UART4Data);
while( USART_GetFlagStatus(UART4 USART_FLAG_TC) == RESET );
}

void UART4_Send_String(u8 *Data)
{
while(*Data)
UART4_Send_Byte(*Data++);
}

void UART4_IRQHandler(void)
{
u8 res;    
if(USART_GetITStatus(UART4 USART_IT_RXNE) == SET) 
{
USART_ClearFlag(UART4 USART_IT_RXNE);
res=USART_ReceiveData(UART4);
UART4_Send_Byte(res);
}  
} 

void UART5_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;        

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5 ENABLE );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC &GPIO_InitStructure);
    
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD &GPIO_InitStructure);

USART_InitStructure.USART_BaudRate = 115200; 
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;
USART_Init(UART5 &USART_InitStructure);

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); 
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

USART_ITConfig(UART5 USART_IT_RXNE ENABLE);
USART_Cmd(UART5 ENABLE); 
}

void UART5_Send_Byte(u8 Data)
{
USART_SendData(UART5Data);
while( USART_GetFlagStatus(UART5 USART_FLAG_TC) == RESET );
}

void UART5_Send_String(u8 *Data) 
{
while(*Data)
UART5_Send_Byte(*Data++);
}

void UART5_IRQHandler(void)
{
u8 res;    
if(USART_GetITStatus(UART5 USART_IT_RXNE) == SET) 
{
USART_ClearFlag(UART5 USART_IT_RXNE);
res=USART_ReceiveData(UART5); 
UART5_Send_Byte(res); 
}  
} 

/@@*......GPIO配置......*/
void GPIO_Config(void)
{  
	 
   GPIO_InitTypeDef GPIO_InitStructure; //定义初始化结构体
   
	 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA ENABLE);
	 GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable  ENABLE);////关闭jtag功能
	 
	 //SYS-LED
	 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
	 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	
	 GPIO_Init(GPIOA &GPIO_InitStructure);
}

/@@*重新定义USART1-printf配置*/
int fputc(int ch FILE *f)
{
  
  USART_SendData(USART1 (uint8_t) ch);	
  while (USART_GetFlagStatus(USART1 USART_FLAG_TC) == RESET);{}
  return ch;
}

/@@*重新定义scanf到串口,用于scanf,getchar等*/
int fgetc(FILE *f)
{
		/@@*等待串口输入数据*/
		while (USART_GetFlagStatus(USART1 USART_FLAG_RXNE) == RESET);

		return (int)USART_ReceiveData(USART1);
}

//#if 0
//void EXTI_Config(void)
//{
//	EXTI_InitTypeDef EXTI_InitStructure;
//	/@@*PD11外部中断输入*/
//	EXTI_InitStructure.EXTI_Line = EXTI_Line13; 
//	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
//	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
//	EXTI_InitStructure.EXTI_LineCmd	= ENABLE;
//	EXTI_Init(&EXTI_InitStructure);
//	
//	/@@*PD12外部中断输入*/
//	EXTI_InitStructure.EXTI_Line = EXTI_Line12;
//	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
//	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
//	EXTI_InitStructure.EXTI_LineCmd	= ENABLE; 
//	EXTI_Init(&EXTI_InitStructure);
//}

//#endif



sys_config.h

#ifndef __SYS_CONFIG_H_
#define __SYS_CONFIG_H_
#include "stm32f10x_conf.h"
#include "stm32f10x.h"
#include <stdio.h>

/@@*使用位带操作,对单个IO灵活操作*/
#define GPIOA_ODR_Addr    (GPIOA_base+12) //0x4001080C 
#define GPIOB_ODR_Addr    (GPIOB_base+12) //0x40010C0C 
#define GPIOC_ODR_Addr    (GPIOC_base+12) //0x4001100C 
#define GPIOD_ODR_Addr    (GPIOD_base+12) //0x4001140C 
#define GPIOE_ODR_Addr    (GPIOE_base+12) //0x4001180C 
#define GPIOF_ODR_Addr    (GPIOF_base+12) //0x40011A0C    
#define GPIOG_ODR_Addr    (GPIOG_base+12) //0x40011E0C    

#define GPIOA_IDR_Addr    (GPIOA_base+8) //0x40010808 
#define GPIOB_IDR_Addr    (GPIOB_base+8) //0x40010C08 
#define GPIOC_IDR_Addr    (GPIOC_base+8) //0x40011008 
#define GPIOD_IDR_Addr    (GPIOD_base+8) //0x40011408 
#define GPIOE_IDR_Addr    (GPIOE_base+8) //0x40011808 
#define GPIOF_IDR_Addr    (GPIOF_base+8) //0x40011A08 
#define GPIOG_IDR_Addr    (GPIOG_base+8) //0x40011E08 
#define PFout(n)	*((volatile unsigned long *)(0x42000000+((GPIOF_ODR_Addr-0x40000000)<<5)+(n<<2)))

void GPIO_Config(void);
void USART1_Configuration(void);
void USART2_Configuration(void);
void EXTI_Config(void);

void USART2_Send_Byte(u8 Data);
void USART2_Send_String(u8 *Data);
void USART2_IRQHandler(void);

void USART1_Send_Byte(u8 Data);
void USART1_Send_String(u8 *Data);




#endif



main.c


#include <stdio.h>
#include "stm32f10x.h"
#include "sys_config.h" 
#include "sys_led.h" 
#include "delay.h" 


/@@*SYS_LED*/
int main(void)
{
	USART1_Configuration();
	USART2_Configuration();
	LED_GPIO_Config();
	GPIO_Config();
  printf("\r\n start.......................\r\n");
	while(1)
 {
	LED_ON;
	delay_ms(2500);
	printf("\r\n lock.......................\r\n");
	LED_OFF;
	delay_ms(2500); 
	printf("\r\n open.......................\r\n");
	USART_SendData(USART1'1');
	USART_SendData(USART2'A');
	printf("\r\n send ok.......................\r\n");

	delay_ms(2500);
	}
}


各位大佬,我想要多串口同时工作,目前我这样配置串口,只有usart1可以正常工作收发,2/4/5都不可以,麻烦各位大佬指点一下。

0 0 收起

我来回答

上传资料:
选择文件 文件大小不超过15M(格式支持:doc、ppt、xls、pdf、zip、rar、txt)
最佳答案
  • 配置串口,你可以使用cubemx 软件配置,

    开通接收和发送DMA,这样能最大限度的提高效率。

    • 发布于 2019-09-23
    • 举报
    • 评论 0
    • 1
    • 0

其他答案 数量:0

相关问题

问题达人换一批

各位大佬,我想要多串口同时工作,麻烦指点一下,在线等