Why USART2 Send a Garbage Value?

两盒软妹~` 提交于 2020-01-07 08:28:13

问题


Hello i'm using stm32f103c8 and i'm trying to use usart2 but it does not work . when i'm using USART1 with the same configuration of USART2 it works with me well . but USART2 send garbage to the PC terminal can someone help me to solve the problem in the USART2 ?

This is the configuration of USART2 :

int main(void){
    RCC_VidInit();                                    //INTIALIZE EXTERNAL CRYSTAL OSCILATOR = 8 MHZ
    RCC_VidEnablePeripheralClock( APB2_BUS , 2 );     //ENABLE CLOCK OF GPIOA

    CLR_BIT( GPIOA->CRL , 8  );    SET_BIT( GPIOA->CRL , 9  );    //SPEED OF PA2 = 2MHZ
    CLR_BIT( GPIOA->CRL , 10 );    SET_BIT( GPIOA->CRL , 11);     //PA2 OUTPUT PUSHPULL AF

    CLR_BIT( GPIOA->CRL , 12  );   CLR_BIT( GPIOA->CRL , 13  );   //PA3 INPUT
    SET_BIT( GPIOA->CRL , 14 );    CLR_BIT( GPIOA->CRL , 15);     //PA3 FLOATING INPUT

    RCC_VidEnablePeripheralClock( APB1_BUS , 17 );                //ENABLE CLOCK OF USART2
    SET_BIT( USART2->CR1 , 13 );   SET_BIT( USART2->CR1 , 3  );   //ENABLE UE & TE

    USART2 -> BRR = 0x341 ;     //BAUDRATE 9600 OF CRYSTLE 8MHZ BY Eq = (8000 000 / 16 * 9600)
    while(1){
        if( ( GET_BIT( USART2 -> SR , 6 ) ) == 1 ){ //CHECH ABOUT TC FLAG
            USART2 -> DR = '1' ; /*PUT DATA ON DATA REG*/    for(int i = 0 ; i <= 1000000 ; i++ ); /*JUST BASIC DELAY*/  }}
return 0; }

and This is the USART1 Configuration that works well with me

int main(void){
    RCC_VidInit();                                  //INTIALIZE EXTERNAL CRYSTAL OSCILATOR = 8 MHZ
    RCC_VidEnablePeripheralClock( APB2_BUS , 2 );   //ENABLE CLOCK OF GPIOA

    CLR_BIT( GPIOA->CRH , 4  );    SET_BIT( GPIOA->CRH , 5  );    //SPEED OF PA9 = 2MHZ
    CLR_BIT( GPIOA->CRH , 6 );     SET_BIT( GPIOA->CRH , 7  );    //PA9 OUTPUT PUSHPULL AF

    CLR_BIT( GPIOA->CRH , 8  );    CLR_BIT( GPIOA->CRH , 9  );    //PA10 INPUT
    SET_BIT( GPIOA->CRH , 10 );    CLR_BIT( GPIOA->CRH , 11 );    //PA10 FLOATING INPUT

    RCC_VidEnablePeripheralClock( APB2_BUS , 14 );                //ENABLE CLOCK OF USART1
    SET_BIT( USART1->CR1 , 13 );   SET_BIT( USART1->CR1 , 3  );   //ENABLE UE & TE

    USART1 -> BRR = 0x341 ; //BAUDRATE 9600 OF CRYSTLE 8MHZ BY Eq = (8000 000 / 16 * 9600)
    while(1){
        if( ( GET_BIT( USART1 -> SR , 6 ) ) == 1 ){ //CHECH ABOUT TC FLAG
            USART1 -> DR = '1' ; /*PUT DATA ON DATA REG*/    for(int i = 0 ; i <= 1000000 ; i++ ); /*JUST BASIC DELAY*/  }}
return 0; }

回答1:


but USART2 send garbage to the PC

That usually means your clock/baudrate setup is wrong. Maybe the APB2 bus has a different clock devider by default. One reason to use HAL, because it should catch these situations and calculate a correct BRR value.

Simplest way to diagnose is using an oscilloscope to look at the UART2 output waveform. But I would just try 1200 and 4800 baud first (guessing 1 MHz and 4 MHz base clock).

Note: OP's "delay" gets optimized out by most ARM C compilers, and must not be used in production code.



来源:https://stackoverflow.com/questions/59107459/why-usart2-send-a-garbage-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!