Exemple #1
0
//轮询蓝牙模块所有可能的波特率,获取当前波特率
//并且配置其波特率为115200,
u32 BT_Scan_Buad(void)
{
	//蓝牙波特率率表,将9600(默认波特率)和230400(hm-06遗留bug)放到最前
	//115200(将要配置的波特率)放到最后
	static u32 bandsel[9] = {230400,9600,1200,2400,4800,19200,38400,57600,115200};
	u8 i;

	for(i=0; i<9; i++) {
		UART1_init(SysClock,bandsel[i]);
		Uart1SendaBTCmd(ATcmdAsk);
		if(CmdJudgement(ATcmdAnswer) == true) {
			Q_printf("BT Scan %d\r\n", bandsel[i]);
			return bandsel[i];
		}
	}

	return 0;
}
/********************************************
              写蓝牙参数函数 (Write Bluetooth function parameters)
********************************************/
void BT_ATcmdWrite(void)
{
	static	u32 BT_CurBaud;

	BT_CurBaud = BT_CurBaud_Get();
	if((BT_CurBaud == BT_BAUD_Set))  BTstate = BThavewrote;//检测到蓝牙当前的波特率和设定值不同,就写入设定值 //Bluetooth current baud rate detection and setting values are different, the set value is written
	else 				BTstate = BTneedwrite;

				if(BTstate == BTneedwrite)
					{
						LedA_off;LedB_off;LedC_off;LedD_off;
						UART1_init(SysClock,BT_CurBaud);//以当前波特率重新初始化串口 //In the current re-initialize the serial port baud rate
						/*开始配置蓝牙设备名,pin码,波特率*/ //Begin configuring the Bluetooth device name, pin code, baud rate
						Uart1SendaBTCmd(ATcmdAsk);
						//printf("\r\n与蓝牙通信中...\r\n"); //Bluetooth Communication
								if(CmdJudgement(ATcmdAnswer) == true)//有蓝牙返回,才往下写指令 //Bluetooth returned, only to write down instructions
									{
										Uart1SendaBTCmd(ATcmdNameAsk);
											if(CmdJudgement(ATcmdNameAnswer) == false)  {Uart1SendaBTCmd(ATcmdNameSet);LedA_off;LedB_on;LedC_off;LedD_on; }   
												
											else ;
										Uart1SendaBTCmd(ATcmdCodeAsk);
											if(CmdJudgement(ATcmdCodeAnswer) == false) {Uart1SendaBTCmd(ATcmdCodeSet); LedA_on;LedB_off;LedC_on;LedD_off; }
												 
											else ;
										Uart1SendaBTCmd(ATcmdBaudAsk);
											if(CmdJudgement(ATcmdBaudAnswer) == false) {
																																	Uart1SendaBTCmd(ATcmdBaudSet);
																																	LedA_off;LedB_on;LedC_off;LedD_on;												
																																	BTstate = BThavewrote;
																																	SaveParamsToEEPROM();
																																	LedA_on;LedB_on;LedC_on;LedD_on;
																																	delay_ms(1000);
																																	LedA_off;LedB_off;LedC_off;LedD_off;
																																	}//最后修改波特率,并写入EEPROM //Last Modified baud rate, and write EEPROM
														
											else BTstate = BTneedwrite;
									
									}
								else  {BTstate = BTneedwrite; printf("\r\n与蓝牙通信失败\r\n");}  //Bluetooth communication and failure
					}
					else ;
			UART1_init(SysClock,BT_BAUD_Set);
}
u32 BT_CurBaud_Get(void)
{
	static u32 bandsel[8] = {1200,2400,4800,9600,19200,38400,57600,115200};//蓝牙波特率率表
  u8 i;

		BT_on();        //开蓝牙 //Open Bluetooth
    delay_ms(500); //等待蓝牙稳定 //Wait Bluetooth stable
		/**确定当前蓝牙波特率**/ //Bluetooth determine the current baud rate
			for(i=0;i<8;i++)
			{
				UART1_init(SysClock,bandsel[i]); 
				Uart1SendaBTCmd(ATcmdAsk);
				if(CmdJudgement(ATcmdAnswer) == true)
				{
				  //printf("\r\nHM-06 baud -->%d\r\n",bandsel[i]);
					break;//得到当前波特率为Bandsel[i] //Get the current baud rate Bandsel[i]
				}
			}
	return bandsel[i];
}
Exemple #4
0
/********************************************
              写蓝牙参数函数
********************************************/
void BT_ATcmdWrite(void)
{
	u8 i;
	static	u32 BT_CurBaud;
	static u32 bandsel[9] = {230400,9600,1200,2400,4800,19200,38400,57600,115200};

	Uart1SendaBTCmd(ATcmdAsk);
	if(CmdJudgement(ATcmdAnswer) == true) {
		//HM-11模块已经是115200波特率,无需配置
		return ;
	}

	Q_printf("BT baund check and init begin. Q_printf is useless.\r\n\r\n");

	BT_CurBaud = BT_Scan_Buad();

	if (BT_CurBaud == 0) {
		Q_printf("BT scan buad failed!\r\n");
		goto quit;
	
	}

	//首先检测蓝牙模块的串口是否已经配置为115200
	if(BT_CurBaud != BT_BAUD_Set) {

		//蓝牙模块需要修改其名字,波特率

		//修改蓝牙的名字为Crazepony
		Uart1SendaBTCmd(ATcmdNameSet);

		//修改蓝牙波特率为115200
		Uart1SendaBTCmd(ATcmdBaudSet);

		//LED闪烁表示原来蓝牙模块是哪个波特率
		for(i=0; i<9; i++) {
			LedA_on;
			LedB_on;
			LedC_on;
			LedD_on;
			delay_ms(1000);
			LedA_off;
			LedB_off;
			LedC_off;
			LedD_off;
			delay_ms(1000);

			if(BT_CurBaud == bandsel[i]) {
				break;
			}
		}
	} else {
		//已经是115200,可以直接通信
		Q_printf("BT module baud is 115200 okay\r\n");
	}
quit:
	//最终STM32的UART波特率设置回115200
	UART1_init(SysClock, BT_BAUD_Set);

	Q_printf("\r\nBT baund check and init end.\r\n");

}