单片机供电瞬间,串口会发送一串乱码怎么处理啊?

2019-07-15 10:07发布

做串口实验的时候,给单片机供电瞬间,串口会发送一串乱码,之后执行没有问题,请问这个是什么情况啊?如何解决呢?
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
4条回答
shaoyx2014
2019-07-15 16:31
wulinwl 发表于 2017-7-24 17:03
检查程序的初始化设置是否合理

没有发现有什么问题啊,麻烦您帮忙看一下。
如下是串口初始化程序:

#include "usart1.h"

void USART1_Init(void)
{
        GPIO_InitTypeDef  GPIO_InitStructure;
        USART_InitTypeDef USART_InitStruct;
        NVIC_InitTypeDef NVIC_InitStruct;
       
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//串口时钟使能;
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//GPIO时钟使能;
       
        GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
        GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);//引脚复用映射;
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;  
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;   
  GPIO_Init(GPIOA, &GPIO_InitStructure); //GPIO端口模式设置;

        USART_InitStruct.USART_BaudRate = 9600;
        USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
        USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
        USART_InitStruct.USART_Parity = USART_Parity_No;
        USART_InitStruct.USART_StopBits = USART_StopBits_1;
        USART_InitStruct.USART_WordLength = USART_WordLength_8b;
        USART_Init(USART1, &USART_InitStruct);//串口参数初始化;
       
        USART_Cmd(USART1, ENABLE);//使能串口;
       
        USART_ClearFlag(USART1, USART_FLAG_TC);
       
        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);         //开启相关中断
       
        NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
        NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
        NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
        NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
        NVIC_Init(&NVIC_InitStruct);

}

void USART1_IRQHandler(void)//编写中断处理函数;
{
        u8 res;
        if(USART_GetITStatus(USART1,USART_IT_RXNE))
        {
                res = USART_ReceiveData(USART1);
                USART_SendData(USART1, res);//串口数据收发;
        }
}
       



然后,是主程序:

#include "usart1.h"

int main(void)
{
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //中断优先级分组
        USART1_Init();
       
  while(1);
}




一周热门 更多>