Skip to main content
kyle
Associate
July 11, 2014
Question

HC-06 Bluetooth Module Configure with UART

  • July 11, 2014
  • 3 replies
  • 1617 views
Posted on July 11, 2014 at 19:01

Hi,

I would like to know if it would be better to send AT commands to the HC-06 Bluetooth module via an interrupt handler or a while(1) in the main as well as some pointers on the code I am trying to get correct and working. I am not sure if I require delays to allow module to reply. Perhaps some kind of scan and printf to see what is being sent and received would help, something like below?

void USART1_IRQHandler(void)

{

  static int tx_index = 0;

  static int rx_index = 0;

    char StringRX[12];

    volatile char StringAT[] = ''AT'';

    volatile char StringBAUD[] = ''AT+BAUD4'';

    volatile char StringNAME[] = ''AT+NAMETest'';

    volatile char StringPIN[] = ''AT+PIN4321'';

  // ------------------StringAT------------------

  if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET)        

  {

    USART_SendData(USART1, StringAT[tx_index++]);

   

    if (tx_index >= (sizeof(StringRX) - 1))

      tx_index = 0;

  }

    

    if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // Received characters modify string

  {

    StringRX[rx_index++] = USART_ReceiveData(USART1);

   

    if (rx_index >= (sizeof(StringRX) - 1))

      rx_index = 0;

  }

    

    // ------------------StringBAUD------------------

    if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET) // Transmit the string in a loop

  {

    USART_SendData(USART1, StringBAUD[tx_index++]);

   

    if (tx_index >= (sizeof(StringRX) - 1))

      tx_index = 0;

    }

    

    if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // Received characters modify string

  {

    StringRX[rx_index++] = USART_ReceiveData(USART1);

   

    if (rx_index >= (sizeof(StringRX) - 1))

      rx_index = 0;

  }

    

    // ------------------StringNAME------------------

    if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET) // Transmit the string in a loop

  {

    USART_SendData(USART1, StringNAME[tx_index++]);

   

    if (tx_index >= (sizeof(StringRX) - 1))

      tx_index = 0;

  }

    

    if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // Received characters modify string

  {

    StringRX[rx_index++] = USART_ReceiveData(USART1);

   

    if (rx_index >= (sizeof(StringRX) - 1))

      rx_index = 0;

  }

    

    // ------------------StringPIN------------------

    if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET) // Transmit the string in a loop

  {

    USART_SendData(USART1, StringPIN[tx_index++]);

   

    if (tx_index >= (sizeof(StringRX) - 1))

      tx_index = 0;

  }

    

    if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // Received characters modify string

  {

    StringRX[rx_index++] = USART_ReceiveData(USART1);

   

    if (rx_index >= (sizeof(StringRX) - 1))

      rx_index = 0;

  }

}

#at-command
    This topic has been closed for replies.

    3 replies

    Tesla DeLorean
    Guru
    July 11, 2014
    Posted on July 11, 2014 at 19:13

    Perhaps this is something you could put in a subroutine?

    That pays attention to the string length/termination, and sends CR and LF character to enter the command?
    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    Tesla DeLorean
    Guru
    July 11, 2014
    Posted on July 11, 2014 at 20:03

    The more I look at this the more disturbed I am.

    The purpose of an interrupt is to do something in an instant, not stick around and loop. Doing printf/scanf under interrupt is also inadvisable.

    Consider just using an interrupt to implement a receive buffer, and transmit data in a simple subroutine.

    Re-read the chapter on ''C Strings''

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    Andrew Neil
    Super User
    July 11, 2014
    Posted on July 11, 2014 at 20:03

    ''I am not sure if I require delays to allow module to reply''

    It's just like talking to another person: You ask them a question, then you shut up and allow them to answer! Only once you have their reply, and have considered it, do you ask the next question...

    If you ask the question and then simply ignore the other person for some arbitrary period of time, the conversation is not going to go well - is it?

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.