I interfaced PIC16F877A with a HC-05 Bluetooth module using serial communication and I can transmit data from the PIC to my PC. I connected my PC's Bluetooth to HC-05 Bluetooth module and in Tera Term or termite 2.9 I see the output. However, when I send data from the terminal I receive nothing on the PIC. As per my code the data received by PIC showed by echoed back by transmitting the same.
My PIC code
#include <htc.h>
__CONFIG(0x2F0A);
#define _XTAL_FREQ 16000000
#define BAUDRATE 9600
void InitUART(void)
{
BRGH = 1;
SPBRG = ((_XTAL_FREQ/16)/BAUDRATE) - 1;
TRISC6 = 0; // TX Pin
TRISC7 = 1; // RX Pin
TXSTA = 0x24;
RCSTA = 0x90;
//SPBRG = 129;
}
void SendByteSerially(unsigned char Byte) // Writes a character to the serial port
{
while(!TXIF) ; // wait for previous transmission to finish
TXREG = Byte;
}
unsigned char ReceiveByteSerially(void) // Reads a character from the serial port
{
while(!RCIF)
continue; // Wait for transmission to receive
return RCREG;
}
void SendStringSerially(const unsigned char* st)
{
while(*st)
SendByteSerially(*st++);
}
void main (void)
{
unsigned char SerialData;
unsigned int i;
TRISB=0x00;
PORTB=0xff;
InitUART(); // Intialize UART
SendStringSerially("Hello World");
while(1)
{
PORTB=0x00;
for(i=0; i<100; i++)
__delay_ms(10);
PORTB=0x01;
for(i=0; i<100; i++)
__delay_ms(10);
SerialData = ReceiveByteSerially();
SendByteSerially(SerialData);
}
}
In the while
loop I blink a LED for 1 second if any data is received.