0
\$\begingroup\$

I want to use Hc-05 bluetooth module with my pic32 controller for communicating with other bluetooth devices. I want to test the hc-05 module with my pic32 controller. I have made the hardware connections but I don't know about the code I have written a simple code to test the module.

But it is not working. The module is not responding to my AT commands. I don't know whether my code is correct or wrong. I will post my code here. Can anyone please help me?

#include <plib.h>
#include <p32xxxx.h>
/* Standard includes. */
#include <stdio.h>

#include "../hdr/Hardware_config.h"
#include "../hdr/commands.h"

#define BLT_BUF_SIZE  512

int BLT();
int i,j;
int ptr = 0;

byte BLT_BUF[BLT_BUF_SIZE];
int BLT_ptr;
char rchar;

void Serial_init()
{
  UARTConfigure(UART2, UART_ENABLE_PINS_TX_RX_ONLY);
  UARTSetLineControl(UART2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
  UARTSetDataRate(UART2, GetPeripheralClock(), 9600);//57600);//);//57600);
  UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));

  SetPriorityIntU2(UART_INT_PR1);
  EnableIntU2RX;

  INTEnableSystemMultiVectoredInt();
}

void __ISR(_UART_2_VECTOR, ipl2) IntUart2Handler(void)
{
  IFS1bits.U2RXIF = 0;
  while( DataRdyUART2())
  {
    BLT_BUF[ptr++] =  ReadUART2();
  }

  // We don't care about TX interrupt
  if ( INTGetFlag(INT_SOURCE_UART_TX(UART2)) )
  {
    INTClearFlag(INT_SOURCE_UART_TX(UART2));
  }
}

void main()
{
  SYSTEMConfig(GetSystemClock(), SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
  SYSTEMConfigPerformance(GetSystemClock());
  DDPCONbits.JTAGEN=0;

  Serial_init();

  while(1)
  {
    BLT();
  }

}

int BLT()
{
  RESET:

  memset(BLT_BUF,0,sizeof(BLT_BUF));  //BLT_ptr=0;
  putsUART2("AT\r\n");
  for(i=0;i<1000;i++)
    for(j=0;j<2000;j++);

  if(!strstr(BLT_BUF,"OK"))
  {
    dly_sec(2);
    goto RESET;
  }
  memset(BLT_BUF,0,sizeof(BLT_BUF));BLT_ptr=0;
  putsUART2("AT+ROLE=1\r\n");
  for(i=0;i<1000;i++)
    for(j=0;j<2000;j++);
  if(!strstr(BLT_BUF,"OK"));
}

Thanks & Regards M.Sivakumar

\$\endgroup\$
  • 1
    \$\begingroup\$ While working with Serial devices it is recommended that you have USB-TTL converter so that you can directly communicate with your PC/laptop. Connect HC-05 directly to your PC to see if it responds to your commands. \$\endgroup\$ – R. Hirur Feb 11 '16 at 7:31
  • \$\begingroup\$ By putting four spaces in front of each line of code, your question will be formatted correctly. There is also a code button at the top of the editor. \$\endgroup\$ – uint128_t Feb 11 '16 at 7:33
  • \$\begingroup\$ How do you know its not responding to your commands? Perhaps it responded to the first but not the second. You need some way to observe results. Use the other UART port for this purpose with your PC. Even some blinking LEDs would be helpful. \$\endgroup\$ – TisteAndii Feb 15 '16 at 17:12
  • \$\begingroup\$ I tried it with pic32 microcontroller. when I am transmitting "AT" command it is not giving "OK" response. I was checked it in buffer. \$\endgroup\$ – siva Feb 16 '16 at 8:46
  • \$\begingroup\$ It is not giving "OK" response. How do you know this? What "buffer" did you check? If its BLT_BUF, where did you print its content? \$\endgroup\$ – TisteAndii Feb 17 '16 at 19:15
1
\$\begingroup\$

BLT_BUF should be declared volatile for a start. Secondly, your delay should be before where you check for "OK". You have those for loops that do no meaningful delay (in all likeliness they will be optimised out by the compiler), then you expect the result to be there straight away when in all likeliness it might not be.

It is always helpful to be able to print out the contents of a buffer (BLT_BUF) so get some sort of usb converter for your pc or examine the contents via a debugger. You want to know if it receives anything at all, first and foremost. Check this before you loop back and erase the buffer.

\$\endgroup\$
  • \$\begingroup\$ I am using my own pic32mx695f512l board so I dont need USB to ttl converter. I can directly connect it with UART2. But I want to know whether the module is not working or I made any mistakes in my code. \$\endgroup\$ – siva Feb 17 '16 at 4:41
  • \$\begingroup\$ I have tested my module with putty and Bluetooth Terminal android app. It is transmitting wrong values. \$\endgroup\$ – siva Feb 24 '16 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.