Rx Tx PIC16F877A HC-05 Bluetooth

Thread Starter

Ben_C

Joined Oct 19, 2010
65
Hi everyone, literally my last resort after doing all the research I can, I'm not getting anywhere.

Basically I want to send a byte of data from my Android phone to a bluetooth module HC-05 and then mirror the image to PORTB of the PIC16F877A where I have connected a row of LED's for visual feedback. Now, I have written the code numerous times but with no luck. What I'm getting is completely wrong for what I'm sending to the HC-05 module.

I believe I'm going wrong with the Baud rate/Clock speed. I'm using an external xtal capable of 20MHz but coded for 4MHz.
The standard output of the HC-05 is 9600 Baud and to match this on the Microcontroller I've set BRGH to 0 and SPBRG to 6 but still not getting the correct output. Even switching BRGH to 1 and setting SPBRG to 25 still gives the incorrect output! Im getting the LED's to illuminate but not from the byte of for example say "11111111" it would show "00001111" even in debug mode it's showing on RCREG window it's receiving "00001111" and then sending this to PORTB.

Has anyone got a "generic" code for this set up that they know works? Or point me in the right direction of where I'm goign wrong? I have this example code pulled from the net which should work in theory, but doesnt.

Code:
void main()
{
    int i;
    TRISB = 0x00;
    PORTB = 0x00;
    SPBRG = 6;                              //Baud rate formula gives 6 as BRGH is 0
    TXSTA = 0b00100010;
    RCSTA = 0b10010000;
    RCIE = 1;                                  //Receive Interrupt enable

    do
    {
        TXREG++;
        while (!TRMT);
        while (!RCIF);
        PORTB = RCREG;
        for (i=0;i<300;i++);     //delay in order to view
    }
    while(1);
}
 

Thread Starter

Ben_C

Joined Oct 19, 2010
65
Ive had messages asking about this so here it is:

Being a noob I was trying to send 8 bit as for example "00101011" from Bluetooth Terminal (Available to download off Play Store) ... This of course wouldn't have been sent from my phone, 1 single value is 8 bits, so just sending a value "1" is 8 bits long!

ASCII code table:

Code:
Code:
00110001 1
00110010 2
00110011 3
00110100 4
00110101 5
00110110 6
00110111 7
00111000 8
so if I wanted a single LED to turn on I had to code for this.

Note I wanted to toggle the LED's on and off ...

Code:
Code:
if (RCREG == 0b00110001) //1
{
PORTBbits.RB7 ^= (1<<0);
}
else if (RCREG == 0b00110010) //2
{
PORTBbits.RB6 ^= (1<<0);
}
else if (RCREG == 0b00110011) //3
{
PORTBbits.RB5 ^= (1<<0);
}
else if (RCREG == 0b00110100) //4
{
PORTBbits.RB4 ^= (1<<0);
}
else if (RCREG == 0b00110101) //5
{
PORTBbits.RB3 ^= (1<<0);
}
else if (RCREG == 0b00110110) //6
{
PORTBbits.RB2 ^= (1<<0);
}
else if (RCREG == 0b00110111) //7
{
PORTBbits.RB1 ^= (1<<0);
}
else if (RCREG == 0b00111000) //8
{
PORTBbits.RB0 ^= (1<<0);
}
My first LED is connected to RB7 so when I send the digit "1" from bluetooth terminal it checks if RB7 is high (on) if it is it turns it off, if it isnt high itll turn it high thus causing the LED to turn on etc...


The rest of the code is missing but I'm sure you can work it out :p
 
Top