cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to connect usart1 to bluetooth hc-05

Muzahir Hussain
Associate III
Posted on June 16, 2018 at 16:35

I've been trying to connect USART_1 with bluetooth hc-05 for the past few days, andI am unable to do so. The Microcontroller I am using is nucleo 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)

{

USART1_DR = data[i];

while((USART1_SR & 0x00000080) == 0); // wait if transmit data register is not empty

++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; // clear CR1 register 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);

}

This

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

https://i.stack.imgur.com/oMl4o.png

However when I burn the code in STM32F103RB, and connect it with Bluetooth and pair the bluetooth with my mobile, it doesn't 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!!!

#uart #stm32f103rb #bluetooth #keil-uvision

Note: this post was migrated and contained many threaded conversations, some content may be missing.
1 ACCEPTED SOLUTION

Accepted Solutions
Posted on June 16, 2018 at 21:06

Suggests the time basis is 8 MHz, not 72 MHz

0690X00000604hSQAQ.jpg

9600 / 9 = 1066.667

72 / 8 = 9

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

10 REPLIES 10
Posted on June 16, 2018 at 17:43

hello

baud rate is calculated  by divide p2clk/16/9600 to find USARTDIV

USARTDIV is an unsigned fixed point number that is coded on the USART_BRR register. (p791 RM0008 Rev 15)

USARTDIV =72000000/16/9600    =>   USARTDIV =468.75

so BRR mantissa is 468  and BRR fraction =0.75 (12/16)

BRR = 0x1D4C   

Posted on June 16, 2018 at 17:49

I would check TXE *before* outputting data to the DR

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 16, 2018 at 19:02

If I calculate the baudrate simply by dividing APB2_FREQ with baudrate (as done in the code),  I get same result in BRR register i.e. 0x1D4C.

USART1_BRR = APB2_FREQ /(baudrate);

Brr = 72000000 / 9600 = 7500 = 0x1D4C.

So, why can't I calculate the baudrate this way?

Thanks for your reply.

Posted on June 16, 2018 at 19:30

>>

So, why can't I calculate the baudrate this way?

You can it's an effective computational short cut

When bring up externally connected hardware I tend to use a pair of USART and forward between them, and then talk to the part via a terminal application. The HC-05/06 parts should work with some AT commands or +++ break sequences as I recall.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 16, 2018 at 19:50

I've also tried connecting the HC-05 with arduino as well. It worked fine, I didn't had to use any AT commands.

Thanks!

Posted on June 16, 2018 at 20:08

Are you sure it's clocking at 72 MHz, the 1066 baud in the debug view suggests it is running off the 8 MHz HSI. I don't see where you enable the PLL. The simulator might not be supporting the PLL.

The other way of checking the USART TX is to output a stream of 'U' (0x55) characters, and then cross check the baud rate by checking bit timings on a scope.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 16, 2018 at 20:53

Are you sure it's clocking at 72 MHz, the 1066 baud in the debug view suggests it is running off the 8 MHz HSI. I don't see where you enable the PLL. The simulator might not be supporting the PLL.

Can you please elaborate a little bit on that?

Posted on June 16, 2018 at 21:06

Suggests the time basis is 8 MHz, not 72 MHz

0690X00000604hSQAQ.jpg

9600 / 9 = 1066.667

72 / 8 = 9

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 16, 2018 at 23:15

Yup you are right...

I'tried,

USART1_BRR = 8000000 /(baudrate);

It worked fine...

Thanks you verrry muchhh!