Need Help With Bluetooth Module HC-06

Submitted by Khoi on

I have learned from this tutorial https://circuitdigest.com/microcontroller-projects/bluetooth-interfacin… I did test the code that It worked perfectly with my smartphone. However, when I try to display 1 string to both LCD and Smartphone, It stopped when It came to Initialize_Bluetooth() function. Please help me, here is my code

void Initialize_Bluetooth()
{
   //Set the pins of RX and TX//
    TRISC6=1;
    TRISC7=1;
    
  //Set the baud rate using the look up table in datasheet(pg114)//
    BRGH=1;      //Always use high speed baud rate with Bluetooth else it wont work
    SPBRG  =129;
    
    //Turn on Asyc. Serial Port//
    SYNC=0;
    SPEN=1;
    
    //Set 8-bit reception and transmission
    RX9=0;
    TX9=0;

   //Enable transmission and reception//
    TXEN=1; 
    CREN=1; 
    
    //Enable global and ph. interrupts//
    GIE = 1;
    PEIE= 1;
  
    //Enable interrupts for Tx. and Rx.//
    RCIE=1;
    TXIE=1;
}
//___________BT initialized_____________//

//Function to load the Bluetooth Rx. buffer with one char.//
void BT_load_char(char byte)  
{
    TXREG = byte;
    while(!TXIF);  
    while(!TRMT);
}
//End of function//

//Function to Load Bluetooth Rx. buffer with string//
void BT_load_string(char* string)
{
    while(*string)
    BT_load_char(*string++);
}
//End of function//

//Function to broadcast data from RX. buffer//
void broadcast_BT()
{
  TXREG = 13;  
  __delay_ms(500);
}
//End of function//

//Function to get a char from Rx.buffer of BT//
char BT_get_char(void)   
{
    if(OERR) // check for over run error 
    {
        CREN = 0;
        CREN = 1; //Reset CREN
    }
    
    if(RCIF==1) //if the user has sent a char return the char (ASCII value)
    {
    while(!RCIF);  
    return RCREG;
    }
    else //if user has sent no message return 0
        return 0;
}
//End of function/

//MY MAIN

void main(){

    unsigned char chuoi1[10],chuoi2[10];

    float speed;
    TRISD=0b00000000;//LCD port
    TRISB0=1;//INT0
    OPTION_REG=0b01000100;//INT0
    TMR0=100;
    TMR0IE=1;
    GIE=1;
    PEIE=1;
    INTE=1;
    RCIE=1;
    TXIE=1;
    __delay_ms(200);
    init_LCD();
    speed=60;
    LCD_Clear();
    Initialize_Bluetooth();
            
    //display on LCD and smartphone

    while(1){
        sprintf(string1,"%2.2f",speed);
        LCD_WriteText(1,1,"Speed(km/h):");
        LCD_Puts(string1);

        BT_load_string(string1);
        broadcast_BT();
    }
}

I want to display "speed" on both LCD and Smartphone at the same time,  B.Aswinth Raj do you have an idea :D

  Joined April 24, 2018      5
Tuesday at 02:18 PM

I figured it out that the interrupt ISR was the culprit behind when I try to combine the speedometer (your tutorial) with bluetooth display. But I still don't know why interrupt service can cause this problem. All I want to do is display speed both on LCD and smartphone

  Joined April 24, 2018      5
Tuesday at 02:18 PM

Which two projects are you exactly trying to combine? Once common problem that might occur with ISR is that if your connection is not correct then you PIC will receive continues Interrupts and while being busy serving these interrupts, the PIC will not be able execute any other code in your program. 

  Joined August 16, 2016      1000
Tuesday at 12:29 AM

the speedometer project (https://circuitdigest.com/microcontroller-projects/digital-speedometer-…) and bluetooth project (https://circuitdigest.com/microcontroller-projects/bluetooth-interfacin…). I figured it out that when I disabled usart interrupt, the project worked fine. Now I really want to send a 1byte number to smartphone but I keep receiving number 13 on smartphone

 I supposed that because of this code line: 

void broadcast_BT()
{
  TXREG = 13;  
  __delay_ms(500);
}

 

Is there anyway to send one byte number, what type of data, I have already tried unsigned int but it only showed 13.

  Joined April 24, 2018      5
Tuesday at 02:18 PM

What ever you load in the TXREG will be sent via bluetooth. So the data you want to send should be loaded in this Tx buffer. 

Convert the 1 byte information to decimal and load it in the Tx register then send it. It should work!! What problem are you facing ?

  Joined August 16, 2016      1000
Tuesday at 12:29 AM

I understand that however, the receiver (mobile app) also regconizes the "13" ( carriage return). My solution is sending single byte, so I can get the byte value I want, then put 13 value in an useless variable, then get another usable byte,....

  Joined April 24, 2018      5
Tuesday at 02:18 PM