stm32f103,串口usart1往PC上发数据,大概快一分钟开始乱码问题

2019-10-15 18:10发布

如题:
用的stm32f103c8t6,串口usart1往PC上发数据,大概快一分钟开始乱码(见附件图片),点一下“手动发送”会恢复正常,过个快一分钟又开始乱码,再点“手动发送”又恢复正常,一直这样。
求大大们帮我看看我的代码是不是有问题,还有可能是哪里出了问题,目前猜想是不是中断的问题,可是不知道中断跟usart有什么关系。
PS:
波特率设置的9600,一开始设置的115200数据完全乱码。

这是我的代码:
[mw_shl_code=c,true]int main(void)
{  
    char temp = 0;
    char buf[] = {"Usart_SendData function!"};
       
    /* 设置时钟频率为 70M */      
    SystemInit();
    /* USART1 config 9600 8-N-1 */
    USART1_Config();

    while (1)
    {
        for (temp=0; temp<24; temp++)
        {
            USART_SendData(USART1, buf[temp]);
            while( USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET );
        }
               
        mydelay_ms(2000);
    }
}
[/mw_shl_code]

[mw_shl_code=c,true]void USART1_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;

    /* config USART1 clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);

    /* USART1 GPIO config */
    /* Configure USART1 Tx (PA.09) as alternate function push-pull */
    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);   
    /* Configure USART1 Rx (PA.10) as input floating */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
          
    /* USART1 mode config */
    USART_InitStructure.USART_BaudRate = 9600;
    //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(USART1, &USART_InitStructure);
    USART_Cmd(USART1, ENABLE);
}[/mw_shl_code]


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。