C51 Programming 23- Application (HC 06 Bluetooth Module)

Publisher:创意航海Latest update time:2022-06-22 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

        Nowadays, mobile phones, tablets, and laptops all have built-in Bluetooth. This article will introduce how to use the HC 06 Bluetooth module on a 51 microcontroller to achieve communication between mobile phones and laptops.


        HC 06 Module


        After purchasing the HC 06 module, you need to check whether the Bluetooth module is good. Use a serial cable to connect it to the HC 06 module. Send "AT" on the serial assistant on the computer, and you can receive "OK" on the serial assistant.


        Note: It is recommended to use the same serial port debugging assistant as in this article, or ask the merchant to provide a serial port assistant program for testing.


        Hardware Hookup       


        HC 06 module TXD is connected to the MCU's RXD, HC 06 module RXD is connected to the MCU's TXD;


        Functional description:


        The host computer sends commands in the serial port assistant, which are transmitted to the microcontroller through the host computer's Bluetooth, so that the host computer controls the LED and returns the received commands.


        Show results:


        Laptop Bluetooth and 51 MCU 

        Mobile Bluetooth and 51 MCU

Note:


        1: When the HC 06 module needs to be paired with the computer via Bluetooth, the "Standard Serial Port on Bluetooth Link" will appear. The default PIN connection code is "1234". By comparing the device manager before and after pairing, the added COM can be used to determine which one is Bluetooth.


        2: If there are multiple "standard serial ports on Bluetooth links" on the laptop, you can directly click the Start button to connect and see if the serial port below is opened correctly.




 


     program:


        main.c


#include "uart.h"

 

void main()

{

    Uart_Init();

    while(1) ;   

 

}

 

void Uart_irq() interrupt 4

{

  if(RI==1) //receiving is effective

   {

    Uart_GetData();

    Uart_CtrlLed();

}

 

}

 

sys.h


#include "stdio.h" //Standard library header file

 

#define ON 1

#define OFF 0

 

//---Redefine keywords---//

//#ifndef fly

#define uchar unsigned char

//#endif

 

//#ifndef uint 

#define uint unsigned int

//#endif

typedef unsigned int u16;   //declare and define the data type

typedef unsigned char u8;

 

 

void delayms(uint y);

void delayus(uint i);

 

 

void irq0Init();

 

#endif

sys.c


#include "sys.h"

 

 

 void delayms(uint y)

{

uint x;

for( ; y>0; y--)

{

for(x=110; x>0; x--);

}

}

 

 

/*******************************************************************************

* Function name: delay

* Function    : Delay function, when i=1, the delay is about 10us

*******************************************************************************/

void delayus(uint i)

{

while(i--);

}

 

 

 

 

 

uart.h


#ifndef __UART_H__

#define __UART_H__

 

#include "sys.h"

 

 

//Bit operation LED

sbit LED0 = P2^0;

sbit LED1 = P2^1;

sbit LED2 = P2^2;

sbit LED3 = P2^3;

sbit LED4 = P2^4;

sbit LED5 = P2^5;

sbit LED6 = P2^6;

sbit LED7 = P2^7;

 

extern u8 thanks[20];

 

void Uart_Init();

 

void Uart_CtrlLed();

void Uart_GetData();

void Uart_PutData(char temp);

void Uart_SendStr(char* pstr);

 

#endif

uart.c


#include "uart.h"

 

u8 tack[20]; //Define the array length to store the received data

u8 recNum = 0; //Receive the length of the data

/*******************************************************************************

* Function name: UsartInit()

* Function    : Set the serial port

* Input : None

* Output          : None

*******************************************************************************/

void Uart_Init()

{

    SCON = 0x50; //Work in mode 1, 8-bit asynchronous transmission and reception, the baud rate is controlled by the timer

    TMOD = 0x20; //Select timer 1 as baud rate generator, mode 2

    TH1 = 0xFD; //Assign initial value to the timer

    TL1  = 0xFD;

    TR1 = 1; //Start the timer

    ES = 1; //Serial port interruption enabled

    EA = 1; // Enable general interrupt

 

}

/*******************************************************************************

* Function name: Usart() interrupt 4

* Function   : Serial communication interrupt function

* Input : None

* Output          : None

*******************************************************************************/

void Uart_GetData()

{   

        tack[0]= SBUF;       

        RI = 0; // Clear the receive interrupt flag

}

 

void Uart_PutData(char temp)

{

    SBUF = temp; //Put the received data into the send register

    while(!TI); //Wait for sending data to complete

    TI=0; //Clear the send flag

}

 

 

void Uart_SendStr(char* pstr)

{

   while((*pstr)!=0)

    {

        Uart_PutData(*pstr);

        pstr++;

    }

}

 

 

void Uart_CtrlLed() {

    if(tack[0]=='1') {

        P2=0xFF;

        LED0 = 0;

        Uart_SendStr("command 1 n");

    }

 

    else if(tack[0]=='2') {

        P2=0xFF;

        LED1 = 0;

        Uart_SendStr("command 2n");

    }

    else if(tack[0]=='3') {

        P2=0xFF;

        LED2 = 0;

        Uart_SendStr("command 3n");

    }

}

Reference address:C51 Programming 23- Application (HC 06 Bluetooth Module)

Previous article:C51 Programming 24-Application (ESP-01S WIFI module and computer communication)
Next article:C51 Programming 22-Application (Decoding Infrared Remote Control)

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号