2015-02-13 07:57 AM
I'm getting some hard time trying to make my HC05 to execute an AT command from my stm32f4 , first it seemed that the problem was that the module was not in AT mode, now that i got it to enter AT mode it still is not responding , here is my code:
#include
<stm32f4xx.h>
#include
<stm32f4xx_usart.h>
#include
<stm32f4xx_gpio.h>
#include
<stm32f4xx_rcc.h>
void init_usart(void){
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;/* enable peripheral clock for USART2 */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
/* GPIOA clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
/* GPIOA Configuration: USART2 TX on PA2 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect USART2 pins to AF2 */
// TX = PA2GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
USART_InitStructure.USART_BaudRate = 38400;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;USART_InitStructure.USART_StopBits = USART_StopBits_1;USART_InitStructure.USART_Parity = USART_Parity_No;USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;USART_InitStructure.USART_Mode = USART_Mode_Tx;USART_Init(USART2, &USART_InitStructure);USART_Cmd(USART2, ENABLE); // enable USART2
}
int main(void){
init_usart();
USART_SendData(USART2, 'A');
USART_SendData(USART2, 'T'); USART_SendData(USART2, '+'); USART_SendData(USART2, 'P'); USART_SendData(USART2, 'I'); USART_SendData(USART2, 'N'); USART_SendData(USART2, '='); USART_SendData(USART2, '1'); USART_SendData(USART2, '7'); USART_SendData(USART2, '8'); USART_SendData(USART2, '9');}
I tried with Both 9600 and 38400 for the baud rate but nothing works. Anyone can point me to the right direction?
Thank you !!!
2015-02-13 08:04 AM
Wait of the TXE bit to assert BEFORE you keep jamming data into it?
2015-02-13 08:07 AM
Hi there , thank you for responding . What do you mean by that ?? can you explain a bit.
2015-02-13 08:16 AM
TXE - Transmit Empty
The Data Register can hold ONE byte of data at a time, it will take several microseconds to shift it via the serial port, the processors is running in cycles in the tens-of-nanoseconds. You can't simply keep cramming data in the register and expect it to function properly.while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART2, 0x49); // Send 'I'
You also don't want to drop out the bottom of main(), nothing good can happen.
2015-02-13 08:25 AM
I see, that seems true. I'll go a head and try to edit my code and see what happens. Thank you for your quick response, you saved my day!!!
2015-02-13 08:43 AM
Hi there, I changed my code to
int main(void){ init_usart(); Delay(0x3FFFFF); USART_SendData(USART2, 'A'); // defined in stm32f4xx_usart.h while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty USART_SendData(USART2, 'T'); // defined in stm32f4xx_usart.h while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty USART_SendData(USART2, '+'); // defined in stm32f4xx_usart.h while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty USART_SendData(USART2, 'P'); // defined in stm32f4xx_usart.h while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty USART_SendData(USART2, 'I'); // defined in stm32f4xx_usart.h while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty USART_SendData(USART2, 'N'); // defined in stm32f4xx_usart.h while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty USART_SendData(USART2, '='); // defined in stm32f4xx_usart.h while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty USART_SendData(USART2, '1'); // defined in stm32f4xx_usart.h while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty USART_SendData(USART2, '7'); // defined in stm32f4xx_usart.h while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty USART_SendData(USART2, '8'); // defined in stm32f4xx_usart.h while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty USART_SendData(USART2, '9'); // defined in stm32f4xx_usart.h while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty USART_SendData(USART2, 0x5c); // defined in stm32f4xx_usart.h while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty USART_SendData(USART2, 0x72); // defined in stm32f4xx_usart.h while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty USART_SendData(USART2, 0x5c); // defined in stm32f4xx_usart.h while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty USART_SendData(USART2, 0x6E); // defined in stm32f4xx_usart.h while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty return 0;}still not working :(2015-02-13 08:54 AM
I think you've misinterpreted the word ''BEFORE''.
This isn't your issue though, I suspect the device is expecting a <CR><LF> (Carriage Return, Line Feed) pair to close out the command. You might want to review your documentation, and perhaps confirm signalling on the pins, and the device's response to you.2015-02-14 01:01 AM
Hi there, It finally worked. I added
USART_SendData(USART2, 0x0D);
at the very end, changed the AT command to AT+PSWD=xxx instead of AT+PIN set the baud rate to 38400 and worked !! Thank you for assisting :)