cancel
Showing results for 
Search instead for 
Did you mean: 

HC-06 Bluetooth Module Configure with UART

kyle
Associate
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
3 REPLIES 3
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 Venmo
Up vote any posts that you find helpful, it shows what's working..
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 Venmo
Up vote any posts that you find helpful, it shows what's working..
Andrew Neil
Evangelist
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?