STM32F103ZET6的串口1发送数据总是在上电后第一次发送数据前几位乱码,之后正常,再等一会后再发数据有乱码在发送就正常,求取大神看看什么问题! 下面是程序
void USART1_Config(unsigned int bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* ÅäÖô®¿Ú1 £¨USART1£© ʱÖÓ*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
/*´®¿ÚGPIO¶Ë¿ÚÅäÖÃ*/
/* ÅäÖô®¿Ú1 £¨USART1 Tx (PA.09)£©*/
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);
/* ÅäÖô®¿Ú1 USART1 Rx (PA.10)*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* ´®¿Ú1ģʽ£¨USART1 mode£©ÅäÖà */
USART_InitStructure.USART_BaudRate = bound;//Ò»°ãÉèÖÃΪ9600;
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(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//¿ªÆôÖжÏ
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* ʹÄÜ´®¿Ú1ÖÐ¶Ï */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
/*IRQͨµÀʹÄÜ*/
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
/*¸ù¾ÝNVIC_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèNVIC¼Ä´æÆ÷USART1*/
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART1, ENABLE); //ʹÄÜ´®¿Ú
}
int fputc(int ch, FILE *f)
{
// ½«PrintfÄÚÈÝ·¢Íù´®¿Ú
USART_SendData(USART1, (unsigned char) ch);
while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET);
return (ch);
}
|