STM32F103使用USART3/UART4乱码问题

         源程序为USART1的配置,更改USART3/4相应寄存器测试,测试一直显示有规律乱码,收发不符。

void uart_init(u32 pclk,u32 bound)
{  	 
	float temp;
	u16 mantissa;
	u16 fraction;	   
	temp=(float)(pclk*1000000)/(bound*16);//得到USARTDIV
	mantissa=temp;				 //得到整数部分
	fraction=(temp-mantissa)*16; //得到小数部分	 
    mantissa<<=4;
	mantissa+=fraction; 
  
	
	//重映射USART到PC10、11
	RCC->APB2ENR|=1<<0; //启动AFIO时钟
	AFIO->MAPR&=~(0x1<<4);
	AFIO->MAPR|=0x1<<4;
	
	RCC->APB1ENR|=1<<18;  //使能串口3时钟 
	
	RCC->APB2ENR|=1<<4;   //使能PORTC口时钟  
	GPIOC->CRH&=0XFFFF00FF;//IO状态设置
	GPIOC->CRH|=0X00008B00;//IO状态设置 
	
	RCC->APB1RSTR|=1<<18;   //复位串口3
	RCC->APB1RSTR&=~(1<<18);//停止复位	   	   
	
	//波特率设置
 	USART3->BRR=mantissa; // 波特率设置	 
	USART3->CR1|=0X200C;  //1位停止,无校验位.

}

发送:

		printf("nihao啊");

 串口助手接收:

灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~瀱瀫?灋~?~

    单片机采用8MH晶振,系统9倍频,72MHz,查STM32手册发现:

STM32F103使用USART3/UART4乱码问题_第1张图片

      USART1与其余的USART使用的时钟不一样,PCLK1最高为36Mhz

STM32F103使用USART3/UART4乱码问题_第2张图片

 初始化时PCLK=36后串口助手接收正常

nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nihao啊nih

你可能感兴趣的:(单片机,stm32,嵌入式硬件,单片机)