1
\$\begingroup\$

I'm trying to get a HC-05 bluetooth module working with stm32f103c8t6 module(blue pill). I used this usb to serial module to test each of the aforementioned boards and this Android app to connect to bluetooth. Both STM32 and HC05 works fine individually. I can send and receive data over serial and android app flawlessly at the baudrate of 9600(using USART1 of stm32f103).

Schematic

The problem is when I connect HC05 and micro-controller together, it doesn't work. The micro can receive data from bluetooth(I know because it toggles LED on and off) but I can't see any data being received on my Android phone. Here is my code(The code is compiled and flashed using Coocox IDE):

#include "stm32f103xb.h"
#include <stdlib.h>
#include <stdio.h>
#include "STM32_USART.h"

int main(void)
{

// Configure system clock and prescalers
RCC->CR |=  RCC_CR_HSION;
// Wait till HSI is ready
while((RCC->CR & RCC_CR_HSIRDY) != RCC_CR_HSIRDY){}
RCC->CFGR &= ~(RCC_CFGR_HPRE);  // set AHB prescaler to 1
RCC->CFGR = ((RCC->CFGR & ~(RCC_CFGR_PPRE1)) | RCC_CFGR_PPRE1_DIV2); // set APB1 prescaler to 2 (PCLK1)
RCC->CFGR = ((RCC->CFGR & ~(RCC_CFGR_PPRE2)) | RCC_CFGR_PPRE2_DIV1); // set APB2 prescaler to 1 (PCLK2)

RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;  // Enable Port A

// Configure a pin for LED
GPIOA->CRL |= GPIO_CRL_MODE1;
GPIOA->CRL &= ~GPIO_CRL_CNF1;

GPIOA->BSRR |= GPIO_BSRR_BS1;
// Initialize USART1 communication 
usart_init();

while(1){
    char data = usartByteReceive();
    //      usartByteSend(85);
    //      char data = ' ';
    if(data == '1')
    {
        // Turn LED on
        GPIOA->BSRR |= GPIO_BSRR_BS1;
        usartByteSend('F');  // send F
        usartByteSend('\r');
        usartByteSend('\n');
    }
    else if(data == '0')
    {
        // Turn LED off
        GPIOA->BRR |= GPIO_BRR_BR1;
        usartByteSend('S'); // send S
        usartByteSend('\r');
        usartByteSend('\n');
    }
    }
}

After this I thought perhaps the problem is the system clock, so I changed to PLL clock for system clock using an 8MHz external crystal(built-in on the module) to have more accurate clock, but no luck:\

Here is the code for that:

#include "stm32f103xb.h"
#include <stdlib.h>
#include <stdio.h>
#include "STM32_USART.h"

int main(void)
{

// Configure system clock and prescalers
// Set the HSE on
RCC->CR |=  RCC_CR_HSEON;
// Wait till HSE is ready
while((RCC->CR & RCC_CR_HSERDY) != RCC_CR_HSERDY){}
// Set HSI on
RCC->CR |=  RCC_CR_HSION;
// Wait till HSI is ready
while((RCC->CR & RCC_CR_HSIRDY) != RCC_CR_HSIRDY){}
// Disable the PLL
RCC->CR &= ~RCC_CR_PLLON;
// Wait till main PLL is disabled
while((RCC->CR & RCC_CR_PLLRDY) == RCC_CR_PLLRDY){}
// Configure the HSE prediv factor
// It can be written only when the PLL is disabled. Not used in PLL source is different than HSE
RCC->CFGR &= ~RCC_CFGR_PLLXTPRE;
RCC->CFGR = (RCC->CFGR & ~(RCC_CFGR_PLLMULL | RCC_CFGR_PLLSRC)) | (RCC_CFGR_PLLMULL2 | RCC_CFGR_PLLSRC);

// Enable the PLL
RCC->CR |= RCC_CR_PLLON;
// Wait till main PLL is enabled
while((RCC->CR & RCC_CR_PLLRDY) != RCC_CR_PLLRDY){}

// SYSTEM CLOCK
FLASH->ACR = (FLASH->ACR & ~(FLASH_ACR_LATENCY)) | FLASH_ACR_LATENCY_1;
RCC->CFGR &= ~(RCC_CFGR_HPRE);  // set AHB prescaler to 1
RCC->CFGR |= RCC_CFGR_SW_PLL;   // set SYSCLK to PLL source
// wait till system clock is set to PLL
while((RCC->CFGR & RCC_CFGR_SWS_PLL) != RCC_CFGR_SWS_PLL){}
RCC->CFGR = ((RCC->CFGR & ~(RCC_CFGR_PPRE1)) | RCC_CFGR_PPRE1_DIV2); // set APB1 prescaler to 2 (PCLK1)
RCC->CFGR = ((RCC->CFGR & ~(RCC_CFGR_PPRE2)) | RCC_CFGR_PPRE2_DIV1); // set APB2 prescaler to 1 (PCLK2)

RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;  // Enable Port A

// Configure a pin for LED
GPIOA->CRL |= GPIO_CRL_MODE1;
GPIOA->CRL &= ~GPIO_CRL_CNF1;

GPIOA->BSRR |= GPIO_BSRR_BS1;
// Initialize USART1 communication 
usart_init();

while(1){
    char data = usartByteReceive();
    //      usartByteSend(85);
    //      char data = ' ';
    if(data == '1')
    {
        // Turn LED on
        GPIOA->BSRR |= GPIO_BSRR_BS1;
        usartByteSend('F');   // Send F
        usartByteSend('\r');
        usartByteSend('\n');
    }
    else if(data == '0')
    {
        // Turn LED off
        GPIOA->BRR |= GPIO_BRR_BR1;
        usartByteSend('S'); // Send S
        usartByteSend('\r');
        usartByteSend('\n');
    }
    }
}

Do you guys have any idea what is wrong??


I think it's worth mentioning that I wanted to see whether the micro is sending anything or not, I connected the usb to serial module to the USART1 while the bluetooth was connected to USART1 too. After this I was able to send and receive data from micro-controller in my Android app, but I couldn't send or get anything on my computer through usb to serial module.:\

Is something wrong with voltage?? (I have connected bluetooth to both 5v and 3.3v of STM32, no difference)

\$\endgroup\$
  • \$\begingroup\$ Would you post a schematic please? Does the scope verify that you have the correct baud rate between module and STM32? Does the module need RTS/CTS and if so, have you implemented it on the STM32? \$\endgroup\$ – DiBosco Feb 23 '18 at 18:14
  • \$\begingroup\$ @DiBosco I added the schematic. Yes they are all working in 9600 bps(I checked it by connecting each board to computer and monitored transmissions). I don't think it needs RTS/CTS since I got it working with arduino UNO. \$\endgroup\$ – dub-dub Feb 23 '18 at 18:50
  • \$\begingroup\$ Can't see the schematic! \$\endgroup\$ – DiBosco Feb 23 '18 at 18:53
  • \$\begingroup\$ @DiBosco it's there:\, here is the link: imgur.com/a/V70Qf \$\endgroup\$ – dub-dub Feb 23 '18 at 18:57
  • \$\begingroup\$ OK, it looks OK if you have it wired up as that somewhat sketchy schematic. I must confess I'm not sure I'm visualising exactly what you have got working. You have had the PC use the USB to serial to talk to the HC05 which then sent a message to the phone? You got that going both ways? Are you sure flow control wasn't being used in that situation (assuming I have understood). You also had the USB to serial talk to the STM32 from the PC and it talked back? Then you have HC05 to STM32 and the phone is getting nothing when you send out from the STM32 and vice versa? \$\endgroup\$ – DiBosco Feb 23 '18 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.