• AVR Freaks

Helpful ReplyHot!I need some help using the HC-05 bluetooth module with the PIC16F18446 microcontroller.

Page: 12 > Showing page 1 of 2
Author
nashsth
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2020/03/17 21:13:03
  • Location: 0
  • Status: offline
2020/05/28 08:12:32 (permalink)
0

I need some help using the HC-05 bluetooth module with the PIC16F18446 microcontroller.

Hey all,

I'm doing a simple project that uses the HC-05 module with a curiosity development
board that uses the PIC16F18446 microcontroller. From what I have gathered, the
HC-05 uses UART to transmit/receive data so all I really have to do is to set up
the USART module properly. I have written the code, but whenever I try to build the project, MPLAB gives me an error. I am using MPLAB X IDE v5.40 and the XC8 compiler v2.20. First, here's the code:

[code]

[\code]

Now, the error that MPLAB is giving me is that it is not recognizing the "SYNC" identifier. Instead, it asks me if I mean "ASYNC". However, in the datasheet for the PIC16F18446, you can see that in order to operate the USART module in asynchronous mode, I need to clear the SYNC bit: SYNC = 0.

I am unsure why MPLAB is not recognizing this identifier. Would you help me in this and also look over my code and tell me if anything is wrong/needs improvement?

Thanks,
Nash
post edited by nashsth - 2020/05/28 08:18:50
#1
nashsth
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2020/03/17 21:13:03
  • Location: 0
  • Status: offline
Re: I need some help using the HC-05 bluetooth module with the PIC16F18446 microcontroller 2020/05/28 08:15:27 (permalink)
+1 (1)
Okay so microchip forums is being frustrating -- I tried to post this question several times before but I kept getting "Access denied" error where it says that I can't post from this server. I then removed my code and it works but when I try to edit my original post to include the code, I just get a blank screen after I hit "save changes". So, I'll upload a notepad file containing the C code.
#2
katela
Super Member
  • Total Posts : 1808
  • Reward points : 0
  • Joined: 2013/06/11 05:25:18
  • Location: South Africa
  • Status: offline
Re: I need some help using the HC-05 bluetooth module with the PIC16F18446 microcontroller 2020/05/28 08:15:27 (permalink)
0
Your code was not attached. You can post your code again.

Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
#3
nashsth
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2020/03/17 21:13:03
  • Location: 0
  • Status: offline
Re: I need some help using the HC-05 bluetooth module with the PIC16F18446 microcontroller 2020/05/28 08:17:33 (permalink)
+1 (1)
Yes microchip was being annoying and wouldn't let me post the code. I edited my original post and uploaded the code in a .txt file. Please check it out! Thank you!
#4
PStechPaul
Super Member
  • Total Posts : 2720
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: offline
Re: I need some help using the HC-05 bluetooth module with the PIC16F18446 microcontroller 2020/05/28 11:30:58 (permalink)
+2 (2)
Microchip clearly does not care about this forum or those who must use it as a first line of help. The major customers who purchase product in the megabuck range have FAEs at their beck and call.
 
I don't know why SYNC is failing, but you might want to try entering TX1STAbits.SYNC. As you type it you may see an autocomplete list of valid bits. Sometimes it may be defined differently from the datasheet, perhaps NSYNC or NASYNC or something.
 
Your code:
    char user_val[8];  // This should be a string (array of char)
    TRISC2 = 0; //set this GPIO pin as output; connect an LED to it
    
    initialize_bluetooth();
    send_str("Press 1 to turn on LED");
    send_str("Press 0 to turn off LED");
    while (1){
        user_val = receive_str();
        
        if (user_val[0] == '1'){  //Check first char in string and compare to a character
            TRISC2 = 1;
            send_str("LED on");
        }
        
        if (user_val[0] == '0'){
            TRISC2 = 0;
            send_str("LED off");    
       
        }
    }    


 
#5
ric
Super Member
  • Total Posts : 27595
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: I need some help using the HC-05 bluetooth module with the PIC16F18446 microcontroller 2020/05/28 13:11:24 (permalink)
+2 (2)
Note here
    SYNC = 0;
    TXEN = 1;
    SPEN = 1;
    CREN = 1;

None of those are real bit names, they are just shortcuts that are sometimes defined in the device header file. Plainly they didn't bother to include SYNC in the list of shortcuts.
The real names are:
TX1STAbits.SYNC
TX1STAbits.TXEN
RC1STAbits.SPEN
RC1STAbits.CREN

If you want to use just "SYNC", you can just add this to the start of your C file
#define SYNC TX1STAbits.SYNC

 
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#6
nashsth
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2020/03/17 21:13:03
  • Location: 0
  • Status: offline
Re: I need some help using the HC-05 bluetooth module with the PIC16F18446 microcontroller 2020/05/29 07:13:19 (permalink)
0
Thanks for your responses guys. Although MPLAB isn't giving me the build error after I use TX1STAbits.SYNC, my code isn't working when I attempt to use the android app "Bluetooth Terminal" to communicate with my setup. At the moment, all I'm trying to do is to send the letter "H".
 
I should see it appear in the terminal on my phone however it isn't showing up. I confirmed that the HC-05 module is paired up with my phone and that the TX on the HC-05 is connected to the RX on the pic chip, and the RX on the HC-05 is connected to the TX on the pic chip. 
 
One thing I thought might have been the issue is that I might have used the wrong pins on the pic chip for the RX and TX. In the datasheet for the pic16f18446 under the "pin diagrams" section, it doesn't specify which pins are used for TX and RX (here is a link to the datasheet). So I thought that maybe I'm using the wrong pins. I'm currently setting up pins RB5 and RB7 as RX and TX, respectively. I got this by using the MCC pin manager. I have linked a screenshot of the package view.
 
Other than that, I'm not sure why my code isn't working as expected. Could you help on this?
 
Thank you,
Nash
 
 
#7
ric
Super Member
  • Total Posts : 27595
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: I need some help using the HC-05 bluetooth module with the PIC16F18446 microcontroller 2020/05/29 14:59:55 (permalink) ☄ Helpfulby nashsth 2020/05/31 19:15:42
0
nashsth
One thing I thought might have been the issue is that I might have used the wrong pins on the pic chip for the RX and TX. In the datasheet for the pic16f18446 under the "pin diagrams" section, it doesn't specify which pins are used for TX and RX (here is a link to the datasheet). So I thought that maybe I'm using the wrong pins. I'm currently setting up pins RB5 and RB7 as RX and TX, respectively. I got this by using the MCC pin manager.

Your PIC has "PPS" so it can place those functions on almost any pin. There are no defaults.
MCC is generating the necessary code to do this in the pin initialisation.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#8
nashsth
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2020/03/17 21:13:03
  • Location: 0
  • Status: offline
Re: I need some help using the HC-05 bluetooth module with the PIC16F18446 microcontroller 2020/05/29 17:15:24 (permalink)
0
Would you have any idea why my code isn't working in that case? I am baffled and unsure how to continue :/
#9
PStechPaul
Super Member
  • Total Posts : 2720
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: offline
Re: I need some help using the HC-05 bluetooth module with the PIC16F18446 microcontroller 2020/05/29 23:13:42 (permalink)
+1 (1)
I'm not sure how the Android terminal is set up, but in Windows and Teraterm, the HC-05 shows up as two COM ports, but only one works for Bluetooth comms. The other port might be used to set various parameters for the Bluetooth module using an AT command set. But that may be before it is paired, and it might need to be done from the PIC side which is a hardware connection to its USART. Perhaps the additional port is for the Bluetooth hardware in the computer. I think the Bluetooth module automatically configures itself for the baud rate, but I have always used the fairly standard default of 9600:N,8,1.
 
More info on the HC-05: http://www.martyncurrey.c...tooth-2-0-edr-modules/
post edited by PStechPaul - 2020/05/29 23:27:37

 
#10
nashsth
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2020/03/17 21:13:03
  • Location: 0
  • Status: offline
Re: I need some help using the HC-05 bluetooth module with the PIC16F18446 microcontroller 2020/05/30 13:21:26 (permalink)
0
I am also using a baudrate of 9600 (I'm not using the AT command mode; just the communication mode as per your link). I don't think it's a hardware issue; it's my coding I think. I wrote a sample program such that I will send the letter "H" to my phone's bluetooth terminal, and after doing so, an LED will start blinking. The code is something like (in the main function):
 
send_str("H");
while(1){
    RC2 = 0;
    __delay_ms(500);
    RC2 = 1;
    __delay_ms(500);
}
 
the LED starts blinking but "H" doesn't appear in the terminal.
 
EDIT:
I've also tried enabling interrupts using the GIE, PEIE bits but enabling them doesn't do anything. I thought that since I'm using the interrupt flags, I might have to enable the interrupts in the first place, but the datasheet says that the interrupt flags are set under certain circumstances regardless of whether the interrupts are enabled or not. In that case, there isn't a need for me to enable the GIE and PEIE bits. I just thought it was worth a try.
 
EDIT2:
I also realized that I used the wrong equation to calculate the value for the SP1BRG bit. It turns out that because I set BRGH = 1, the formula I should be using is found on table 36-1 on page 577. The new value I calculate is 103. Previously, it was 51. However, this modification hasn't solved my problem either.
post edited by nashsth - 2020/05/30 14:50:12
#11
ric
Super Member
  • Total Posts : 27595
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: I need some help using the HC-05 bluetooth module with the PIC16F18446 microcontroller 2020/05/30 15:18:00 (permalink)
+3 (3)
nashsth
I've also tried enabling interrupts using the GIE, PEIE bits but enabling them doesn't do anything.

Don't.
All that will do is make your code crash if you don't have an interrupt service routine.

I thought that since I'm using the interrupt flags, I might have to enable the interrupts in the first place, but the datasheet says that the interrupt flags are set under certain circumstances regardless of whether the interrupts are enabled or not.

Trust the datasheet.

In that case, there isn't a need for me to enable the GIE and PEIE bits. I just thought it was worth a try.

Sure, try everything, but in this case that is not your problem.
 

EDIT2:
I also realized that I used the wrong equation to calculate the value for the SP1BRG bit. It turns out that because I set BRGH = 1, the formula I should be using is found on table 36-1 on page 577. The new value I calculate is 103. Previously, it was 51. However, this modification hasn't solved my problem either.

I would always double check my baud setting by transmitting 0x55 over and over, and observing the Tx pin with a scope to see what speed I was really getting.
 
No, I don't know what your problem is. Just pointing out any problems with what you are trying.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#12
upand_at_them
Super Member
  • Total Posts : 521
  • Reward points : 0
  • Joined: 2005/05/16 07:02:38
  • Location: Pennsylvania
  • Status: offline
Re: I need some help using the HC-05 bluetooth module with the PIC16F18446 microcontroller 2020/05/30 16:46:35 (permalink)
0
nashsth
code from post #1:
void initialize_bluetooth(){
//set the TX and RX pins. According to the MCC pin layout, we have:
TRISB5 = 1; //rx enabled
TRISB7 = 1; //tx enabled




Why are you setting both the RX and the TX pin to input?
#13
ric
Super Member
  • Total Posts : 27595
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: I need some help using the HC-05 bluetooth module with the PIC16F18446 microcontroller 2020/05/30 16:54:55 (permalink)
+3 (3)
upand_at_them
...Why are you setting both the RX and the TX pin to input?

That is what the datasheet tells you to do.
In reality, in ASYNC mode it does not matter what you set the TX direction to, the USART forces it into output mode whenever TXEN is set.
It's only important in SYNC mode, where the USART toggles the TX/DAT pin between input and output mode as required.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#14
upand_at_them
Super Member
  • Total Posts : 521
  • Reward points : 0
  • Joined: 2005/05/16 07:02:38
  • Location: Pennsylvania
  • Status: offline
Re: I need some help using the HC-05 bluetooth module with the PIC16F18446 microcontroller 2020/05/30 17:44:42 (permalink)
+1 (1)
Is that something that has been properly implemented?  I remember from one of the older PIC's that UART pin directions were supposed to be automatically setup, but I always had to do it because of a bug in the hardware (16F88, I think).
#15
ric
Super Member
  • Total Posts : 27595
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: I need some help using the HC-05 bluetooth module with the PIC16F18446 microcontroller 2020/05/30 18:47:51 (permalink)
0
On most PIC16F devices, RX must be set to input, and TX doesn't matter.
I don't know about the PIC16F88 in particular.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#16
nashsth
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2020/03/17 21:13:03
  • Location: 0
  • Status: offline
Re: I need some help using the HC-05 bluetooth module with the PIC16F18446 microcontroller 2020/05/31 17:36:02 (permalink)
0
Update:
 
Okay so I got half of the bluetooth to work. Right now, my phone receives messages that I send from the microcontroller, however the microcontroller doesn't receive the messages my phone sends. So the TX is working but the RX isn't. However, for some reason the message that I send gets sent twice. See the attached screenshot.
 
I made two changes to my code that fixed this: the first is that I had accidentally set OSCFRQ = 0x05 (16 MHz), instead of 0x06 (32 MHz). The other change I made is that I used the PPS register to set TX to RB7: RB7PPS = 0x0F.
 
I tried using the PPS register to set RX to RB5: RX1PPS = 0x0D, however this doesn't work. Even if I don't use the PPS register to set RX to RB5, the microcontroller isn't receiving anything. I checked and ensured that the RX of the HC-05 is connected to the TX of pic, and the TX of HC05 is connected to RX of pic.
post edited by nashsth - 2020/05/31 17:45:47

Attached Image(s)

#17
ric
Super Member
  • Total Posts : 27595
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: I need some help using the HC-05 bluetooth module with the PIC16F18446 microcontroller 2020/05/31 17:50:44 (permalink) ☄ Helpfulby nashsth 2020/05/31 19:16:03
+1 (1)
Are you writing to ANSELB to switch RB5 from analog mode to digital?
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#18
nashsth
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2020/03/17 21:13:03
  • Location: 0
  • Status: offline
Re: I need some help using the HC-05 bluetooth module with the PIC16F18446 microcontroller 2020/05/31 17:52:08 (permalink)
0
I tried that, yes, using: ANSELBbits.ANSB4 = 0; //disable analog functionality
but it didn't do anything :(
#19
nashsth
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2020/03/17 21:13:03
  • Location: 0
  • Status: offline
Re: I need some help using the HC-05 bluetooth module with the PIC16F18446 microcontroller 2020/05/31 17:54:49 (permalink)
+2 (2)
EDIT: doh.... my RX bit is RB5 but I was using ANSEL on ANSB4 -_- it works now.
 
Thank you so much for your help everyone :)
 
post edited by nashsth - 2020/05/31 17:59:14
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2020 APG vNext Commercial Version 4.5