2
\$\begingroup\$

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.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Or you can do it with one electronics.stackexchange.com/questions/94921/… \$\endgroup\$
    – Martin
    Jan 13 '14 at 6:51
  • \$\begingroup\$ i have a project and i want to send data from android application to pic16f877a i try to use your code but it doesn't send to pc , can you tell me if you make any configuration for HC-05 befor you use it , or can you help my to send data . pleas. \$\endgroup\$ Nov 25 '14 at 11:30
2
\$\begingroup\$

The HC-05 Bluetooth module runs off a 3.3V supply. If you're running your PIC off 5V then you'll need to level-shift the module's 3.3V output signal to a 5V signal input to the PIC. Figure 6 in the product guide manual:

http://www.mcu-turkey.com/wp-content/uploads/2013/01/HC-Serial-Bluetooth-Products-201104.pdf

...shows how to do that with a couple of transistors.

\$\endgroup\$
3
  • \$\begingroup\$ the HC-05 has 3.3 volt regulator, so we can wire directly to 5 volt \$\endgroup\$
    – mikmik
    Nov 1 '14 at 19:42
  • \$\begingroup\$ @makouda True, the JY-MCU board has a 3.3V regulator to power the HC-05 but the signal levels coming out of the HC-05 module itself are still relative to 3.3V - so level shifting is still required if you are communicating with a MCU running off 5V \$\endgroup\$
    – aja
    Nov 2 '14 at 3:06
  • \$\begingroup\$ tell me, but when i use two HC-05 modules to communicate between PICs, I dont need regulator?????. since I receive from another HC-05 \$\endgroup\$
    – mikmik
    Nov 2 '14 at 17:29

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.