2

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.

Nick T
  • 12,360
  • 2
  • 44
  • 71
user30435
  • 21
  • 1
  • 2
  • 1
    Or you can do it with one http://electronics.stackexchange.com/questions/94921/pulse-voltage-drops-trying-to-connect-2-devices/94927#94927 – Martin Jan 13 '14 at 06:51
  • 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. – Ghadeer Joma Nov 25 '14 at 11:30

1 Answers1

2

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.

aja
  • 298
  • 2
  • 5
  • the HC-05 has 3.3 volt regulator, so we can wire directly to 5 volt – bouqbouq Nov 01 '14 at 19:42
  • @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 – aja Nov 02 '14 at 03:06
  • 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 – bouqbouq Nov 02 '14 at 17:29