Pages

Thursday 20 November 2014

Bluetooth Module HC-06


Bluetooth Module HC-06:

This module enables you to wireless transmit & receive serial data. It is a drop in replacement for wired serial connections allowing transparent two way data communication. You can simply use it for serial port replacement to establish connection between MCU or embedded project and PC for data transfer. This board operates on 5V and has LED indication and 3V regulator.







Bluetooth Interface Arm code: 


/*
 * Project name:
     BLUETOOTH MODULE
 * Copyright
     (c) Researchdesignlab.com
 * Description:
    
 * Test configuration:
     MCU:             LPC2129
     Dev.Board:       LPC2129 mini
     Oscillator:      12 MHz
     Software:        Keil uVision3

*/


#include<stdio.h>
#include"lpc21xx.h"    // header file
#include<string.h>
//*******************************************************************
void SEND_CMD_2_LCD(unsigned char);    // function protoype
void SEND_2_LCD(unsigned char *, unsigned char);
void DISPLAY_ON_LCD(unsigned char DATA);
unsigned char RECIVE_DATA(void);
void TRANSMIT_UART0_BYTE(unsigned char P);
void TRANSMIT_UART0(unsigned char *P);


#define sw4 0x00010000 

void DELAY(void); 
void LCD_DELAY(void);


unsigned int LCD_RS=0X01000000;      // pin declerations
unsigned int LCD_EN=0X02000000;
unsigned int LCD_DATA_LINES=0X00003C00;
unsigned char RX;
                        

//*******************************************************************
int main()
{
    unsigned char LCD_CMD[]={0X20,0X28,0X0f,0X06,0X01,0X80},COUNT,i; // LCD commands


    IODIR0|=LCD_DATA_LINES;
    IODIR1=0X03300000;
      
    for(COUNT=0;COUNT<6;COUNT++)
    SEND_CMD_2_LCD(LCD_CMD[COUNT]); // LCD INIT function call

    PINSEL0    = 0X00050000;
    PINSEL1=0X15400000;       // UART INIT
    U1LCR = 0X83;           // 8bit data setting        
    U1DLL = 0X61;                                   
    U1LCR = 0X03;    // 9600 baud rate setting        
    U1FCR = 0X01;    // FIFO enable

    PINSEL0|=0X00000005;
    U0LCR=0X83;
    U0DLL=0X61;
    U0DLM=0X00;
    U0LCR=0X03;

      DELAY();

      DELAY();
      DELAY();
      
                 
  TRANSMIT_UART0("MC 1");              //TRANSMIT DATA TO BLUE TOOTH
  while(1)
   {
  
   
   RX=RECIVE_DATA();            // RECEIVING BLUE TOOTH DATA DISPLAY ON LCD
   DISPLAY_ON_LCD(RX);
   i++;
  

  }
 }



    void DELAY(void)
    {
    unsigned int X=800000;
    while(X--);
    }
    
    
    void LCD_DELAY(void)
    {
    unsigned int X=20000;  // delay calculation subroutine
    while(X--);
    } 

 void SEND_CMD_2_LCD(unsigned char DATA)
{
    unsigned int TEMP;                      // LCD command write function
    IOCLR0=LCD_DATA_LINES;
    TEMP=DATA;
    TEMP=((TEMP&0xf0)<<6);
    IOCLR1=LCD_RS;
    IOSET0=TEMP;
    IOSET1=LCD_EN;
    LCD_DELAY();
    LCD_DELAY();
    IOCLR1=LCD_EN;
    IOCLR0=LCD_DATA_LINES;
    
    LCD_DELAY();

    TEMP=DATA;
    TEMP=((TEMP&0x0f)<<10);
    IOCLR1=LCD_RS;
    IOSET0=TEMP;
    IOSET1=LCD_EN;
    LCD_DELAY();
    LCD_DELAY();
    IOCLR1=LCD_EN;
    IOCLR0=LCD_DATA_LINES;
}

void DISPLAY_ON_LCD(unsigned char DATA)
{
    unsigned int TEMP;
    IOCLR0=LCD_DATA_LINES;
    TEMP=DATA;                          //LCD data write function
    TEMP=((TEMP&0xf0)<<6);
    IOSET1=LCD_RS;
    IOSET0=TEMP;
    IOSET1=LCD_EN;
    LCD_DELAY();
    LCD_DELAY();
    IOCLR1=LCD_EN;
    IOCLR0=LCD_DATA_LINES;
    
    LCD_DELAY();

    TEMP=DATA;
    TEMP=((TEMP&0x0f)<<10);
    IOSET1=LCD_RS;
    IOSET0=TEMP;
    IOSET1=LCD_EN;
    LCD_DELAY();
    LCD_DELAY();
    IOCLR1=LCD_EN;
    IOCLR0=LCD_DATA_LINES;
}


void SEND_2_LCD(unsigned char *PTR, unsigned char POS)
{
    SEND_CMD_2_LCD(POS);
    while(*PTR)
    {                               // sending the data to LCD data line function
        DISPLAY_ON_LCD(*PTR);
        PTR++;
    }
    LCD_DELAY();
    LCD_DELAY();
}


void TRANSMIT_UART0(unsigned char *P)
   {
    while(*P){
        U0THR=*P;
        while(!(U0LSR&0X20));
        P++;}
    }

        
void TRANSMIT_UART0_BYTE(unsigned char P)
   {

        U0THR=P;
        while(!(U0LSR&0X20));
    
    }

    
  unsigned char RECIVE_DATA(void)
   { 
        while(!(U0LSR&0X01));
        RX=U0RBR;
        return(RX);
   }


Bluetooth Interface atmel schematic:




Bluetooth Interface Atmel Code:


/*
 * Project name:
    BLUETOOTH MODULE
 * Copyright
     (c) Researchdesignlab.com
 * Description:
    
 * Test configuration:
     MCU:             AT89S52
     Dev.Board:       8051
     Oscillator:      11.0592 MHz
     Software:        Keil uVision3

*/


#include<reg51.h>  

#define LCD_PORT P2                    // LCD D0-D7 PINS connected P2
sbit rs=P3^5;                        // LCD RS PIN connected P3.5
sbit en=P3^7;                        // LCD EN PIN connected P3.7
sbit D7=P2^7;
sbit rw=P3^6;                         // LCD RW PIN connected P3.6


void busy();                          //LCD busy 
void CMD_WRT(unsigned char);           //To write the commands to the LCD
void DATA_WRT(unsigned char);         //To write the data to the LCD
void LCD_WRT(unsigned char *); 
void DELAY();
void TRANSMIT(unsigned char *);
void transmit_byte(unsigned char );
unsigned char SCI_ReceiveByte( void );


 void main()
  {
   unsigned char CMD[]={0x38,0x01,0x0f,0x06,0x80},TEMP1,i,RX;

    for(i=0;i<5;i++)
    {
    TEMP1=CMD[i];                         //write the commands to the LCD
    CMD_WRT(TEMP1);
    }

    TMOD=0X20;                              //TIMER 1 , MODE 2
    TH1=0XFD;                              //9600 BAURD RATE            //SERAIL INIT
       SCON=0X50;
    TR1=1;                                  //TIMER1 ON
    DELAY();
    DELAY();
    DELAY();                               //DELAY
    DELAY();

     TRANSMIT("MC 1 ");          // trasmit the data to BLUETOOTH
  while(1)
  {
  
  
   RX=SCI_ReceiveByte();                      // RECIVING DATA from BLUETOOTH AND DISPLAY ON LCD
   DATA_WRT(RX);
      
  }
 }
    
    void DELAY()
    {                                  //delay of 3ms 
    unsigned int X=800000;
    while(X--);
    }
    
    
    void busy()
    {
    D7=1;
    rs=0;                             //cmd mode
    rw=1;                             //read
    while(D7!=0)                    //wait till LCD is ready
    {
    en=0;
    en=1;                            // low to high latch 
    }
    } 
    
    
    
    void CMD_WRT(unsigned char val)
    {
    busy();
    LCD_PORT=val;                   //write cmd to P2
    rs=0;                           //cmd mode
    rw=0;                           //write
    en=1;
    en=0;                           //high to low latch 
    }
    
    
    void DATA_WRT(unsigned char ch)
    {
    busy();
    LCD_PORT = ch;                  //write cmd to P2
    rs=1;                          //data mode
    rw=0;                          //write
    en=1;
    en=0;                          //high to low latch 
    }
    

    void transmit_byte(unsigned char  byte)       // TRANSIMITT SERIAL DATA
    {
    SBUF=byte;                                  //TRANSMIT DATA FROM  BYTE TO SBUF 
    while(!TI);                                   // WAIT TILL DTA GET TRANSMITTED 
    TI=0;
    }
    
    void TRANSMIT(unsigned char *string)
    {
    while(*string)
    transmit_byte(*string++);
    }
          
    unsigned char SCI_ReceiveByte( void )
    {                                            // RECIVING SERIAL DATA
    unsigned char byte;
    //    RI=0;
    while(RI!=1);                                // WAIT TILL RI IS HIGH
    byte = SBUF;                                //RECIVE DATA FROM SBUF TO BYTE
    RI=0;
    return byte;                                 // RETURN THE DATA
    }

Bluetooth Interface PIC Schematic:



Bluetooth Interface PIC Code:

/*
 * Project name:
    BLUETOOTH MODULE
 * Copyright
     (c) Researchdesignlab.com
 * Test configuration:
     MCU:             PIC16F877A
     Dev.Board:       PIC
     Oscillator:      20.0 MHz
     Software:  mikroC PRO for PIC v 4.6

*/



// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

unsigned char uart_rd,i;

void main() {


  Lcd_Init();                        // Initialize LCD

  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off

  UART1_Init(9600);               // Initialize UART module at 9600 bps
  Delay_ms(100);                  // Wait for UART module to stabilize

   UART1_Write_Text("MC 1");       // TRANSMIT "MC 1" TO   bluetooth

  
  while(1)
  {

   if (UART1_Data_Ready()) {     // If data is received,
      uart_rd = UART1_Read();     // read the received data FROM   bluetooth
      Lcd_Chr_Cp(uart_rd);       // and send data lcd
      i++;
      }

    /*
   
    
     */
    
  }
  
 }



Bluetooth module Features:

  • 5V power operation.
  • Supports baud rates from 1200 to 1382400 bps (default is 9600 bps)
  • UART interface
  • TTL output.
  • Built in antenna
  • 10 meters range
  • Easy to use
  • Minimum External Components
  •  Status LEDs
  • High quality PCB FR4 Grade with FPT Certified.

For Sample Codes , Schematics : CLICK HERE