STM32 例程-5 Proteus使用串口2

代码如下

void usart2_config()
{
    GPIO_InitTypeDef gpioInitStruct;
	USART_InitTypeDef usartInitStruct;
	NVIC_InitTypeDef nvicInitStruct;	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
	//PA2	TXD
	gpioInitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
	gpioInitStruct.GPIO_Pin = GPIO_Pin_2;
	gpioInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &gpioInitStruct);
	
	//PA3	RXD
	gpioInitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	gpioInitStruct.GPIO_Pin = GPIO_Pin_3;
	gpioInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &gpioInitStruct);
	
	usartInitStruct.USART_BaudRate = 9600;
	usartInitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	usartInitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	usartInitStruct.USART_Parity = USART_Parity_No;
	usartInitStruct.USART_StopBits = USART_StopBits_1;
	usartInitStruct.USART_WordLength = USART_WordLength_8b;	
	USART_Init(USART2, &usartInitStruct);
	
	USART_Cmd(USART2, ENABLE);	
	
	

}

2、将串口1发送的数据显示到串口2终端上

 

 3、串口1发送接收

usart1_config();
usart2_config();
USART_SendData(USART1,'8');	
u8 rd=USART_ReceiveData(USART1);
USART_SendData(USART2,rd);

4、具体芯片哪些引脚用来做串口可以看DataSheet,也可以用Stm32Cube看

 

 

  

 

posted @ 2021-02-15 22:32  zhaogaojian  阅读(675)  评论(0编辑  收藏  举报