cancel
Showing results for 
Search instead for 
Did you mean: 

Struggling with HC05 Module for AT Command

monta
Associate II
Posted on February 13, 2015 at 16:57

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 = PA2

GPIO_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 !!! 
7 REPLIES 7
Posted on February 13, 2015 at 17:04

Wait of the TXE bit to assert BEFORE you keep jamming data into it?

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
monta
Associate II
Posted on February 13, 2015 at 17:07

Hi there , thank you for responding . What do you mean by that ?? can you explain a bit.

Posted on February 13, 2015 at 17:16

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.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
monta
Associate II
Posted on February 13, 2015 at 17:25

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!!! 

monta
Associate II
Posted on February 13, 2015 at 17:43

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 :(

Posted on February 13, 2015 at 17:54

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.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
monta
Associate II
Posted on February 14, 2015 at 10:01

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 🙂