void controlClibra(void) { static u8 i; uint16_t sum[4]={0,0,0,0}; static int8_t lednum=1; static int8_t clibrasumNum = 20; if((ClibraFlag == FAIL))//校准失败 { for(i=0;i<clibrasumNum;i++) { #ifdef AMERICAN_RC_MODE sum[0] += 1000 + (1000 - (1000*Get_Adc_Average(3,15))/4096); sum[1] += 1000 + (1000*Get_Adc_Average(1,15))/4096; #else sum[0] += 1000 + (1000*Get_Adc_Average(1,15))/4096; sum[1] += 1000 + (1000 - (1000*Get_Adc_Average(3,15))/4096); #endif sum[2] += 1000 + (1000*Get_Adc_Average(0,15))/4096; sum[3] += 1000 + (1000*Get_Adc_Average(2,15))/4096; delay_ms(100); if(++lednum == led5 + 2)lednum = 2; LedSet(lednum - 1,0); LedSet(lednum ,1); } Throttle_Calibra = sum[0]/i; Pitch_Calibra = sum[1]/i; Roll_Calibra = sum[2]/i; Yaw_Calibra = sum[3]/i; // Throttle_Calibra = 1500; // Pitch_Calibra = 1500; // Roll_Calibra = 1500; // Yaw_Calibra = 1500; LoadRCdata(); //摇杆赋值 if((Throttle>=1510)||(Throttle<1490)||(Pitch>=1510)||(Pitch<=1490)||(Roll>=1510)||(Roll<=1490)||(Yaw>=1510)||(Yaw<=1490)) ClibraFlag = FAIL;//校准失败 else ClibraFlag = OK;//校准成功标志 SaveParamsToEEPROM(); LedSet(led2,0);LedSet(led3,0);LedSet(led4,0);LedSet(led5,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); }
void NRFmatching(void) { static uint32_t nTs,nT; static uint32_t writeOvertime = 2 * 1000000;// unit :us LedC_on; //led3 always on when 2.4G matching nTs = micros(); do { NRFMatched = 0; nT = micros() - nTs; if(nT >= writeOvertime){ RX_ADDRESS[4] = table.NRFaddr[4]; break; //exit when time out,and do not change original address } SetRX_Mode(); // reset RX mode write RX panel address delay_ms(4); // delay is needed after reset NRF sta = NRF_Read_Reg(NRF_READ_REG + NRFRegSTATUS); if((sta & 0x0E )== 0x00){ NRFMatched = 1; }else{ RX_ADDRESS[4] ++; //search the next RX_ADDRESS if(RX_ADDRESS[4] == 0xff ){ RX_ADDRESS[4] = 0x00; } } }while((sta & 0x0E )== 0x0E); SetRX_Mode(); // reset RX mode if((NRFMatched == 1)&&(RX_ADDRESS[4]!= table.NRFaddr[4])){ SaveParamsToEEPROM(); //write eeprom when current addr != original addr } LedC_off; // matching end }