PIC18F23k22 interfacing with HC05

Submitted by priya on

Hello

I am interfacing pic18f23k22 with HC05 bluetooth module through uart interface.if i send 'a' from bluetooth terminal app led must glow on and send some ok msg back to controller and if i press 'b' then led must off and send response back to pc.i have completed coding but i am not getting expected output so please help me to resolve this problem.i have also done hc05 setting through command mode.i am sharing my code please help me

//config file//

#include <xc.h>

/// CONFIG1H
#pragma config PLLCFG = ON // 4X PLL Enable->Oscillator multiplied by 4
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit->Fail-Safe Clock Monitor disabled
#pragma config PRICLKEN = ON // Primary clock enable bit->Primary clock enabled
#pragma config FOSC = INTIO67 // Oscillator Selection bits->Internal oscillator block
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit->Oscillator Switchover mode disabled

// CONFIG2L
#pragma config PWRTEN = OFF // Power-up Timer Enable bit->Power up timer disabled
#pragma config BOREN = OFF //SBORDIS // Brown-out Reset Enable bits->Brown-out Reset enabled in hardware only (SBOREN is disabled)
#pragma config BORV = 190  //3 // Brown Out Reset Voltage bits->VBOR set to 1.90 V nominal

// CONFIG2H
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits->1:32768
#pragma config WDTEN = OFF // Watchdog Timer Enable bits->Watch dog timer is always disabled. SWDTEN has no effect.

// CONFIG3H
#pragma config CCP2MX = PORTC1 // CCP2 MUX bit->CCP2 input/output is multiplexed with RC1
#pragma config P2BMX = PORTB5 // ECCP2 B output mux bit->P2B is on RB5
#pragma config MCLRE = EXTMCLR // MCLR Pin Enable bit->MCLR pin enabled, RE3 input pin disabled
#pragma config CCP3MX = PORTB5 // P3A/CCP3 Mux bit->P3A/CCP3 input/output is multiplexed with RB5
#pragma config T3CMX = PORTC0 // Timer3 Clock input mux bit->T3CKI is on RC0
#pragma config PBADEN = OFF // PORTB A/D Enable bit->PORTB<5:0> pins are configured as digital I/O on Reset
#pragma config HFOFST = ON // HFINTOSC Fast Start-up->HFINTOSC output and ready status are not delayed by the oscillator stable status

// CONFIG4L
#pragma config XINST = OFF // Extended Instruction Set Enable bit->Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
#pragma config LVP = ON // Single-Supply ICSP Enable bit->Single-Supply ICSP enabled if MCLRE is also 1
#pragma config STVREN = OFF  //ON  // Stack Full/Underflow Reset Enable bit->Stack full/underflow will not cause Reset
#pragma config DEBUG = OFF // Background Debug->Disabled

#define _XTAL_FREQ 16000000

//main.c //

#define USE_OR_MASKS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.c"
#include <pic18f23k22.h>
#include "plib/usart.h"
 
#define Baud_rate 9600 
 
 
 
unsigned char data_flush = 0 ;
unsigned char temp = 0;
unsigned char Data = 0;
unsigned char temp1 = 0;
unsigned char data =0;
unsigned char rc_data[100];
//unsigned char NoB[2];
//unsigned char *NoBt = NoB;    
unsigned char NoBt[2];
unsigned char TI[5];
unsigned char *TId = &TI[0];   
//int i=0;
unsigned char chrm=0,chrm1=0,j=0,k=0,flagq=0,flagp=0,flag1 = 0, flag2 = 0;
//unsigned char *buffer;//
static unsigned int i = 0;
unsigned int len = 0;
char out;
 
 
 
 
 
void SYSTEM_Initialize(void)
{
   
    TRISC=0X00;; //make port c as a output port
    OSCCON=0x72;       //Configure OSCON Register to use 16 MHZ  // for 8 MHZ 0x62  //internal oscillator. Please check datasheet    
    
   
  
    PIE1bits.RC1IE=1;  // enable receive interrupt
    PIR1bits.RC1IF=0;  //receive interrupt flag disable
    
    INTCONbits.PEIE = 1; //enable all peripheral interrupt
    INTCONbits.GIE = 1 ; //enable global interrupt
     
    
     
    PIE1bits.TMR1IE = 1; //enable timer 1 interrupt
    PIR1bits.TMR1IF = 0; //timer 1 interrupt flag disable
     
    PIE1bits.TX1IE=0;  //Disable transmit interrupt
    PIR1bits.TX1IF=0; // transmit interrupt flag disable
     
      SSPIE = 1;  //Master Synchronous Serial Port Interrupt Enable bit //1 = Enables the MSSP interrup
      
     PIE1bits.ADIE = 0; // Disable ADC interrupts
 
}
 
 
 
void Initialize_uart(void)
{
     
     BAUDCONbits.BRG16 = 0; // 8-bit Baud Rate Generator is used (SPBRGx)
    
     BAUDCONbits.ABDEN = 0; // Auto baud detect disabled
 
 
     SPBRG = 25;  //For a device with FOSC of 16 MHz, desired baud rate
    
     
    /*******TXSTA*********/
      TXSTAbits.TX9   = 0;    // 8-bit TRANSMISSION selected
       TXSTAbits.TXEN  = 1;    // enable transmission
     TXSTAbits.SYNC  = 0;    // Asynchronous mode
      TXSTAbits.BRGH  = 0;  // for low baud_rate
       TXSTAbits.TRMT = 0;
       
       
       /*******RCSTA*******/
     RCSTAbits.SPEN  = 1;    // Enable serial port pins
     RCSTAbits.RX9   = 0;    // 8-bit reception mode selected
     RCSTAbits.SREN = 0;  //Single Receive Enable bit
     RCSTAbits.CREN  = 1;    // enable reception
    
 
     TRISC6 = 0; // TX Pin set as output
     TRISC7 = 1; // RX Pin set as input
     
     
}
  
 
void UART_send_char(char bt)  
{
 
        while(!TX1IF);  // hold the program till TX buffer is free
    TXREG1 = bt; //Load the transmitter buffer with the received value
}
 
void UART_send_string(char* st_pt)
{
    while(*st_pt) //if there is a char
    UART_send_char(*st_pt++); //process it as a byte data
}
 
 
 
           
  
 void __interrupt ISR(void)
   
   if( PIR1bits.RC1IF == 1)
    {  
 
        if(RCSTAbits.OERR)
        {
            RCSTAbits.CREN = 0; //continuous receive enable bit
            NOP();
            RCSTAbits.CREN = 1;
        }
      
       data = RCREG;
if(data == 'a')
{
                
PORTCbits.RC5 = 1;
                UART_send_string("BR1ON");
 
                
}
else if(data =='b')
{
            
PORTCbits.RC5 = 0;
                 UART_send_string("BR2OFF");
 
}
 
      
       
     PIR1bits.RC1IF = 0;
    }
}
 
void main(void)
{
    
    SYSTEM_Initialize();
    Initialize_uart();
      
    PORTCbits.RC5 =0;
    while(1)
   {
    //  UART_send_char('A');
        
   }
  
}
 
    
    
 
            
   
    
 
     
    

What is happeing when you sending a and b?

  Joined February 12, 2018      696
Monday at 02:11 PM

  1. when i am sending a or b my isr must execute but my execution dont go to isr also another thing is if i am sending char 'A' in while loop in main function to bluetooth module ,i am getting some dummy data continuously .i have checked this code serially using DB9 cable but for bluetooth module it is not working
  2.  

  Joined October 21, 2021      4
Thursday at 02:55 PM

Check th configuration of the HC-05.

  Joined February 12, 2018      696
Monday at 02:11 PM

i checked configuration but now new problem arrises ,my hc05 is not detected in my android app also i checked using another 2 mobiles but hc05 is not detected why?

 

  Joined October 21, 2021      4
Thursday at 02:55 PM

solved

in hc05 configuration role must be slave 

 

  Joined October 21, 2021      4
Thursday at 02:55 PM

Great. Always check configuration of addtional devices when you conenct with a microcontroller.

  Joined February 12, 2018      696
Monday at 02:11 PM

yes.

  Joined October 21, 2021      4
Thursday at 02:55 PM