• AVR Freaks

Hot!bluetooth module not working

Page: 12 > Showing page 1 of 2
Author
m4l1c1ou5
New Member
  • Total Posts : 22
  • Reward points : 0
  • Joined: 2019/03/26 01:03:22
  • Location: 0
  • Status: offline
2019/04/13 13:07:23 (permalink)
0

bluetooth module not working

I m trying to connect Bluetooth module HC-05 to pic16f877a but data is not transferring so I want to know whether UART of my pic chip can be faulty or my code is wrong.Thanks for helping in advance.
#1

33 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: bluetooth module not working 2019/04/13 16:05:48 (permalink)
    +2 (2)
    Do you have a logic probe or a scope to try looking at the signals?
     
    Do you know that your PIC is actually running? Have you tried flashing an LED first?
     

    Nearly there...
    #2
    pcbbc
    Super Member
    • Total Posts : 1089
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: bluetooth module not working 2019/04/13 16:55:01 (permalink)
    +3 (3)
    You have configured watchdog timer on:
    #pragma config WDTE = ON

    But as far as I can see, you are not resetting the watchdog anywhere?

    This code:
    while(*Msg)
    {
    TXREG = *Msg;
    while(!TXIF);
    while(!TRMT)continue;
    Msg++;
    }


    What is the purpose of while(!TRMT) continue? Firstly the continue is unnecessary just while(!TRMT); would be sufficient. Secondly there is no need to check TRMT at all, just TXIF to see if there is space in the buffer. Although usually you would do this before adding a character, rather than after.

    Are you sure the Bluetooth module is in transparent mode? Is it not necessary to send it some AT commands to configure it first? Sorry, not worked with this module, but usually that is the case.
    post edited by pcbbc - 2019/04/13 17:10:50
    #3
    m4l1c1ou5
    New Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2019/03/26 01:03:22
    • Location: 0
    • Status: offline
    Re: bluetooth module not working 2019/04/16 12:01:09 (permalink)
    0
    thanks for replying qhb but my pic ic is working I've used the timer in it.
    and I m using my smartphone to check bluetooth(app-bluetooth smart scanner)
    #4
    m4l1c1ou5
    New Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2019/03/26 01:03:22
    • Location: 0
    • Status: offline
    Re: bluetooth module not working 2019/04/16 12:03:19 (permalink)
    0
    thanks pcbbc, can you explain to me something more about transparent mode.
    #5
    pcbbc
    Super Member
    • Total Posts : 1089
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: bluetooth module not working 2019/04/16 17:40:15 (permalink)
    +1 (1)
    Transparent mode = NOT Command mode.
    In transparent mode any data you send the device is sent out over the Bluetooth serial interface. As opposed to command mode, where any data you send the device is treated as an AT command (to configure it) as per the manual, and so therefore not “sent” anywhere.
    In fact I do not see you sending it any AT commands at all. How is the device configured?

    Did you correct the watchdog timer issue?
    Did you fix the issues with the transmit function?

    I would start by verifying your serial port is correctly configured by enquiring the status of the module via AT command and making sure you got a reply. Only once you are sure that is working, try actually sending data over the link.
    #6
    m4l1c1ou5
    New Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2019/03/26 01:03:22
    • Location: 0
    • Status: offline
    Re: bluetooth module not working 2019/04/18 03:53:27 (permalink)
    0
    I've corrected the transmit function and I'm receiving some weird character which is having '?' inside it
    and I m not getting the correct data.
     
    thanks for helping 
     
    my new code:-----
     
     
    #include <xc.h>

    // BEGIN CONFIG
    #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
    #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
    #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
    #pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
    #pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
    #pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
    #pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
    #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
    //END CONFIG
    #define _XTAL_FREQ 16000000
    void EnableUART  ( void )
        {
            TRISC6   = 1;
            TRISC7   = 1;
            SPBRG        = 103; //BR = 9600
            SPEN        = 1;
            
            BRGH  = 1;        
        
            SYNC        = 0;        
            TX9         = 0;
            GIE        = 0;     //Disable interrupts
            TXEN        = 1;
        }

        void SendMessage ( char* Msg )
        {
            while(*Msg)
            {
                while(!TXIF);
               TXREG = 0b01;
                Msg++;
            } 
        }
        #define StatusMSG1 "Booting Begins"
        void main()
        {
            TRISB = 0x00; 
            PORTB = 0b00000111;
            EnableUART();
            SendMessage(StatusMSG1);
            while(1)
            {
            SendMessage(StatusMSG1);
            }
            
        }
     
    #7
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: bluetooth module not working 2019/04/18 04:05:52 (permalink)
    +1 (1)
    The problem is in your SendMessage() function. It is sending a value of 0x01 instead of the characters from your string.
    Here:
        void SendMessage ( char* Msg )
        {
            while(*Msg)
            {
                while(!TXIF);
               TXREG = 0b01;
                Msg++;
            } 
        }

               TXREG = 0b01;
    should be
               TXREG = *Msg;
     

    Nearly there...
    #8
    pcbbc
    Super Member
    • Total Posts : 1089
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: bluetooth module not working 2019/04/18 04:33:22 (permalink)
    +2 (2)
    Tip: Please put [CODE]tags around your code[/CODE] when posting to have your code format correctly.
    Note that you need to type "code" in lower case and not upper case as shown.
    #9
    m4l1c1ou5
    New Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2019/03/26 01:03:22
    • Location: 0
    • Status: offline
    Re: bluetooth module not working 2019/04/26 02:20:45 (permalink)
    0
    a
    post edited by m4l1c1ou5 - 2019/04/26 02:23:50
    #10
    m4l1c1ou5
    New Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2019/03/26 01:03:22
    • Location: 0
    • Status: offline
    Re: bluetooth module not working 2019/04/26 02:22:09 (permalink)
    0
    I m sorry for my mistake 
    please ignore my above post
    thanks qhb 
    but i've changed it that way "*Msg" to "0b101" inorder to check if it is working 
    and still it is not showing the data i've assigned to TXREG
    My new code:---

    #include <xc.h>
    // BEGIN CONFIG
    #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
    #pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
    #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
    #pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
    #pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
    #pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
    #pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
    #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
    //END CONFIG
    #define _XTAL_FREQ 16000000
    char UART_Init(const long int baudrate)
    {
    unsigned int x;
    x = (_XTAL_FREQ - baudrate*64)/(baudrate*64);
    if(x>255)
    {
    x = (_XTAL_FREQ - baudrate*16)/(baudrate*16);
    BRGH = 1;
    }
    if(x<256)
    {
    SPBRG = x;
    SYNC = 0;
    SPEN = 1;
    TRISC7 = 1;
    TRISC6 = 1;
    CREN = 1;
    TXEN = 1;
    return 1;
    }
    return 0;
    }
    void SendMessage ( char* Msg )
    {
    while(*Msg)
    {
    while(!TRMT && !TXIF);
    TXREG = *Msg;
    Msg++;
    }
    }
    #define StatusMSG1 "AT+BAUD6"
    void main()
    {
    // Indicator LED ON
    TRISB = 0x00; // Configure PORTB-pins as outputs
    PORTB = 0b00000111;

    //Initialise UART port for communication with Computer
    UART_Init(38400);

    //Start Sending Status message on Port 80 (UART)
    SendMessage(StatusMSG1);

    //Initialise the Display like LCD
    while(1)
    {
    SendMessage("B");
    }

    }

     
    post edited by m4l1c1ou5 - 2019/04/26 02:27:11
    #11
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: bluetooth module not working 2019/04/26 02:29:43 (permalink)
    +1 (1)
    In your SendMessage() function, you changed
    while(!TXIF);

    to
    while(!TRMT && !TXIF);

    Change it back!
     

    Nearly there...
    #12
    m4l1c1ou5
    New Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2019/03/26 01:03:22
    • Location: 0
    • Status: offline
    Re: bluetooth module not working 2019/04/26 13:09:21 (permalink)
    0
    I'VE CHANGED IT BACK BUT IT IS STILL THROWING RANDOM VALUES
    MY CODE IS

     
     
     
    #include <xc.h>

    // BEGIN CONFIG
    #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
    #pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
    #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
    #pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
    #pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
    #pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
    #pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
    #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
    //END CONFIG
    #define _XTAL_FREQ 16000000
    char UART_Init(const long int baudrate)
    {
        unsigned int x;
        x = (_XTAL_FREQ - baudrate*64)/(baudrate*64);
        if(x>255)
        {
            x = (_XTAL_FREQ - baudrate*16)/(baudrate*16);
            BRGH = 1;
        }
        if(x<256)
        {
         SPBRG = x;
         SYNC = 0;
         SPEN = 1;
         BRGH=1;
              TRISC7 = 1;
              TRISC6 = 1;
              CREN = 1;
              TXEN = 1;
         return 1;
        }
        return 0;
    }

        void SendMessage ( char* Msg )
        {
            while(*Msg)
            {
                while(!TXIF);
                TXREG = *Msg;
                Msg++;
                TXIF=1;
            }
        }
        #define StatusMSG1 "AT+BAUD7"
        void main()
        {
            // Indicator LED ON
            TRISB = 0x00; // Configure PORTB-pins as outputs
            PORTB = 0b00000111;
            
            //Initialise UART port for communication with Computer
            UART_Init(38400);
            
            //Start Sending Status message on Port 80 (UART)
            SendMessage(StatusMSG1);
            
            //Initialise the Display like LCD         

            while(1)
            {
            SendMessage("G");
            }
            
        }
       

    output coming is as follows:
    2 2 2 2 2  2 "NULL value" 2 2 ...........
    (in ASCII format)
     
    post edited by m4l1c1ou5 - 2019/04/26 13:11:49
    #13
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: bluetooth module not working 2019/04/26 14:43:46 (permalink)
    +2 (2)
    I just re-checked your baudrate calculation. Did you write that, or borrow it from somewhere?
     
    It will be selecting BRGH=0 and SPBRG = 5, which gives a real baud rate of 41,666, which is an 8.5% error.
    If it had selected BRGH=1 and SPBRG = 25, then the real baud rate would have been 38, 461 which is only 0.2% error.
    So, your calculation is backwards, it should be testing the BRGH=1 option first, and only switch to BRGH=0 if the calculated SPBRG value is > 255.
    Personally, I don't recommend doing the calculation on the fly, as like in this case, you don't notice massive errors.
    Work it out yourself and hard code it into the program.
     
     

    Nearly there...
    #14
    m4l1c1ou5
    New Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2019/03/26 01:03:22
    • Location: 0
    • Status: offline
    Re: bluetooth module not working 2019/04/28 11:17:29 (permalink)
    0
    but I want my baud rate = 38400 so if I will not do the calculation in the program then I will not be able to assign SPBRG an integer value
    so what should I do?
    and I don't understand the "error" thing so please explain it 
    post edited by m4l1c1ou5 - 2019/04/28 11:18:49
    #15
    pcbbc
    Super Member
    • Total Posts : 1089
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: bluetooth module not working 2019/04/28 11:51:22 (permalink)
    +1 (1)
    Most people just do the calculation manually using pencil and paper (or a calculator) the code the resulting value directly to SPBRG assuming their chosen Fosc. In your case 16MHz. So:
    BRGH=1;
    SPBRG = 25;

    Having this calculation in code is very wasteful on an 8 bit device with limited RAM and ROM as you need a fair chunk of the 32-bit integer math library, which otherwise probably isn’t required elsewhere. If you want to “future proof” your program against changes in crystal frequency, put a “#define SPBRGVAL 25” right next to the one for _XTAL_FREQ, comment accordingly, then use “SPBRG = SPBRGVAL;” to set it.

    There is an error because the value assigned to SPBRG is expressed as an integer number of CPU crystal cycles per serial bit. Unfortunately as your 16MHz/n (where n is 16 or 64 depending on BRGH setting) isn’t exactly divisible by 38400 you need to choose the nearest integer value. That means the actual baud rate is slightly faster (if you round down) or slower (if you round up) the BRGH value. The error is potentially worse with faster baud rates and hence lower SPBRG values.

    Usually this doesn’t matter if you select the correct value for BRGH. Normally you should set BRGH=1 (and specify SPBRG in units of 16 x crystal frequency). Then only switch to setting BRGH=0 if the value you would need to program to SPBRG is greater than 255. Unfortunately you have the logic for that round the wrong way, as explained by qhb.
    #16
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: bluetooth module not working 2019/04/28 13:35:20 (permalink)
    +1 (1)
    Just confirming what pcbbc posted.
    m4l1c1ou5
    but I want my baud rate = 38400 so if I will not do the calculation in the program then I will not be able to assign SPBRG an integer value

    It sounds like you think that doing it as a calculation is getting you a fractional value that gives you an exact baud rate. This is wrong, SPBRG can only ever be an integer value, from 0 to 255.
     

    and I don't understand the "error" thing so please explain it

    The "error" is how much the ACTUAL baud rate you have set differs from the baud rate you want.
    Do the calculations yourself by hand (using the formulas in the datasheet) and you will see why with a 16MHz clock, it's impossible to get the EXACT baud rate for most standard speeds if SPBRG must be an integer.
    RS232 can tolerate an error of up to about 3% before it will cause problems.
     

    Nearly there...
    #17
    m4l1c1ou5
    New Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2019/03/26 01:03:22
    • Location: 0
    • Status: offline
    Re: bluetooth module not working 2019/05/09 04:26:26 (permalink)
    0
    I've done all those things you've mentioned but it was still not working, so i tried to do transmission first so i've written this code
    but transmission is also not working

    #include <xc.h>
    // BEGIN CONFIG
    #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
    #pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
    #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
    #pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
    #pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
    #pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
    #pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
    #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
    //END CONFIG
    #define _XTAL_FREQ 16000000
    char UART_Init()
    {

    SPBRG = 23;
    SYNC = 0;
    SPEN = 1;
    CREN = 1;
    TRISC7 = 1;
    TRISC6 = 1;
    BRGH=1;
    return 0;
    }
    void recieveMessage ()
    {
    if(OERR){
    CREN=0;
    CREN=1;
    }
    if(RCIF==1){
    while(!RCIF);
    while(OERR){
    CREN=0;
    CREN=1;
    }
    char x=RCREG;
    if(x=='a'){
    RB3=1;
    RB1=0;
    }
    else{
    RB3=0;
    RB1=1;
    }
    RB5=1;
    __delay_ms(50);
    RB5=0;
    }
    }
    void main()
    {
    // Indicator LED ON
    TRISB = 0x00;
    RB5=0;// Configure PORTB-pins as output
    //InitialiZe UART port for communication with Computer
    UART_Init();

    //Start Sending Status message on Port 80 (UART)

    //Initialise the Display like LCD
    while(1)
    {
    recieveMessage ();
    RCIF=0;
    }

    }

    everytime only the else part is working i.e. RB1=1 and RB3=0
     
    #18
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: bluetooth module not working 2019/05/09 04:42:08 (permalink)
    0
    Why are you loading SPBRG with 23, when pcbbc specifically told you to use 25?
    23 will give a baud rate of 16MHz /(16 * (23+1)) = 416666 baud
    25 would give 16MHz /(16 * (25+1)) = 38461
     
    Why are you enabling the WDT, after he specifically told you this is another problem?
     
    This horrible bit of code in your recieveMessage() function almost certainly is not doing what you intended.
    How about adding some comment to illustrate what you think each bit is doing.
      if(RCIF==1){
        while(!RCIF);
        while(OERR){
          CREN=0;
          CREN=1;
        }

     
     

    Nearly there...
    #19
    m4l1c1ou5
    New Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2019/03/26 01:03:22
    • Location: 0
    • Status: offline
    Re: bluetooth module not working 2019/05/11 10:39:15 (permalink)
    0
    Now,I've done each and everything as told but still not working fine i.e. PIC is receiving garbage value

     
    #include <xc.h>
     
    // BEGIN CONFIG
    #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
    #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
    #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
    #pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
    #pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
    #pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
    #pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
    #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
    //END CONFIG
    #define _XTAL_FREQ 16000000
    char UART_Init()
    {

    SPBRG = 25;
    SYNC = 0;
    SPEN = 1;
    CREN = 1;
    TRISC7 = 1;
    TRISC6 = 1;
    BRGH=1;
    return 0;
    }
     
    void recieveMessage ()
    {
    if(OERR){ // IF ERROR DETECTED RESETTING
    CREN=0; //THE CREN BIT
    CREN=1;
    }
    if(RCIF==1){
    while(!RCIF);
    char x=RCREG;
    if(x=='a'){
    RB3=1;
    RB1=0;
    }
    else{
    RB3=0;
    RB1=1;
    }
    RB5=1;
    __delay_ms(50);
    RB5=0;
    }
    }
    void main()
    {
    // Indicator LED ON
    TRISB = 0x00;
    RB5=0;// Configure PORTB-pins as output
    //InitialiZe UART port for communication with Computer
    UART_Init();

    //Start Sending Status message on Port 80 (UART)

    //Initialise the Display like LCD
     
    while(1)
    {
    recieveMessage ();
    RCIF=0;
    }

    }
     

    post edited by m4l1c1ou5 - 2019/05/11 10:40:41
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2019 APG vNext Commercial Version 4.5