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");
}
}
Previous article:C51 Programming 24-Application (ESP-01S WIFI module and computer communication)
Next article:C51 Programming 22-Application (Decoding Infrared Remote Control)
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- ASML predicts that its revenue in 2030 will exceed 457 billion yuan! Gross profit margin 56-60%
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- Single-phase full-bridge inverter circuit
- Experiment of controlling relay with MF RC522 RF card based on Arduino UNO R3
- Problems in transplanting UCOSII to 51 MCU
- LiChuang Mall is right and confident in its mistakes. Everyone should be careful and don't be cheated.
- For C5000, how to boot a program larger than 48K?
- Qorvo's technical highlights on WiFi 6
- How to locate RFID based on wireless network
- Regarding the issue of using NPN transistors for high voltage conversion output.
- CircuitPython Icicle Drip Code
- Micro ESP32-S2 Development Board microS2