PIC16F877A works with RS232 but not with HC-05

Thread Starter

Aswinth

Joined Nov 1, 2016
34
I am trying to connect my HC-05 bluetooth module with PIC16F877A. I know we have to use USART to talk with bluetooth module. So I started with USART of PIC by reading its datasheet.

I am using MPLABX IDE with XC8 compiler and i am not interested in using any libraries.

Code:
// CONFIG#pragma config FOSC = HS 
// Oscillator Selection bits (HS oscillator)#pragma config WDTE = OFF 
// Watchdog Timer Enable bit (WDT disabled)#pragma config PWRTE = ON 
// Power-up Timer Enable bit (PWRT enabled)#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)

// #pragma config statements should precede project file includes.// Use project enums instead of #define for ON and OFF.

#define _XTAL_FREQ 20000000
#include<xc.h>

void main(void){

unsignedint i;
TRISA=0xff;

//UART
TRISC=0X00;
TXSTA=0X20;// TX RS232 settings
RCSTA=0X80;// RX RS232 Settings
SPBRG=31;// Baud rate is set based on Crystal clock

TXSTAbits.TXEN=1;// Enable RS232 transmissionRCSTAbits.SPEN=1;// Enable RS232 reception

while(1){

TXREG=65;
__delay_ms(200);

}}
I used the above code to send a char 'A' from PIC to USART and was successful in reading it using my serial monitor on PC (with RS232).

Now form what I understood, if I replace the RS232 with my bluetooth module I should get the same char 'A' transmitted to my mobile phone.

I am using Bluetooth Terminal android app to check the reception of data. But the after the app connects to the module it just shows a blank screen. I dont receive anything.

What could be the reason please help me!!!!!!.

Things that I tried :

  1. There is no problem with the baud-rate, both my Rs232 and HC-05 works at 9600. I have ensured the same using my arduino and HC-05.

  2. there is no logic level problem I have used a potential divider to shift 5V to 3.3V.
I am quite new here so forgive me if my question is not that appropriate.

Moderator edit: inserted code tags
 

AlbertHall

Joined Jun 4, 2014
12,653
With RS232, you can send data without reference to what is at the other end of the cable (even if the cable isn't connected to anything).
Bluetooth doesn't work like that. The devices need to paired so that they will recognise each other and you need one end as a master and the other end as a slave.
There is some (badly translated) information here:
https://www.rcscomponents.kiev.ua/datasheets/hc_hc-05-user-instructions-bluetooth.pdf
 

Thread Starter

Aswinth

Joined Nov 1, 2016
34
Hi AlbertHall,

Thanks for your time in interest in replying me. I sorted out the issue, now i am able to transmit the information to my Phone (Bluetooth Terminal App) via HC-05.

The problem has been with the __delay_ms(); function in my program, once i removed received the transmitted characters on the other end. I am still trying to make it perfect. Will update the thread once I sort out the issues completely.

Also thanks for your link, will read it shortly.
 
Top