資源描述:
《【精品】MK代碼分析》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫。
1、cli();關(guān)閉總中斷檢測飛控板版木設(shè)置LED相應(yīng)管腳關(guān)閉看門狗四通道桿量置零T1MER0初始化,用于產(chǎn)??刂茪鈮焊叨扔嬈秒妷旱膒wm倍號,控制蜂鳴器鳴叫以及電機更新頻率。TTMER2初始化,用于向4017芯片輸出pwm信號,從而將1路pwm轉(zhuǎn)換成多路舵機控制信號。UART0初始化MK主串口,負(fù)責(zé)與上位機的信息交互。RC_INTT();設(shè)置ppm信號輸入引腳,設(shè)置TIMER1的輸入捕獲功能以解碼ppm信號。ADC_TNTT();初始化AD采樣,采樣信息包括三軸加速度,三軸角速度,氣壓高度,電池電量。12C_IN1T();12C總線初始化,配置I2C以控制電機
2、。sei()使能全局中斷。PARAMSET_TNTT();設(shè)置參數(shù),將參數(shù)讀入內(nèi)存屮的ParamSeto檢查加速應(yīng)計是否校準(zhǔn)。檢杳是否開啟高度控制,若開啟則校準(zhǔn)高度計偏置。SetNeutral();//NeutralReadingWhile(1){UpdateMotor標(biāo)志是否更新電機狀態(tài){MotorControl();SendMotorData();}PcAccess標(biāo)志是否有上位機請求信息I2cTimeout標(biāo)志I2C是否超時需報錯S10_DEBUG標(biāo)志是否在串口調(diào)試{Transmit.TxD3t8();ProcessRxData();}檢測電池電量。主函
3、數(shù)屮的關(guān)鍵過程:voidMotorControl(void)通過控制率確定電機控制量voidSendMotorData(void)發(fā)送控制量給電機voidTransmitTxData(void)負(fù)責(zé)根據(jù)串口接收的指令要求發(fā)送相應(yīng)數(shù)據(jù)voidProcessRxData(void)處理串口接收到的數(shù)據(jù),給各種事件標(biāo)/忐置位串口協(xié)議Protocol?Theprotocolisbasedonindividualserialdataframesthatareorganizedasshowninthefollowingtable?Start-BytAddresID~Byt
4、nData-BytesCRC-ByteCRC-ByteStop-BytesByteecoded12e,岸'『+'V','DAddr'etcz/modified-base64〃variablevariableCommands?TheCommandsbasedonthedataframesabovearelistedhereThestructsusedbelowaredefinedinuart?h?Forimplementationdetails,seeuart.c.Addressesaredefinedasfollows:#defineFC_ADDRESS1(b
5、)#defineNC_ADDRESS2(c)#defineMK3MAG_ADDRESS3(d)#defineBL_CTRL_ADDRESS5(f)CommonCommandsDescriptionIDReceivedAddres丁“DataIDsAddressSentDataLabelsoftheanalogvaluesintheDebugDataStruet(see'd')aAnyAddru8IndexofAnalogChannel,ASlaveAddru8Index,char[16]labeltextExternControl'bAnyAddrExtern
6、ControlStructSlaveAddrunsignedchar,echoofExternControl.FrameasconfirmationRequestdisplay'hAnyAddru8RemoteKey,u8AutoScndlntcrva1,HSlaveAddrchar[80]DisplayTextRequestdisplay,1AnyAddru8Menultcm,LSlaveAddru8MenuItem,u8MaxMenuItem,char[80]DisplayTextVersionRequestVAnyAddrnone,vSlaveAddrV
7、ersionInfoStructDebugRequest'dAnyAddru8AutoSendlnterva1.Valueismultipliedby10inreceiverandthenusedasmilliseconds.Subsciptionneedstoberenewedevery4s.,DSlaveAddrDebugDataStructReset,RAnyAddrnone————GetExtcrnalControlgAnyAddrnone,GSlaveAddrExtcrnControlStrcutFlight-CtrlDescriptionRecei
8、vedbyFCAddreIDDataI