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