⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lmx9820.c

📁 the code connect Atmega 32 uC to 1 module Bluetooth ARF32
💻 C
字号:
#include "LMX9820.h"
#include "uart.c"
/*----Ham Reset Module---*/
void Reset_Module(void)
	{
		/*Delay it nhat 1s truoc khi thuc hien lenh Reset*/
		_delay_ms(1000);//delay 2s
		_delay_ms(1000);
		Send_Byte_To_UART(STX);	/* Truyen byte STX-Start*/
		Send_Byte_To_UART(REQ);	/* Truyen byte Packet Type= Request*/
		Send_Byte_To_UART(RESET);	/*Truyen byte Opcode=Reset*/
		Send_Byte_To_UART(0x00);	/*Truyen 2 byte Data Length=0x0000*/
		Send_Byte_To_UART(0x00);
		Send_Byte_To_UART(0x78);	/*Truyen Data=Null*/
		Send_Byte_To_UART(ETX);		/*Truyen Byte ETX: End*/
	}
//-----------------------------------------------------------------------
/*---Ham nhan cac lenh tra ve tu Module toi Vi dieu khien---*/
/*---Ham tra ve gia tri TRUE-->Truyen khong co loi
	 Ham tra ve gia tri FALSE-->Truyen co loi*/
unsigned char Get_Data_From_Module(void )	
	{
		int i=0, j;
		Receive_Cmd[0]=Receive_Byte_From_UART(); 	/*Nhan byte STX*/
		if (Receive_Cmd[0] != STX) return FALSE; 	/*Neu byte dau tien ko phai STX-->False*/	
		Receive_Cmd[1] = Receive_Byte_From_UART(); 	//Packet 
		Receive_Cmd[2] = Receive_Byte_From_UART(); 	//op-code
		Receive_Cmd[3] = Receive_Byte_From_UART(); 	//2 byte Data length
		Receive_Cmd[4] = Receive_Byte_From_UART();

		for (i=0,j=5; i<Receive_Cmd[3]+2; i++, j++)	//Doc tiep cac byte Data va 1 byte ETX
			Receive_Cmd[j] = Receive_Byte_From_UART();
		
		/*Neu byte cuoi cung khac ETX hoac byte dau tien khac STX-->FALSE*/
		if ((Receive_Cmd[--j] != ETX) || (Receive_Cmd[0] != STX)) return FALSE;
		else return TRUE;
	}	
//----------------------------------------------------------------------------------
/*---Ham tim kiem cac thiet bi xung quanh---*/
void Search_Device(void)
	{
		Send_Byte_To_UART(STX);		//byte Start STX
		Send_Byte_To_UART(REQ);		//packet type:Request
		Send_Byte_To_UART(GAP_INQUIRY);	//Opcode
		Send_Byte_To_UART(0x03);	// 2byte data length=0x0003
		Send_Byte_To_UART(0x00);
		Send_Byte_To_UART(0x55);	//Check sum
		Send_Byte_To_UART(0x0A);	//Duration=10s
		Send_Byte_To_UART(0x00);	//Unlimited	responses
		Send_Byte_To_UART(0x00);	//General inquiry
		Send_Byte_To_UART(ETX);		//byte End ETX
	}

//------------------------------------------------------------------------------------
/*---Ham tra ve thong tin cac thiet bi tim kiem duoc
	Thong tin bao gom Address, IP(device class)---*/
	
void Get_Device_Found(unsigned char *bd_addr, unsigned char *bd_ip)
	{
		bd_addr[0] = Receive_Cmd[6];
		bd_addr[1] = Receive_Cmd[7];
		bd_addr[2] = Receive_Cmd[8];
		bd_addr[3] = Receive_Cmd[9];
		bd_addr[4] = Receive_Cmd[10];
		bd_addr[5] = Receive_Cmd[11];
		
		bd_ip[0] = Receive_Cmd[12];
		bd_ip[1] = Receive_Cmd[13];
		bd_ip[2] = Receive_Cmd[13];
	}
//--------------------------------------------------------------------------------------
/*---Ham Confirm qua trinh tim kiem cac thiet bi xung quanh---*/
unsigned char Search_Confirm(void)
	{
		if(Receive_Cmd[6]==ERROR_OK) return TRUE;	//Status=0x00 thi OK
		else return FALSE;
	}
//--------------------------------------------------------------------------------------	
/*---Ham Create SDAP Connection---*/
void Create_SDAP_Connection(unsigned char *bd_addr)
	{
		Send_Byte_To_UART(STX);				//Start
		Send_Byte_To_UART(REQ);				//Packet Type
		Send_Byte_To_UART(SDAP_CONNECT);	//Opcode
		Send_Byte_To_UART(0x06);			//2byte data length=0x0006
		Send_Byte_To_UART(0x00);
		Send_Byte_To_UART(0x8A);			//Check sum
		for(int i=0; i<=5; i++)			//Gui 6 byte  Bluetooth Address
			{Send_Byte_To_UART(bd_addr[i]);}
		Send_Byte_To_UART(ETX);				//Ket thuc ETX
	}
//-----------------------------------------------------------------------------------------
/*---Ham SDAP Confirm---*/
unsigned char SDAP_Confirm(void)
	{
		if(Receive_Cmd[6]==ERROR_OK) return TRUE;	//Status=0x00 thi OK
		else return FALSE;
	}
//----------------------------------------------------------------------------------------
/*----Ham SDAP Service Browse-----*/
void SDAP_Service_Browse(void)
	{
		Send_Byte_To_UART(STX);		//Start
		Send_Byte_To_UART(REQ);		//PACKET TYPE=REQUEST
		Send_Byte_To_UART(SDAP_SERVICE_BROWSE);		//OPCODE=SDAP service Browse
		Send_Byte_To_UART(0x02);	//Data Length=2 Byte 0x0002
		Send_Byte_To_UART(0x00);
		Send_Byte_To_UART(0x89);	//Check Sum
		Send_Byte_To_UART(0x01);	// 2 byte UUID
		Send_Byte_To_UART(0x11);	//UUID=0x1101
		Send_Byte_To_UART(ETX);		//End
	}

//----------------------------------------------------------------------------------------
unsigned char SDAP_Service_Confirm(unsigned char *remote_port)
	{
		if (Receive_Cmd[6]==ERROR_OK)			//STATUS=OK
			{*remote_port=Receive_Cmd[12];		//Serial Port Number on Remote device
			 return TRUE;	}
		else return FALSE;
	}
//-----------------------------------------------------------------------------------------
/*---Ham SDAP Disconnect---*/
void SDAP_Disconnect(void)	
	{
		Send_Byte_To_UART(STX);			//Start
		Send_Byte_To_UART(REQ);		//PACKET TYPE
		Send_Byte_To_UART(SDAP_DISCONNECT);	//OPCODE
		Send_Byte_To_UART(0x00);		//Data length=0x0000
		Send_Byte_To_UART(0x00);
		Send_Byte_To_UART(0x85);		//Check Sum
		Send_Byte_To_UART(ETX);			//End
	}
//-----------------------------------------------------------------------------------------
/*---Ham SDAP Disconnect Confirm---*/
unsigned char SDAP_Disconnect_Confirm(void)
	{	if (Receive_Cmd[6]=ERROR_OK) {return TRUE;}
		else {return FALSE;}
	}
//------------------------------------------------------------------------------------------	
	
/*---Ham Establish SPP Connection---*/
void Create_SPP_Link(unsigned char *bd_addr,unsigned char local_port, unsigned char remote_port)	
	{
		Send_Byte_To_UART(STX);			//Start
		Send_Byte_To_UART(REQ);		//PACKET TYPE
		Send_Byte_To_UART(SPP_ESTABLISH_LINK);	//OPCODE
		Send_Byte_To_UART(0x08);		//Data Length=0x0008
		Send_Byte_To_UART(0x00);
		Send_Byte_To_UART(0x64);		//Check Sum
		Send_Byte_To_UART(local_port);
		
		for(int i=0; i<=5; i++)
		Send_Byte_To_UART(bd_addr[i]);
		
		Send_Byte_To_UART(remote_port);
		Send_Byte_To_UART(ETX);
	}
//-----------------------------------------------------------------------------------------------
/*---Ham Create SPP Link Confirm---*/
unsigned char SPP_Link_Confirm(unsigned char *local_port)
	{
		*local_port = Receive_Cmd[7];
		if (Receive_Cmd[6]=ERROR_OK){return TRUE;}
		else {return FALSE;}
	}
//-----------------------------------------------------------------------------------------------
/*---Ham Establish SPP Link Indication---*/
unsigned char SPP_Link_Indication(unsigned char *remote_addr, unsigned char *local_port, unsigned char *remote_port)
	{	
		for(int i=0; i<=5;i++)
			remote_addr[i]=Receive_Cmd[7+i];
		*local_port = Receive_Cmd[13];
		*remote_port = Receive_Cmd[14];
		if(Receive_Cmd[6]==ERROR_OK) return TRUE;
		else return FALSE;
	}

//-----------------------------------------------------------------------------------------------
/*---Ham Send Data o che do Master Command Mode----*/
void Send_Data_To_Remote(unsigned char local_port,unsigned char data_length,char *data)
	{
		Send_Byte_To_UART(STX);					//Start
		Send_Byte_To_UART(REQ);					//Packet Type
		Send_Byte_To_UART(SPP_SEND_DATA);		//OPCODE
		Send_Byte_To_UART(data_length+3);		//2 byte payload length
		Send_Byte_To_UART(0x00);
		Send_Byte_To_UART(0x64+data_length);	//Check Sum
		Send_Byte_To_UART(local_port);
		Send_Byte_To_UART(data_length);
		Send_Byte_To_UART(0x00);
		
		for(int i=0;i<data_length;i++)			//Du lieu can gui
			Send_Byte_To_UART(data[i]);
		Send_Byte_To_UART(ETX);					//End
	}
//-----------------------------------------------------------------------------------------------
/*---Ham Release Link--*/
void Release_Link(unsigned char local_port)
	{
		Send_Byte_To_UART(STX);					//Start
		Send_Byte_To_UART(REQ);					//Packet Type
		Send_Byte_To_UART(SPP_RELEASE_LINK);	//OPCODE
		Send_Byte_To_UART(0x01);				//Data length =0x0001
		Send_Byte_To_UART(0x00);
		Send_Byte_To_UART(0x60);				//Check sum
		Send_Byte_To_UART(local_port);			//Release Link at RFComm Port indicated by local_port
		Send_Byte_To_UART(ETX);
	}
	
//---------------------------------------------------------------------------------------	
/*---Ham Release Link Confirm---*/
unsigned char Release_Link_Confirm(void)
	{	
		if (Receive_Cmd[6]=ERROR_OK){return (TRUE);}
		else {return FALSE;}
	}
//--------------------------------------------------------------------------------------
/*---Ham Release link indication---*/
unsigned char Release_Link_Indication(unsigned char *local_port)
	{
		*local_port=Receive_Cmd[7];
		if(Receive_Cmd[6]==0x00) return TRUE;
		else return FALSE;
	}
/*--------------------------------------------------------------------------------------*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -