当前位置:网站首页>51 communicates with the Bluetooth module, and 51 drives the Bluetooth app to light up

51 communicates with the Bluetooth module, and 51 drives the Bluetooth app to light up

2022-07-08 00:22:00 Youxin Electronics

Introduction of Bluetooth module

This experiment uses JDY31 Bluetooth module ,JDY-31 Bluetooth Based on Bluetooth 3.0 SPP Design ,
 Insert picture description here

Click on the picture to buy

Bluetooth module is serial communication , SCM also has serial communication , We only need to configure it to realize the communication between MCU and Bluetooth , Even more functions , This article is about just learning 51 It needs a lot of help to make a Bluetooth Car

Serial port configuration

STC89C52 Of UART There are four working modes :
Pattern 0: Synchronous shift register
Pattern 1:8 position UART, The baud rate is variable ( Commonly used )
Pattern 2:9 position UART, Baud rate is fixed
Pattern 3:9 position UART, The baud rate is variable
This experiment is configured with mode 1; 8 Bit auto reassembly

The program configuration of specific programs can be assisted by STC Official downloader , The downloader comes with a baud rate calculator , Just generate the copy code according to the following figure
 Insert picture description here
The default baud rate of Bluetooth module is 9600, So here we have to choose 4800 Double speed

Main code

Serial port initialization code :89C52 Single chip microcomputer doesn't have AUXR, You can get rid of these two sentences , And on again EA and ES interrupt

/** * @brief  Serial initialization [email protected] * @param  nothing  * @retval  nothing  */
void UART_Init()		
{
    
	SCON=0x50; 			//8 Bit data , Variable baud rate 
	PCON |=0x80; 		// Enable baud rate multiplier bit SMOD 
	TMOD &= 0x0F;		// Set timer mode 
	TMOD |= 0x20;		// Set timer mode 
	TL1 = 0xFA;		// Set the initial timing value 
	TH1 = 0xFA;		// Set time overload value 
	ET1 = 0;		// Disable timer %d interrupt 
	TR1 = 1;		// Timer 1 Start timing 
	EA=1;           // Open total interrupt 
	ES=1;			// Enable serial port interrupt 
}

Serial port sends data :SBUF Serial port data buffer register ,

**
   * @brief				 Serial port sends a byte of data 
   * @param				Byte 	 To send a byte of data 
   * @retval			 nothing 
   */
void UART_SendByte(unsigned char Byte)
{
    
	SBUF=Byte;
	while(TI==0);
	TI=0;
}

Main code

void main ()
{
    
	UART_Init();  			 // Serial initialization 
	while(1)
	{
    		
	}
}

void UART_Routine() interrupt 4    // Serial port interrupt number 
{
    
	if(RI==1)
	{
    
		P1=SBUF;			   // Receive data control P1
		RI=0;                  // Set the serial port receiving register to zero 
	}	
}

Experimental wiring

Do not connect Bluetooth when downloading SCM , Download the program before connecting , Otherwise the download will fail

Single chip microcomputer Bluetooth module
5VVCC
P3.0TX
P3.1RX
GNDGND

mobile phone APP To configure

1, Download at mobile store SPP Bluetooth serial port , This Bluetooth module Apple phone is not available ,
2, It opens at APP Upper right corner Connect the corresponding Bluetooth
3, Customize a button in the place of switch , The configuration is as follows :
 Insert picture description here
After configuration, you can start operation

Experimental phenomena

 Insert picture description here

summary

The data transmitted by Bluetooth exists SBUF In the register , All kinds of operation control can be carried out by processing the received data , This article is equivalent to a serial communication template , If you need a complete project, you can leave a message below

原网站

版权声明
本文为[Youxin Electronics]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/189/202207072217134619.html