2018-06-16 07:35 AM
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,
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.Solved! Go to Solution.
2018-06-16 02:06 PM
Suggests the time basis is 8 MHz, not 72 MHz
9600 / 9 = 1066.667
72 / 8 = 9
2018-06-16 08:43 AM
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
2018-06-16 08:49 AM
I would check TXE *before* outputting data to the DR
2018-06-16 12:02 PM
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.
2018-06-16 12:30 PM
>>
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.
2018-06-16 12:50 PM
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!
2018-06-16 01:08 PM
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.
2018-06-16 01:53 PM
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?
2018-06-16 02:06 PM
Suggests the time basis is 8 MHz, not 72 MHz
9600 / 9 = 1066.667
72 / 8 = 9
2018-06-16 04:15 PM
Yup you are right...
I'tried,
USART1_BRR = 8000000 /(baudrate);
It worked fine...
Thanks you verrry muchhh!