2014-07-11 10:01 AM
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-command2014-07-11 10:13 AM
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?2014-07-11 11:03 AM
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''2014-07-11 11:03 AM
''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?