CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Interfacing hc-05 bluetooth module with pic 18f2550
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
srikrishna



Joined: 06 Sep 2017
Posts: 82

View user's profile Send private message

Interfacing hc-05 bluetooth module with pic 18f2550
PostPosted: Wed Oct 11, 2017 1:12 am     Reply with quote

I can not interface hc-05 Bluetooth module with pic 18f2550. I require help. Please write me a sample code and corresponding circuit connection. Here is the picture of the module that i use. I am using tera term software to see the values coming from com port.


Last edited by srikrishna on Wed Oct 11, 2017 3:57 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19536

View user's profile Send private message

PostPosted: Wed Oct 11, 2017 2:21 am     Reply with quote

You won't.....

Problem is the HC-05, is a 3.3v module. Though it can be powered off 5v, it only gives a 3.3v output.
To connect it to the 2550, you need level conversion hardware.

The TX line out of the PIC needs a voltage divider (simple resistive divider say 1K8 to the PIC TX line, then a 3K3 to ground, and feed the junction to the RXD pin on the HC05) - otherwise you risk damaging the HC05 with the overvoltage on this line.
The TXD line needs a level shifter. Something like a 74ACT125D, or a similar chip that accepts a TTL level 'in' and gives a near 5v 'out'.

Problem here is the PIC RXD needs the signal to go up to 4v to be seen as a logic '1'. The 3.3v HC05 can't do this.
srikrishna



Joined: 06 Sep 2017
Posts: 82

View user's profile Send private message

PostPosted: Wed Oct 11, 2017 3:58 am     Reply with quote

The model i have using has TTL outputs. Here is the specifications:
temtronic



Joined: 01 Jul 2010
Posts: 9241
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Oct 11, 2017 4:49 am     Reply with quote

As Mt T points out the device in your picture is a 3 volt unit and nothing in the specs you post say what the MCU interface logic levels are. There's no power supply data to read.

As a general rule, all peripheral devices (gps,mem,rtc,etc) these days ARE 3 volt devices.Very few are 5 volt. Some, mounted on PCBs, aka 'modules' _may_ have logic level shifters on board.

There are two way to solve this 5V PIC vs 3V device problem.

#1. Use a 3 volt PIC. Most PIC families have an 'L' version,which is 3 volt rated. Some PICs, like th 18F46K22 WILL run at 3 or 5 volts. Using a PIC designed to run at 3 volts means NO logic level conversion necessary, simpler PCB design, faster R&D time !

#2. Design and install proper logic level conversion. This can consist of transistors, ICs or even 'modules'. Downfall is cost, space and potential mis-wiring.

As a BTW, I've found the HC-12 version to be a better device. Easier to use, similar price. Beware though all these devices require a LOT of power for xmt...it's in the specs...so be sure your PSU is good for it !

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19536

View user's profile Send private message

PostPosted: Wed Oct 11, 2017 6:52 am     Reply with quote

srikrishna wrote:
The model i have using has TTL outputs. Here is the specifications:


The outputs will drive conventional TTL. Problem is the inputs of the PIC (for serial), are Schottky, not conventional TTL. This is why you can use a TTL input gate as a buffer to the PIC.

Jay has given a lot more info.
guy



Joined: 21 Oct 2005
Posts: 297

View user's profile Send private message Visit poster's website

PostPosted: Thu Oct 12, 2017 2:24 am     Reply with quote

namaskar srikrishna.
With low baud rates (slow communication, less than 115,200) you can also build a level converter with a transistor:
https://cdn.sparkfun.com/assets/f/3/3/4/4/526842ae757b7f1b128b456f.png
you can also replace the transistor with a fast NPN trans. such as 2N3904.
Let me know if you need a more detailed schematic & explanation or you're good with this.
Also I suggest you read all of the manual - there are a few things to look out for with the HC-05. https://www.itead.cc/wiki/Serial_Port_Bluetooth_Module_(Master/Slave)_:_HC-05
See Ttelmah's answer on how to divide the voltage of the PIC TX line from 5V to 3.3V .

As for code, check the compiler's example on #USE RS232, output a string to the HC-05 every second and see if you receive it on the computer.
srikrishna



Joined: 06 Sep 2017
Posts: 82

View user's profile Send private message

PostPosted: Wed Nov 15, 2017 2:07 am     Reply with quote

Ttelmah wrote:
You won't.....
.


But Ttelmah some of my friends also interfaced bluetooth with pic. They interfaced Hc-05 as well as Hc-06 module. Here is the code. Although they used mplab xc8 compiler for coding.
Code:
#include <xc.h>
#include <p18F452.h>
#include <stdio.h>
#include <string.h>

#pragma config OSC = HSPLL      // Oscillator Selection bits (HS oscillator with PLL enabled/Clock frequency = (4 x FOSC))
#pragma config OSCS = ON        // Oscillator System Clock Switch Enable bit (Oscillator system clock switch option is enabled (oscillator switching is enabled))
#pragma config PWRT = ON        // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = ON         // Brown-out Reset Enable bit (Brown-out Reset disabled)
#pragma config BORV = 20        // Brown-out Reset Voltage bits (VBOR set to 2.0V)
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 128      // Watchdog Timer Postscale Select bits (1:128)
#pragma config CCP2MUX = ON    // CCP2 Mux bit (CCP2 input/output is multiplexed with RB3)
#pragma config STVR = OFF       // Stack Full/Underflow Reset Enable bit (Stack Full/Underflow will not cause RESET)
#pragma config LVP = OFF        // Low Voltage ICSP Enable bit (Low Voltage ICSP disabled)

#define _XTAL_FREQ 40000000

void setup_port(void);
void clear(void);

void inter(void);
void setup_bluetooh(void);
void TXdata(unsigned char data);
void RXdata(void);
void sentence_to_BL(void);
unsigned int datarx = 0;
unsigned int BLsentence[40];
unsigned int lartext = 0; unsigned char i = 0;

void main (void){
    setup_port();
    setup_bluetooh();
    inter();
    clear();
    while(1){
        if (PIR1bits.RCIF == 1){
            RXdata();
        }
        sprintf (BLsentence,"Hola");
        sentence_to_BL();
        __delay_us(100);
    }
   
    return;
}

void setup_port(){
    TRISC=0x80; LATC=0x00; PORTC=0x00;
 }
void clear(void){
    TXREG=0;
}

void inter(void){
    INTCONbits.GIE = 1;
    INTCONbits.PEIE = 1;
}
void setup_bluetooh(void){
    TXSTA=0x22;
    RCSTA=0x90;
    SPBRG=64;
}
void TXdata(unsigned char data){
    while(TXSTAbits.TRMT == 0);
    TXREG = data;
}
void RXdata(void){
    if (RCSTAbits.OERR == 1){ //Error de sobrecarga.
        RCSTAbits.CREN = 0; RCSTAbits.CREN = 1;//Reinicia CREN
    }
    datarx =  RCREG;
}
void sentence_to_BL(void){
    lartext = strlen(BLsentence);
    for (i = 0; i < lartext; i++){
        TXdata(BLsentence[i]);
        BLsentence[i] = 0;
        __delay_us(100);
    }
}


And used hyperterminal to see the result. But my question is, is this possible in ccs c??
Ttelmah



Joined: 11 Mar 2010
Posts: 19536

View user's profile Send private message

PostPosted: Wed Nov 15, 2017 2:37 am     Reply with quote

Seriously, they have either just been very lucky, or they have modules that have 3.3v to 5v conversion built in.

We are not talking something software changes, but _hardware_ differences. I've used these modules. To get reliable connection with the PIC (using assembler, Microchip C, or CCS C), there _has_ to be circuitry to raise the output voltage to match the PIC's input requirement, _or_ the PIC can be run at a lower voltage to give the same effect (for some PIC's).

The 452, they could be running at 40MHz down to 4.2v. At 4.2v, the module would (just) work.
temtronic



Joined: 01 Jul 2010
Posts: 9241
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Nov 15, 2017 6:07 am     Reply with quote

As Mr. T points out, you need to change the Hardware not the software (code).
You've got a 2550 , your friend has a 452. Yes, they are PICs but NOT of the same family, so it is very possible the actual PIC, the 'die' is physically different and the 452 just happens to work, actually 'out of spec'. While it might work 'on the bench' change the temperature and it'll probably stop working. In order to get reliable operation you must supply VDD within the spec at the operating frequency. Typically, charts show the lower the VDD, the slower the PIC can operate. One of them 'physics' details we never learned at school..
Also be sure to have a high current power supply ! Wireless modules tend to need 1 amp (or more) during transmit. While a short duration of time, drawing that much current can drop VDD below the PICs proper operating requirement and it will not function correctly.

Bottom line.. NO amount of code will fix incorrect hardware.

Jay
srikrishna



Joined: 06 Sep 2017
Posts: 82

View user's profile Send private message

PostPosted: Tue Dec 05, 2017 5:17 pm     Reply with quote

Ttelmah wrote:
I've used these modules. To get reliable connection with the PIC (using assembler, Microchip C, or CCS C), there _has_ to be circuitry to raise the output voltage to match the PIC's input requirement


Ttelmah can you provide me picture of your circuit diagram ??
temtronic



Joined: 01 Jul 2010
Posts: 9241
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Dec 05, 2017 6:04 pm     Reply with quote

There's a LOT of information about these on the web, easy to use PROVIDING you use a 3 volt PIC.
PIC TXD > HC05 RXD
PIC RXD> HC05 TXD
PIC GND--HC05GND
PIC Vdd--HC05 VCC -- +3 volts

Please state which PIC you're trying to use.
srikrishna



Joined: 06 Sep 2017
Posts: 82

View user's profile Send private message

PostPosted: Tue Dec 05, 2017 7:09 pm     Reply with quote

temtronic wrote:


Please state which PIC you're trying to use.


pic 18f2550
temtronic



Joined: 01 Jul 2010
Posts: 9241
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Dec 05, 2017 9:06 pm     Reply with quote

That PIC is NOT rated for 3 volt operation. As such will NOT directly interface( connect) to the HC-05 module.
You have options
1) add logic level conversion between the 5 V PIC and the 3V HC-05

2) use the PIC18LF2550, the 3 V rated PIC of that family.

I'd choose #2 as it's fewer parts and less chance for a wiring or connection error. Also be sure to use a power supply rated for at least 2 Amps. All wireless devices require a LOT of power during transmit mode.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19536

View user's profile Send private message

PostPosted: Tue Dec 05, 2017 11:57 pm     Reply with quote

I've used a 74125 for the conversion from 3v to 5v.

For a little board:
<https://www.sparkfun.com/products/12009>

This is the most basic FET circuit, that works well:
<http://www.hobbytronics.co.uk/mosfet-voltage-level-converter>

Some of the breakout boards supplied with HC05 modules have this circuit built onto the board which is why the friends of the original poster may have been OK, but the standard modules don't, since it is not needed to drive the inputs of chips like the Arduino.
srikrishna



Joined: 06 Sep 2017
Posts: 82

View user's profile Send private message

PostPosted: Thu Dec 21, 2017 2:59 am     Reply with quote

Ttelmah wrote:

This is the most basic FET circuit, that works well:
<http://www.hobbytronics.co.uk/mosfet-voltage-level-converter>

Ttelmah I have brought this module from ebay.in
https://www.ebay.in/itm/4CH-Logic-Level-Converter-Bi-Directional-Shifter-Module-Board-3-3V-5V/122678173676?ssPageName=STRK%3AMEBIDX%3AIT&_trksid=p2057872.m2749.l2649

This is the picture of the module



And i get the corresponding connection from sparkfun site
https://learn.sparkfun.com/tutorials/bi-directional-logic-level-converter-hookup-guide

Here is the connection i have used


This is my code
Code:
#include <18f2550.h>
#device ADC=10

#FUSES WDT128
#FUSES INTXT
#FUSES NOPROTECT
#FUSES NOBROWNOUT
#FUSES NOPUT
#FUSES NOCPD
#FUSES STVREN
#FUSES NODEBUG
#FUSES NOLVP
#FUSES NOWRT
#FUSES NOWRTD
#FUSES IESO
#FUSES FCMEN
#FUSES PBADEN
#FUSES NOWRTC
#FUSES NOWRTB
#FUSES NOEBTR
#FUSES NOEBTRB
#FUSES NOCPB
#FUSES MCLR
#FUSES LPT1OSC
#FUSES NOXINST
#FUSES PLL1
#FUSES CPUDIV1
#FUSES NOUSBDIV
#FUSES NOVREGEN

#use delay(clock=20M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,PARITY=N,BITS=8)
void main()
{
int16 pot;
setup_adc_ports(AN0_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
delay_us(10);
while (TRUE);
{
pot = read_adc();
delay_ms(20);
printf("%lu",pot);

delay_ms(1000);
}
}


But still i did not get any value from pic. Sad Sad

Please tell me any changes needed in the hardware or in the code.


Last edited by srikrishna on Thu Dec 21, 2017 8:45 am; edited 2 times in total
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group