Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Junk AT Response. HC-05 Bluetooth M. with PIC1826K22

Status
Not open for further replies.

rishiraj

Junior Member level 1
Junior Member level 1
Joined
Dec 25, 2012
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,393
Hi,
I am trying to interface the HC-05 module with a bootloaded PIC1826K22. I connected USART1 with the module and USART2 is connected to my laptop via USART to Serial converter IC. Below is my code.

The problem is, I get garbage value in return when I type the AT command in hterm with CR-LN. My USART1 is configured at 38400 baud rate (defualt for HC05) and USART2 at 9600.

My HC-05 is working fine and it is indeed in 38400 baud rate because I checked the same in Arduino and it worked fine. In Arduino. Kindly throw some light

Code:
#include <stdio.h>
#include <stdlib.h>
#include <p18f26k22.h>
#include "oscillator.h"
void main(void)
{
    set_freq(f8Mhz);
    //########### EUART 1 CONFIG #########
    TXSTA1bits.TXEN=1;      //Transmitter Enabled
    TXSTA1bits.SYNC=0;      //Asynchronous Mode
    TXSTA1bits.BRGH=1;      //x16 Multiplier in Use
    BAUDCON1bits.BRG16=0;   // Set Low BRG16
    SPBRG1=12;              //Set the multiplier for 38400 baudrate
    RCSTA1bits.CREN=1;      //Serial Port Enabled
    RCSTA1bits.SPEN=1;      //Serial Port Enabled
    ANSELCbits.ANSC6=0;     //Disbale Analog
    ANSELCbits.ANSC7=0;     //Disbale Analog
    TRISCbits.TRISC7=1;     //For EUART1 (NOTE THIS !!!!) RX
    TRISCbits.TRISC6=1;      //For EUART1 TX

    //########### EUART 2 CONFIG #########

    TXSTA2bits.TXEN=1;      //Transmitter Enabled
    TXSTA2bits.SYNC=0;      //Asynchronous Mode
    TXSTA2bits.BRGH=1;      //x16 Multiplier in Use
    BAUDCON2bits.BRG16=0;   // Set Low BRG16
    SPBRG2=51;              //Set the multiplier for 9600 baudrate
    RCSTA2bits.CREN=1;      //Serial Port Enabled
    RCSTA2bits.SPEN=1;      //Serial Port Enabled
    TRISBbits.TRISB7=1;     //For EUART2 (NOTE THIS !!!!) RX
    TRISBbits.TRISB6=1;     //For EUART2 TX

    while(1)
    {
        if(PIR3bits.RC2IF)
        {
        TXREG1=RCREG2;
        PIR3bits.RC2IF=0;
        }
        if(PIR1bits.RC1IF)
        {
        TXREG2=RCREG1;
        PIR1bits.RC1IF=0;
        }
        
        
    }
}
 

it sounds as though you have the USARTs configured incorrectly, e.g. baud rate divisors, stop bit or data bit settings.
 

The HC05 datasheet mentions that it takes 8 Data Bit and 1 Stop bit. Now do I need to assign the TXSTAx.TX9D register to 1 for the stop bit in the above code ?
 

what I think is happening is that the HC-05 module is sending data to USART1 at 38400baud which you are then transmitting to the PC at 9600baud. unless the amount of data is small or USART1 is using hardware handshaking you will be loosing characters, i.e. characters are arriving faster at USART1 than you can transmit them via USART2.

alternatives
1. can you increase the speed of USART2 and your PC to 38400 or greater
2. as characters arrive in USART1 put them into a buffer which can be emptied by USART2. I would use a ring buffer for thus with USART2 receive interrupts enabled
 

Well, I considered all that you have said. I changed both the baud rate to 38400 but still no response to AT command.
Here is my new code

Code:
#include <stdio.h>
#include <stdlib.h>
#include <p18f26k22.h>
#include "oscillator.h"
void transmitData(void);
void receiveData(void);
void main(void)
{
    set_freq(f8Mhz);

    //########### EUART 1 CONFIG #########

    TXSTA1bits.TXEN=1;      //Transmitter Enabled
    TXSTA1bits.SYNC=0;      //Asynchronous Mode
    TXSTA1bits.BRGH=1;      //x16 Multiplier in Use
    TXSTA1bits.TRMT=1;
    BAUDCON1bits.BRG16=0;   // Set Low BRG16
    SPBRG1=12;              //Set the multiplier for 38400 baudrate
    RCSTA1bits.CREN=1;      //Serial Port Enabled
    RCSTA1bits.SPEN=1;      //Serial Port Enabled
    ANSELCbits.ANSC6=0;     //Disbale Analog
    ANSELCbits.ANSC7=0;     //Disbale Analog
    TRISCbits.TRISC7=1;     //For EUART1 (NOTE THIS !!!!) RX
    TRISCbits.TRISC6=1;      //For EUART1 TX

    //########### EUART 2 CONFIG #########
    
    TXSTA2bits.TXEN=1;      //Transmitter Enabled
    TXSTA2bits.SYNC=0;      //Asynchronous Mode
    TXSTA2bits.BRGH=1;      //x16 Multiplier in Use
    TXSTA2bits.TRMT=1;
    BAUDCON2bits.BRG16=0;   // Set Low BRG16
    SPBRG2=12;              //Set the multiplier for 38400 baudrate
    RCSTA2bits.CREN=1;      //Serial Port Enabled
    RCSTA2bits.SPEN=1;      //Serial Port Enabled
    TRISBbits.TRISB7=1;     //For EUART2 (NOTE THIS !!!!) RX
    TRISBbits.TRISB6=1;     //For EUART2 TX
    while(1)
    {
        transmitData();
        receiveData();
    }   
}
 void transmitData()
 {
     TXREG1=RCREG2;
     while(!(TXSTA1bits.TRMT));
 }
 void receiveData()
 {
     while(!(PIR1bits.RC1IF));   //Rx from HC05
     TXREG2=RCREG1;
 }
 

in your loop test if a character has been received - if so then transmit
Code:
    while(1)
    {
    if((PIR1bits.RC1IF)) TXREG2=RCREG1;   //Rx from HC05
    if(  ...  similar for other USART
    }
 

hello,

Is the led blinking on your HC05 ?
if not, no chance to have AT command...
I know that it is very difficult to put HC05 or HC06 bluetooth device in AT mode
when Power ON.. sometimes they are stuck..
Even using KEY input .. or the (not direct accessible) Reset input.

Try before AT commands with a
TERMINAL <- RS232 level converter --> HC05
Do power off and power on, and test again if HC05 gives answer ....

use factory speed if never respond.. maybe need a true RESET

HC05_reset_pin.jpg
 

in your loop test if a character has been received - if so then transmit
Code:
    while(1)
    {
    if((PIR1bits.RC1IF)) TXREG2=RCREG1;   //Rx from HC05
    if(  ...  similar for other USART
    }

My last code did not work in loop back test. But the below one worked well in loop back test. Both the USART are at 38400 baud rate but when connected to HC05 it returns garbage value.

Code:
while(1)
    {
     while(!(PIR3bits.RC2IF)); 
     TXREG1=RCREG2;
     
     
     while(!(PIR1bits.RC1IF));  
     TXREG2=RCREG1;
     }

- - - Updated - - -

hello,

Is the led blinking on your HC05 ?
if not, no chance to have AT command...
I know that it is very difficult to put HC05 or HC06 bluetooth device in AT mode
when Power ON.. sometimes they are stuck..
Even using KEY input .. or the (not direct accessible) Reset input.

Try before AT commands with a
TERMINAL <- RS232 level converter --> HC05
Do power off and power on, and test again if HC05 gives answer ....

use factory speed if never respond.. maybe need a true RESET

View attachment 105032

My HC05 is in AT command mode. The LED blinks at 2s interval which tells me that it is at AT command mode. I achieved it by making the key pin HIGH first and then the Vcc pin
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top