Help with PIC18F4550 programation to interface it with bluetooth module HC-06

Thread Starter

alohara

Joined Sep 4, 2015
1
Hello,

Currently I am developing a student project where I want to communicate a microcontroller with a computer in wireless way. I chose the PIC18F4550 because is the microcontroller I am most familiar with and to make the wireless communication I chose the bluetooth module HC-06.

But I found what I did isn't working so I decided to fractionate and test it. I tested the physical scheme (which I also uploaded the file with PIC18F4550 datasheet and HC-06 datasheet) and seems like there's no problem with it.
schematic.jpg
Fig: schematic

Anyway when I simplified the microcontroller code (I'm using C language) to make some tests I found out nothing is being sent to the computer. So I think the main problem is with my code.

Code:
//Libraries inclusion
#include <xc.h>
#include <usart.h>

//Configurations
#pragma config PLLDIV = 5 //PLL to 20MHz
#pragma config CPUDIV = OSC1_PLL2  //PLL off
#pragma config FOSC = HS //High speed oscillator - 20MHz
#pragma config WDT = OFF //Watchdog off
#pragma config PBADEN = OFF //PORTB begins as digital
#pragma config LVP = OFF //Low voltage record off
#pragma config DEBUG = OFF //Debug off
#pragma config PWRT = ON //Power-up timer on
#pragma config BOR = ON //Brown-out reset on
#pragma config MCLRE = OFF //Master clear off

#define _XTAL_FREQ 20000000 // Oscillator crystal frequency - 20MHz

#define RXFLAG PIR1bits.RCIF     //Arrival of byte by serial flag
#define TXFLAG PIR1bits.TXIF     // TXREG empty flag

#define BAUDRATE 9600

//Usart initiation function
void usart_init(void)
{
    OpenUSART(USART_TX_INT_OFF &    //Transmit interrupt off 
    USART_ASYNCH_MODE &        //Asynchronous mode
    USART_EIGHT_BIT &            //8-bit data
    USART_BRGH_HIGH,25);                              //9600 baudrate
}

//Main function
void main (void)
{
    INTCON = 0;
    PORTC = 0;
    TRISC = 0xF9; //0b11111001  //RC6 and RC7 as TX and RX

    //Serial port inicialization
    TXSTA = 0x24;   //Asynchronous mode to 8 bits
    SPBRG = 129;    //Oscillator 20MHz - Baud Rate = 9600bps
    RCSTA = 0x90;   //Serial transmission on

    INTCON2bits.RBPU = 0;
    __delay_ms(30);

      char p = 0x31;

    while (1)
    {
            usart_init();   //Calls usart initialization function
            __delay_ms(10);
            putrsUSART(p); //Transmission of string
            __delay_ms(10);
    }
}
I feed the bluetooth module (with 5V, it can be powered from 3.3V to 6V) and got its signal with the computer so I checked its properties and it seems like it's all matching with the properties I configured in the code (9600 bps, etc.).

I only want to do a constant serial transmission and to begin with I want to just send a character to computer as a test. It's my first time working with the usart.h library though and I'm not getting what I am doing wrong.

I read that it'll send the character/string in ASCII code so I put the value of p as 0x31 (1 in ASCII) but I'm not sure if I already have to put it in ASCII values. Also I don't know if I'm using the putrsUSART command in the right way. I chose this command because lately I will need to transmit a string but I already tested my code with putcUSART, putsUSART and WriteUSART but nothing worked.

I am very lost so I would be very thankful if someone can help me with this, please.
 

Attachments

jayanthd

Joined Jul 4, 2015
904
Hi alohara

Please mention which Compiler you are using. It seems you are using MPLAB X IDE but have selected wrong Compiler. In code you have included xc.h file which is for XC8 compiler but in code you use OpenUSART() function which is a function of C18 Compiler I think. If you are using C18 Compiler then you have to include p18f4550.h file.

Are you using XC8 compiler but C18 USART library with XC8 code ?

One issue I see in your code is that you are calling usart_init() in while(1) loop. Which is not a good practice. You have to call it once before while(1) loop and then you can use the putrsUSART() in while(1) loop to send data. Also 18F datasheet tells TRIS of RX and TX pins should be 1 (input) but try both TRISC = 0xC0 and TRISC = 0x80 and see if any works.

It would be better if you zip and post your complete MPLAB/MPLAB X project files. I will have a look at it and try to fix the problems.
 

jayanthd

Joined Jul 4, 2015
904
Here is the code.

C:
#include <p18f4550.h>
#include <delays.h>
#include <usart.h>
#include <stdio.h>
#include <stdlib.h>

//Configurations
#pragma config PLLDIV = 5 //PLL to 20MHz
#pragma config CPUDIV = OSC1_PLL2  //PLL off
#pragma config USBDIV = 2
#pragma config FOSC = HS //High speed oscillator - 20MHz
#pragma config WDT = OFF //Watchdog off
#pragma config PBADEN = OFF //PORTB begins as digital
#pragma config LVP = OFF //Low voltage record off
#pragma config DEBUG = OFF //Debug off
#pragma config PWRT = ON //Power-up timer on
#pragma config BOR = ON //Brown-out reset on
#pragma config MCLRE = OFF //Master clear off

char data = 'A';
char str[] = "\r\nHi, I am Alohara and I am testing BT HC-06\r\n";

//Main function
void main (void) {

    INTCON = 0x00;
   
    CMCON = 0x07;
    CVRCON = 0x00;
   
    ADCON1 = 0x0F;
   
    TRISA = 0xC0;
    TRISB = 0x00;
    TRISC = 0xC0;
    TRISD = 0x00;
    TRISE = 0x00;
   
    PORTA = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
    PORTD = 0x00;
    PORTE = 0x00;
   
    LATA = 0x00;
    LATB = 0x00;
    LATC = 0x00;
    LATD = 0x00;
    LATE = 0x00;
   
    Delay10KTCYx (240);
    Delay10KTCYx (240);
    Delay10KTCYx (120);
   
    OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_ADDEN_OFF & USART_BRGH_HIGH, 129);
       
    Delay10KTCYx (240);
    Delay10KTCYx (240);
    Delay10KTCYx (120);
   
    while (1)
    {
        WriteUSART(data);
       
        Delay10KTCYx (240);
        Delay10KTCYx (240);
        Delay10KTCYx (120);
       
        Delay10KTCYx (240);
        Delay10KTCYx (240);
        Delay10KTCYx (120);
       
               
        putsUSART(str);
       
        Delay10KTCYx (240);
        Delay10KTCYx (240);
        Delay10KTCYx (120);
       
        Delay10KTCYx (240);
        Delay10KTCYx (240);
        Delay10KTCYx (120);      
    }  
}
 
Top