Can't get STM32F103RB usart1 to communicate with hc-05

6

I am unable to connect USART_1 with bluetooth hc-05. The Microcontroller is STM32F103RB. Here's the code (I am not using any library)

#define APB2_FREQ 72000000

void USART1_Send(char* data)
{   
    unsigned int length = 0;
    while(data[length] != '\0')
        ++length;       

    int i = 0;
    while(i < length)
    {
        while((USART1_SR & 0x00000080) == 0); // wait if transmit data register is not empty
        USART1_DR = data[i];
        ++i;
    }
}

char USART1_Receive()
{
    while((USART1_SR & 0x00000020) == 0);     //wait for data to arrive
    return USART1_DR;
}

void USART1_Init(int baudrate, short parity, short stop)
{
    RCC_APB2ENR |= 0x00004004;                // enable Port A, enable usart1 clock

    GPIOA_CRH &= 0xFFFFF00F;
    GPIOA_CRH |= 0x000004A0;                  // pin 9 = alternate function push-pull, pin 10 = Floating Input


    USART1_CR1 &= 0x00000000;                 // resetting the parity bit to 0(which is no parity) and "M bit" to 0(which is 8 bit data)

    if(parity == 1)                           // even parity
        USART1_CR1 |= 0x00000400;
    else if(parity == 2)
        USART1_CR1 |= 0x00000600;             // odd parity


    USART1_CR2 &= 0x00000000;                 // Reset USART_CR2, 1 stop bit
    if(stop == 2)
        USART1_CR2 |= 0x00002000;             // 2 stop bit

    USART1_BRR = APB2_FREQ /(baudrate);


    USART1_CR1 |= 0x00002000;                 // enable UART

    USART1_CR1 |= 0x0000000C;               // enable Transmiter, receiver
    USART1_Send("LINK OK:\r\n");
}

int main(void)
{   
    USART1_Init(9600, 0, 1);                  // 9600 baudrate, no parity, 1 stop bit

    while(1){
        USART1_Send("Hello World\n");
    }

    return (1);
}

The above program, configures USART1 and transmits Hello World. When I run the code in Keil uvision4, uart1 window prints Hello World repeatedly.

enter image description here

However when I burn the code in STM32F103RB, and connect it with Bluetooth and pair the bluetooth with my mobile, it doesnot display anything on Bluetooth Terminal app in Mobile.

Here's how I've hooked up the wires,

  • Bluetooth Ground > STM32 Ground
  • Bluetooth 5v > STM32 5v
  • Bluetooth Rx > STM32 PA9
  • Bluetooth Tx > STM32 PA10

I've tried the same Bluetooth with arduino and it worked fine.

Thanks!!!

stm32
uart
keil
hc-05
asked on Stack Overflow Jun 14, 2018 by Muzahir Hussain • edited Jun 16, 2018 by Muzahir Hussain

1 Answer

2

Got it... The Micro-controller was running off the 8 MHz HSI as system clock.

So, there are couple of solutions,

One of the solutions is to change the clock frequency in baudrate calculation to 8000000,

USART1_BRR = 8000000 /(baudrate);

Another solution is to increase system clock to 72MHz. A way to do that is to configure PLL in such a way that it multiplies the clock frequency of HSI or HSE upto 72 MHz and then use PLL as System clock.

Hope this helps someone.

answered on Stack Overflow Jun 17, 2018 by Muzahir Hussain • edited Jun 23, 2018 by Muzahir Hussain

User contributions licensed under CC BY-SA 3.0