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

Discussion in 'Embedded Systems and Microcontrollers' started by alohara, Sep 4, 2015.

  1. alohara

    Thread Starter New Member

    Sep 4, 2015
    1
    0
    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 (Text):
    1. //Libraries inclusion
    2. #include <xc.h>
    3. #include <usart.h>
    4.  
    5. //Configurations
    6. #pragma config PLLDIV = 5 //PLL to 20MHz
    7. #pragma config CPUDIV = OSC1_PLL2  //PLL off
    8. #pragma config FOSC = HS //High speed oscillator - 20MHz
    9. #pragma config WDT = OFF //Watchdog off
    10. #pragma config PBADEN = OFF //PORTB begins as digital
    11. #pragma config LVP = OFF //Low voltage record off
    12. #pragma config DEBUG = OFF //Debug off
    13. #pragma config PWRT = ON //Power-up timer on
    14. #pragma config BOR = ON //Brown-out reset on
    15. #pragma config MCLRE = OFF //Master clear off
    16.  
    17. #define _XTAL_FREQ 20000000 // Oscillator crystal frequency - 20MHz
    18.  
    19. #define RXFLAG PIR1bits.RCIF     //Arrival of byte by serial flag
    20. #define TXFLAG PIR1bits.TXIF     // TXREG empty flag
    21.  
    22. #define BAUDRATE 9600
    23.  
    24. //Usart initiation function
    25. void usart_init(void)
    26. {
    27.     OpenUSART(USART_TX_INT_OFF &    //Transmit interrupt off
    28.     USART_ASYNCH_MODE &        //Asynchronous mode
    29.     USART_EIGHT_BIT &            //8-bit data
    30.     USART_BRGH_HIGH,25);                              //9600 baudrate
    31. }
    32.  
    33. //Main function
    34. void main (void)
    35. {
    36.     INTCON = 0;
    37.     PORTC = 0;
    38.     TRISC = 0xF9; //0b11111001  //RC6 and RC7 as TX and RX
    39.  
    40.     //Serial port inicialization
    41.     TXSTA = 0x24;   //Asynchronous mode to 8 bits
    42.     SPBRG = 129;    //Oscillator 20MHz - Baud Rate = 9600bps
    43.     RCSTA = 0x90;   //Serial transmission on
    44.  
    45.     INTCON2bits.RBPU = 0;
    46.     __delay_ms(30);
    47.  
    48.       char p = 0x31;
    49.  
    50.     while (1)
    51.     {
    52.             usart_init();   //Calls usart initialization function
    53.             __delay_ms(10);
    54.             putrsUSART(p); //Transmission of string
    55.             __delay_ms(10);
    56.     }
    57. }
    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.
     
  2. jayanthd

    Member

    Jul 4, 2015
    259
    25
    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.
     
  3. jayanthd

    Member

    Jul 4, 2015
    259
    25
    Hi Alohara

    Here I am attaching a MPLAB X C18 project which I made for you. I have only tested it in Proteus and it is working fine. Test it in hardware and reply.

    Use WinRAR to open the file. bt hc06.png
     
  4. jayanthd

    Member

    Jul 4, 2015
    259
    25
    Here is the code.

    Code (C):
    1.  
    2.  
    3. #include <p18f4550.h>
    4. #include <delays.h>
    5. #include <usart.h>
    6. #include <stdio.h>
    7. #include <stdlib.h>
    8.  
    9. //Configurations
    10. #pragma config PLLDIV = 5 //PLL to 20MHz
    11. #pragma config CPUDIV = OSC1_PLL2  //PLL off
    12. #pragma config USBDIV = 2
    13. #pragma config FOSC = HS //High speed oscillator - 20MHz
    14. #pragma config WDT = OFF //Watchdog off
    15. #pragma config PBADEN = OFF //PORTB begins as digital
    16. #pragma config LVP = OFF //Low voltage record off
    17. #pragma config DEBUG = OFF //Debug off
    18. #pragma config PWRT = ON //Power-up timer on
    19. #pragma config BOR = ON //Brown-out reset on
    20. #pragma config MCLRE = OFF //Master clear off
    21.  
    22. char data = 'A';
    23. char str[] = "\r\nHi, I am Alohara and I am testing BT HC-06\r\n";
    24.  
    25. //Main function
    26. void main (void) {
    27.  
    28.     INTCON = 0x00;
    29.    
    30.     CMCON = 0x07;
    31.     CVRCON = 0x00;
    32.    
    33.     ADCON1 = 0x0F;
    34.    
    35.     TRISA = 0xC0;
    36.     TRISB = 0x00;
    37.     TRISC = 0xC0;
    38.     TRISD = 0x00;
    39.     TRISE = 0x00;
    40.    
    41.     PORTA = 0x00;
    42.     PORTB = 0x00;
    43.     PORTC = 0x00;
    44.     PORTD = 0x00;
    45.     PORTE = 0x00;
    46.    
    47.     LATA = 0x00;
    48.     LATB = 0x00;
    49.     LATC = 0x00;
    50.     LATD = 0x00;
    51.     LATE = 0x00;
    52.    
    53.     Delay10KTCYx (240);
    54.     Delay10KTCYx (240);
    55.     Delay10KTCYx (120);
    56.    
    57.     OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_ADDEN_OFF & USART_BRGH_HIGH, 129);
    58.        
    59.     Delay10KTCYx (240);
    60.     Delay10KTCYx (240);
    61.     Delay10KTCYx (120);
    62.    
    63.     while (1)
    64.     {
    65.         WriteUSART(data);
    66.        
    67.         Delay10KTCYx (240);
    68.         Delay10KTCYx (240);
    69.         Delay10KTCYx (120);
    70.        
    71.         Delay10KTCYx (240);
    72.         Delay10KTCYx (240);
    73.         Delay10KTCYx (120);
    74.        
    75.                
    76.         putsUSART(str);
    77.        
    78.         Delay10KTCYx (240);
    79.         Delay10KTCYx (240);
    80.         Delay10KTCYx (120);
    81.        
    82.         Delay10KTCYx (240);
    83.         Delay10KTCYx (240);
    84.         Delay10KTCYx (120);      
    85.     }  
    86. }
    87.  
     
  5. paulfjujo

    New Member

    Mar 6, 2014
    20
    3
    hello,

    i think your HC06 is powered with 5V
    but level Tx RX are with 3,3V levels ?
    so wiring PIC Tx 5V -> HC06 RX 3,3V is bad..
    use a resistor divider US= UE * R1 / (R1+R2)
    5 * 1,5/ 2,5 => 3V

    Pic_HC06.jpg
     
    SK MBATHA likes this.
Loading...