求助!! stm32f401re 串口乱码

2019-07-20 03:49发布

新人求帮助!!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
8条回答
滕召群
1楼-- · 2019-07-20 06:03
不是你程序问题,是你的串口软件上面的 HEX显示选项没勾上,所以出现乱码,勾上就能看到正常数据了
dateher
2楼-- · 2019-07-20 06:29
贴上代码:
void USART1_Config(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
       
        //Ïàó|ê±Öóê1Äü
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);  //ê1ÄüUSART1ê±Öó
        RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOB  , ENABLE); //ê1ÄüGPIOBê±Öó
       
        //′®¿ú1¶Ôó|òy½Å¸′óÃó3éä
        GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_USART1); //GPIOB9¸′óÃÎaUSART1  
        GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_USART1); //GPIOB10¸′óÃÎaUSART1  
       
        //USART1¶Ë¿úÅäÖÃ
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; //TX | RX
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   //Ëù¶è50MHz  
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíì¸′óÃêä3ö
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
        GPIO_Init(GPIOB,&GPIO_InitStructure); //3õê¼»ˉPA9,PA10

        NVIC_Configuration();       
       
        //USART1 3õê¼»ˉéèÖÃ
        USART_InitStructure.USART_BaudRate = 115200; //2¨ìØÂêéèÖÃ
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;  //×Ö3¤Îa8λêy¾Y¸ñê½
        USART_InitStructure.USART_StopBits = USART_StopBits_1;  //ò»¸öí£Ö1λ
        USART_InitStructure.USART_Parity = USART_Parity_No ;  //ÎTÆæż¼ìÑéλ
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //ÎTó2¼têy¾Yá÷¿ØÖÆ
        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;  //êÕ·¢Ä£ê½
        USART_Init(USART1, &USART_InitStructure);  //3õê¼»ˉ′®¿ú1
       
        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);  //¿aÆôÏà1ØÖD¶Ï
       
        USART_Cmd(USART1, ENABLE);  //ê1Äü′®¿ú1
}


void NVIC_Configuration(void)
{
        NVIC_InitTypeDef NVIC_InitStructure;
        /* Configure the NVIC Preemption Priority Bits */  
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
       
        /* Enable the USARTy Interrupt */
        NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;          //′®¿ú1ÖD¶Ïí¨μà
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00; //ÇàÏèóÅÏ輶
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;  //×óóÅÏ輶
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  //IRQí¨μàê1Äü
        NVIC_Init(&NVIC_InitStructure);  //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷
}



/*
* oˉêyÃû£ofputc
* Ãèêö  £oÖض¨Ïòc¿aoˉêyprintfμ½USART1
* êäèë  £oÎT
* êä3ö  £oÎT
* μ÷óà £oóéprintfμ÷óÃ
*/
int fputc(int ch, FILE *f)
{
        /* ½&#171rintfÄúèY·¢íù′®¿ú */
        USART_SendData(USART1, (unsigned char) ch);
        /* μè′y·¢Ëííê±Ï */
  while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);      
       
        return (ch);
}

/// Öض¨Ïòc¿aoˉêyscanfμ½USART1
int fgetc(FILE *f)
{
                /* μè′y′®¿ú1êäèëêy¾Y */
                while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);

                return (int)USART_ReceiveData(USART1);
}

int main(void)
{       
        /* USART1 config 115200 8-N-1 */
        USART1_Config();


        printf(" this is a USART Interrupt demo ");

        //delay(0x0FFFEF);

        while (1)
        {
               
        }
}
dateher
3楼-- · 2019-07-20 08:08
 精彩回答 2  元偷偷看……
dateher
4楼-- · 2019-07-20 10:26
 精彩回答 2  元偷偷看……
dateher
5楼-- · 2019-07-20 13:18
已经解决,  结帖!
dateher
6楼-- · 2019-07-20 15:30
 精彩回答 2  元偷偷看……

一周热门 更多>