-1

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; }
  • If I was to express my opinion why you're not getting answers it would be that you use raw values and write them directly into registers. People here are used to working with constants coming from CMSIS and HAL (including LL HAL). If you were to refactor your code to use those, you'd probably get help much faster. Right now I'm suspecting not many are going to be determined enough to dive into reference manual to painstakingly check the meaning of each bit. I also wouldn't be surprised if that wasn't the reason why you get an error in the first place - it's quite easy for a mistake this way. – J_S Nov 30 '19 at 16:03
  • anyway . but why usart1 work but usart2 send garbage as i use same configuration for both only changing clock enable and Tx and Rx pins – Hesham Adel Nov 30 '19 at 16:08
  • As I said, at least when it comes to me any discussion and going into analyzing this code is out of the question unless it's refactored to be readable. – J_S Nov 30 '19 at 16:10
  • okay so you see to me to change my code with #define to each pin and make my code readable to any one to help me ? – Hesham Adel Nov 30 '19 at 16:15
  • At least use constants defined in HAL. Don't write your own defines. Preferably use HAL functions to initialize hardware, at least using LL HAL. – J_S Nov 30 '19 at 16:17
  • but i'm not need to use Hal i need to make my own drivers so i made in main a simple example to see what is the problem then i will make my own driver – Hesham Adel Nov 30 '19 at 16:19

1 Answers1

3

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.

Turbo J
  • 7,563
  • 1
  • 23
  • 43
  • thank you Turbo the Clock of the PCLK1 of the usart2 need to edit i was wrong when i edit it the usart2 work thank you very match – Hesham Adel Dec 01 '19 at 00:22