STM32F07xx单片机串口4配置

    GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
    USART_ClockInitTypeDef USART_ClockInitStructure;
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC,ENABLE); 
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART4,ENABLE);

	//端口重映射
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_4); 	//TX
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_4);  //RX

	//初始化管脚
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOC, &GPIO_InitStructure);  

  RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART4, ENABLE);
  RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART4, DISABLE);
	USART_InitStructure.USART_BaudRate = 115200;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	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_ClockInitStructure.USART_Clock = USART_Clock_Disable;
	USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
	USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
	USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;

	USART_ClockInit(USART4, &USART_ClockInitStructure);
	USART_Init(USART4, &USART_InitStructure);

	//Enable usart4 receive interrupt
	USART_ITConfig(USART4, USART_IT_RXNE, ENABLE);
	// Enable the USARTx 
	USART_Cmd(USART4, ENABLE);

猜你喜欢

转载自blog.csdn.net/qq_37449342/article/details/103121417