Rx Tx PIC16F877A HC-05 Bluetooth

Discussion in 'Programmer's Corner' started by Ben_C, Feb 15, 2015.

  1. Ben_C

    Thread Starter Active Member

    Oct 19, 2010
    65
    4
    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 (Text):
    1.  
    2. void main()
    3. {
    4.     int i;
    5.     TRISB = 0x00;
    6.     PORTB = 0x00;
    7.     SPBRG = 6;                              //Baud rate formula gives 6 as BRGH is 0
    8.     TXSTA = 0b00100010;
    9.     RCSTA = 0b10010000;
    10.     RCIE = 1;                                  //Receive Interrupt enable
    11.  
    12.     do
    13.     {
    14.         TXREG++;
    15.         while (!TRMT);
    16.         while (!RCIF);
    17.         PORTB = RCREG;
    18.         for (i=0;i<300;i++);     //delay in order to view
    19.     }
    20.     while(1);
    21. }
    22.  
     
  2. Ben_C

    Thread Starter Active Member

    Oct 19, 2010
    65
    4
    Issue resolved, persistence! :rolleyes::p:cool:
     
    AlexLPD likes this.
  3. Ben_C

    Thread Starter Active Member

    Oct 19, 2010
    65
    4
    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 (Text):
    1.  
    2. 00110001 1
    3. 00110010 2
    4. 00110011 3
    5. 00110100 4
    6. 00110101 5
    7. 00110110 6
    8. 00110111 7
    9. 00111000 8
    10.  
    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 (Text):
    1.  
    2. if (RCREG == 0b00110001) //1
    3. {
    4. PORTBbits.RB7 ^= (1<<0);
    5. }
    6. else if (RCREG == 0b00110010) //2
    7. {
    8. PORTBbits.RB6 ^= (1<<0);
    9. }
    10. else if (RCREG == 0b00110011) //3
    11. {
    12. PORTBbits.RB5 ^= (1<<0);
    13. }
    14. else if (RCREG == 0b00110100) //4
    15. {
    16. PORTBbits.RB4 ^= (1<<0);
    17. }
    18. else if (RCREG == 0b00110101) //5
    19. {
    20. PORTBbits.RB3 ^= (1<<0);
    21. }
    22. else if (RCREG == 0b00110110) //6
    23. {
    24. PORTBbits.RB2 ^= (1<<0);
    25. }
    26. else if (RCREG == 0b00110111) //7
    27. {
    28. PORTBbits.RB1 ^= (1<<0);
    29. }
    30. else if (RCREG == 0b00111000) //8
    31. {
    32. PORTBbits.RB0 ^= (1<<0);
    33. }
    34.  
    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
     
Loading...