Browse Source

更新

1.代码功能分类 定义分类
2.相关功能添加
3.保护添加 待验证
4.代码格式化
avery 5 months ago
parent
commit
2f8a7b22c0

+ 92 - 262
KeilC51/Fortior.uvopt

@@ -12,7 +12,7 @@
     <lExt>*.lib</lExt>
     <tExt>*.txt; *.h; *.inc; *.md</tExt>
     <pExt>*.plm</pExt>
-    <CppX>*.cpp</CppX>
+    <CppX>*.cpp; *.cc; *.cxx</CppX>
     <nMigrate>0</nMigrate>
   </Extensions>
 
@@ -28,7 +28,7 @@
     <TargetOption>
       <CLK51>24000000</CLK51>
       <OPTTT>
-        <gFlags>1</gFlags>
+        <gFlags>0</gFlags>
         <BeepAtEnd>1</BeepAtEnd>
         <RunSim>1</RunSim>
         <RunTarget>0</RunTarget>
@@ -133,256 +133,38 @@
         <Ww>
           <count>0</count>
           <WinNumber>1</WinNumber>
-          <ItemText>Uart,0x10</ItemText>
+          <ItemText>inputCapture,0x0A</ItemText>
         </Ww>
         <Ww>
           <count>1</count>
           <WinNumber>1</WinNumber>
-          <ItemText>RealTempR.LTmepSensorADC,0x0A</ItemText>
+          <ItemText>FaultSource</ItemText>
         </Ww>
         <Ww>
           <count>2</count>
           <WinNumber>1</WinNumber>
-          <ItemText>mcFaultSource,0x0A</ItemText>
+          <ItemText>faultCheck,0x0A</ItemText>
         </Ww>
         <Ww>
           <count>3</count>
           <WinNumber>1</WinNumber>
-          <ItemText>mcState,0x0A</ItemText>
+          <ItemText>loopCtrl,0x0A</ItemText>
         </Ww>
         <Ww>
           <count>4</count>
           <WinNumber>1</WinNumber>
-          <ItemText>mcFan,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>5</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>mcFocCtrl,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>6</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>Uart.L_TargetTemp_C,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>7</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>RealTempR.LTmepSensor_C,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>8</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>Uart.LTmepSensor,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>9</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>Uart.RTmepSensor,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>10</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>Uart.T_DATA[5],0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>11</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>fault,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>12</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>mcPowerLimit,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>13</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>mcVoltagePrtc,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>14</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>Uart.VoltageProtect,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>15</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>Uart.ONTIME</ItemText>
-        </Ww>
-        <Ww>
-          <count>16</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>RealTempR,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>17</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>mcCurOffset,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>18</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>mcFocCtrl.State_Count,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>19</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>mcFocCtrl.RunStateCnt,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>20</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>mcRefRamp,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>21</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>vsp,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>22</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>k,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>23</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>mcFan.Output,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>24</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>mcVoltagePrtc,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>25</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>mcFocCtrl.mcDcbusFlt,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>26</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>mcFocCtrl.EMFsquare,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>27</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>mcFocCtrl.SpeedFlt,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>28</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>mcFocCtrl.State_Count,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>29</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>Restart.LP_DelayTcntc,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>30</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>Restart.LP_DelayTcnt,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>31</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>Restart.LP_DelayTcntcC</ItemText>
-        </Ww>
-        <Ww>
-          <count>32</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>mcFan.IbusLpf,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>33</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>k,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>34</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>mcFan.Output,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>35</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>mcFan.VoltVref,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>36</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>mcFan.ADCFedLpf,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>37</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>mcFan.VoltNegFed,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>38</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>mcFan.VoltPosiFed,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>39</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>testflag</ItemText>
-        </Ww>
-        <Ww>
-          <count>40</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>mcFan.IbusFlt,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>41</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>mcFan.IbusLpf,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>42</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>mcFocCtrl.FANONOFF</ItemText>
-        </Ww>
-        <Ww>
-          <count>43</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>mcFan.VoltNegFed,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>44</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>mcFan.VoltPosiFed,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>45</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>mcFan.ADCFedLpf,0x0A</ItemText>
-        </Ww>
-        <Ww>
-          <count>46</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>Restart.LRS_DelayTcnt,0x0A</ItemText>
+          <ItemText>estData</ItemText>
         </Ww>
       </WatchWindow1>
-      <MemoryWindow1>
-        <Mm>
-          <WinNumber>1</WinNumber>
-          <SubType>0</SubType>
-          <ItemText>Uart.F4TempCompensation</ItemText>
-          <AccSizeX>0</AccSizeX>
-        </Mm>
-      </MemoryWindow1>
       <Tracepoint>
         <THDelay>0</THDelay>
       </Tracepoint>
       <DebugFlag>
         <trace>0</trace>
         <periodic>0</periodic>
-        <aLwin>0</aLwin>
+        <aLwin>1</aLwin>
         <aCover>0</aCover>
-        <aSer1>1</aSer1>
+        <aSer1>0</aSer1>
         <aSer2>0</aSer2>
         <aPa>0</aPa>
         <viewmode>1</viewmode>
@@ -428,12 +210,12 @@
     <File>
       <GroupNumber>1</GroupNumber>
       <FileNumber>1</FileNumber>
-      <FileType>5</FileType>
+      <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\User\include\Customer.h</PathWithFileName>
-      <FilenameWithoutPath>Customer.h</FilenameWithoutPath>
+      <PathWithFileName>..\User\Application\main.c</PathWithFileName>
+      <FilenameWithoutPath>main.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
@@ -444,8 +226,8 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\User\Application\main.c</PathWithFileName>
-      <FilenameWithoutPath>main.c</FilenameWithoutPath>
+      <PathWithFileName>..\User\Application\AddFunction.c</PathWithFileName>
+      <FilenameWithoutPath>AddFunction.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
@@ -456,8 +238,8 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\User\Application\AddFunction.c</PathWithFileName>
-      <FilenameWithoutPath>AddFunction.c</FilenameWithoutPath>
+      <PathWithFileName>..\User\Application\Interrupt.c</PathWithFileName>
+      <FilenameWithoutPath>Interrupt.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
@@ -468,8 +250,8 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\User\Application\Interrupt.c</PathWithFileName>
-      <FilenameWithoutPath>Interrupt.c</FilenameWithoutPath>
+      <PathWithFileName>..\User\Application\control.c</PathWithFileName>
+      <FilenameWithoutPath>control.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
@@ -480,8 +262,8 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\User\include\Parameter.h</PathWithFileName>
-      <FilenameWithoutPath>Parameter.h</FilenameWithoutPath>
+      <PathWithFileName>..\User\Application\customer.h</PathWithFileName>
+      <FilenameWithoutPath>customer.h</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
@@ -492,8 +274,8 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\User\include\Protect.h</PathWithFileName>
-      <FilenameWithoutPath>Protect.h</FilenameWithoutPath>
+      <PathWithFileName>..\User\Application\protect.h</PathWithFileName>
+      <FilenameWithoutPath>protect.h</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
@@ -541,6 +323,42 @@
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
+    <File>
+      <GroupNumber>2</GroupNumber>
+      <FileNumber>10</FileNumber>
+      <FileType>1</FileType>
+      <tvExp>0</tvExp>
+      <tvExpOptDlg>0</tvExpOptDlg>
+      <bDave2>0</bDave2>
+      <PathWithFileName>..\User\Function\pwm.c</PathWithFileName>
+      <FilenameWithoutPath>pwm.c</FilenameWithoutPath>
+      <RteFlg>0</RteFlg>
+      <bShared>0</bShared>
+    </File>
+    <File>
+      <GroupNumber>2</GroupNumber>
+      <FileNumber>11</FileNumber>
+      <FileType>1</FileType>
+      <tvExp>0</tvExp>
+      <tvExpOptDlg>0</tvExpOptDlg>
+      <bDave2>0</bDave2>
+      <PathWithFileName>..\User\Function\led.c</PathWithFileName>
+      <FilenameWithoutPath>led.c</FilenameWithoutPath>
+      <RteFlg>0</RteFlg>
+      <bShared>0</bShared>
+    </File>
+    <File>
+      <GroupNumber>2</GroupNumber>
+      <FileNumber>12</FileNumber>
+      <FileType>1</FileType>
+      <tvExp>0</tvExp>
+      <tvExpOptDlg>0</tvExpOptDlg>
+      <bDave2>0</bDave2>
+      <PathWithFileName>..\User\Function\protocol.c</PathWithFileName>
+      <FilenameWithoutPath>protocol.c</FilenameWithoutPath>
+      <RteFlg>0</RteFlg>
+      <bShared>0</bShared>
+    </File>
   </Group>
 
   <Group>
@@ -551,7 +369,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>3</GroupNumber>
-      <FileNumber>10</FileNumber>
+      <FileNumber>13</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -563,7 +381,7 @@
     </File>
     <File>
       <GroupNumber>3</GroupNumber>
-      <FileNumber>11</FileNumber>
+      <FileNumber>14</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -575,7 +393,7 @@
     </File>
     <File>
       <GroupNumber>3</GroupNumber>
-      <FileNumber>12</FileNumber>
+      <FileNumber>15</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -587,19 +405,7 @@
     </File>
     <File>
       <GroupNumber>3</GroupNumber>
-      <FileNumber>13</FileNumber>
-      <FileType>1</FileType>
-      <tvExp>0</tvExp>
-      <tvExpOptDlg>0</tvExpOptDlg>
-      <bDave2>0</bDave2>
-      <PathWithFileName>..\User\Hardware\CRC.c</PathWithFileName>
-      <FilenameWithoutPath>CRC.c</FilenameWithoutPath>
-      <RteFlg>0</RteFlg>
-      <bShared>0</bShared>
-    </File>
-    <File>
-      <GroupNumber>3</GroupNumber>
-      <FileNumber>14</FileNumber>
+      <FileNumber>16</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -611,7 +417,7 @@
     </File>
     <File>
       <GroupNumber>3</GroupNumber>
-      <FileNumber>15</FileNumber>
+      <FileNumber>17</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -623,7 +429,7 @@
     </File>
     <File>
       <GroupNumber>3</GroupNumber>
-      <FileNumber>16</FileNumber>
+      <FileNumber>18</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -635,7 +441,7 @@
     </File>
     <File>
       <GroupNumber>3</GroupNumber>
-      <FileNumber>17</FileNumber>
+      <FileNumber>19</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -647,7 +453,7 @@
     </File>
     <File>
       <GroupNumber>3</GroupNumber>
-      <FileNumber>18</FileNumber>
+      <FileNumber>20</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -659,7 +465,7 @@
     </File>
     <File>
       <GroupNumber>3</GroupNumber>
-      <FileNumber>19</FileNumber>
+      <FileNumber>21</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -669,6 +475,18 @@
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
+    <File>
+      <GroupNumber>3</GroupNumber>
+      <FileNumber>22</FileNumber>
+      <FileType>1</FileType>
+      <tvExp>0</tvExp>
+      <tvExpOptDlg>0</tvExpOptDlg>
+      <bDave2>0</bDave2>
+      <PathWithFileName>..\User\Hardware\DMA.c</PathWithFileName>
+      <FilenameWithoutPath>DMA.c</FilenameWithoutPath>
+      <RteFlg>0</RteFlg>
+      <bShared>0</bShared>
+    </File>
   </Group>
 
   <Group>
@@ -679,7 +497,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>4</GroupNumber>
-      <FileNumber>20</FileNumber>
+      <FileNumber>23</FileNumber>
       <FileType>2</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -689,6 +507,18 @@
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
+    <File>
+      <GroupNumber>4</GroupNumber>
+      <FileNumber>24</FileNumber>
+      <FileType>4</FileType>
+      <tvExp>0</tvExp>
+      <tvExpOptDlg>0</tvExpOptDlg>
+      <bDave2>0</bDave2>
+      <PathWithFileName>..\System\FU6502_Config.LIB</PathWithFileName>
+      <FilenameWithoutPath>FU6502_Config.LIB</FilenameWithoutPath>
+      <RteFlg>0</RteFlg>
+      <bShared>0</bShared>
+    </File>
   </Group>
 
 </ProjectOpt>

+ 35 - 15
KeilC51/Fortior.uvproj

@@ -324,7 +324,7 @@
               <MiscControls></MiscControls>
               <Define></Define>
               <Undefine></Undefine>
-              <IncludePath>..\User\Include;..\System</IncludePath>
+              <IncludePath>..\User\Include;..\System;..\User\Application;..\User\Function;..\User\Hardware</IncludePath>
             </VariousControls>
           </C51>
           <Ax51>
@@ -376,11 +376,6 @@
         <Group>
           <GroupName>Application</GroupName>
           <Files>
-            <File>
-              <FileName>Customer.h</FileName>
-              <FileType>5</FileType>
-              <FilePath>..\User\include\Customer.h</FilePath>
-            </File>
             <File>
               <FileName>main.c</FileName>
               <FileType>1</FileType>
@@ -397,14 +392,19 @@
               <FilePath>..\User\Application\Interrupt.c</FilePath>
             </File>
             <File>
-              <FileName>Parameter.h</FileName>
+              <FileName>control.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\User\Application\control.c</FilePath>
+            </File>
+            <File>
+              <FileName>customer.h</FileName>
               <FileType>5</FileType>
-              <FilePath>..\User\include\Parameter.h</FilePath>
+              <FilePath>..\User\Application\customer.h</FilePath>
             </File>
             <File>
-              <FileName>Protect.h</FileName>
+              <FileName>protect.h</FileName>
               <FileType>5</FileType>
-              <FilePath>..\User\include\Protect.h</FilePath>
+              <FilePath>..\User\Application\protect.h</FilePath>
             </File>
           </Files>
         </Group>
@@ -426,6 +426,21 @@
               <FileType>1</FileType>
               <FilePath>..\User\Function\MotorProtect.c</FilePath>
             </File>
+            <File>
+              <FileName>pwm.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\User\Function\pwm.c</FilePath>
+            </File>
+            <File>
+              <FileName>led.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\User\Function\led.c</FilePath>
+            </File>
+            <File>
+              <FileName>protocol.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\User\Function\protocol.c</FilePath>
+            </File>
           </Files>
         </Group>
         <Group>
@@ -446,11 +461,6 @@
               <FileType>1</FileType>
               <FilePath>..\User\Hardware\CMP.c</FilePath>
             </File>
-            <File>
-              <FileName>CRC.c</FileName>
-              <FileType>1</FileType>
-              <FilePath>..\User\Hardware\CRC.c</FilePath>
-            </File>
             <File>
               <FileName>DRIVER.c</FileName>
               <FileType>1</FileType>
@@ -481,6 +491,11 @@
               <FileType>1</FileType>
               <FilePath>..\User\Hardware\SMDU.c</FilePath>
             </File>
+            <File>
+              <FileName>DMA.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\User\Hardware\DMA.c</FilePath>
+            </File>
           </Files>
         </Group>
         <Group>
@@ -491,6 +506,11 @@
               <FileType>2</FileType>
               <FilePath>..\System\STARTUP_FU6805.A51</FilePath>
             </File>
+            <File>
+              <FileName>FU6502_Config.LIB</FileName>
+              <FileType>4</FileType>
+              <FilePath>..\System\FU6502_Config.LIB</FilePath>
+            </File>
           </Files>
         </Group>
       </Groups>

File diff suppressed because it is too large
+ 342 - 312
KeilC51/Listings/Fortior_FOC_FU68x5.map


BIN
System/FU6502_Config.LIB


+ 1 - 2
System/FU6522_72_MDU.h

@@ -97,8 +97,7 @@ int16 DivQ_L_MDU(uint16 Xn1, uint16 Xn0,uint16 Yn);
 int16 MULU_H_MDU(uint16 Xn0, uint16 Xn1);
 uint16 Sqrt_alpbet(int16 i_alp, int16 i_bet);
 int16 DivQ_L_MDU2(uint16 Xn1, uint16 Xn0, uint16 Yn);
-void MULS_H_MDU(int16 Xn0, int16 Xn1,uint16 xdata *lusResult32);	
-
+void MULS_H_MDU(int16 Xn0, int16 Xn1,uint16 xdata *lusResult32);
 
 #ifdef __cplusplus
 }

+ 18 - 165
User/Application/AddFunction.c

@@ -1,11 +1,7 @@
 #include <MyProject.h>
 
 
-bool  isCtrlPowOn = false;                  ///< 开关机控制
-
 FOCCTRL xdata mcFocCtrl;
-MCRAMP xdata mcRefRamp;
-int16 refRampOut = 0;
 
 /**
     @brief        对变量取16位的绝对值
@@ -13,187 +9,44 @@ int16 refRampOut = 0;
     @return       绝对值
     @date         2022-07-13
 */
-uint16 Abs_F16(int16 value)
+uint16_t Abs_F16(int16_t value)
 {
     if (value < 1)
-    {
-        return (-value);
-    }
+    { return (-value); }
     else
-    {
-        return (value);
-    }
+    { return (value); }
 }
 
-/**
-    @brief        对变量取32位的绝对值
-    @param[in]    value
-    @return       绝对值
-    @date         2022-07-13
-*/
-uint32 Abs_F32(int32 value)
-{
-    if (value < 1)
-    {
-        return (-value);
-    }
-    else
-    {
-        return (value);
-    }
-}
 
 
 
-/**
-    @brief        调速信号处理包含:开关机控制、将调速信号处理成控制目标给定信号
-    @date         2022-07-14
-*/
-void TargetRef_Process(void)
-{
-    #if (SPEED_MODE == NONEMODE)
-	isCtrlPowOn = true;  // 开机
-    mcFocCtrl.Ref = MOTOR_SPEED_MAX_RPM;
-    #elif (SPEED_MODE == PWMMODE)
-    #endif
-}
-
-/**
-    @brief        外部闭环控制函数,示例代码提供 电流环,速度环,功率环,UQ控制示例代码,可根据需要自行修改
-                 建议使用默认1ms周期运行
-    @date         2022-07-14
-*/
-void Speed_response(void)
-{
-    uint16 FOC_KSLIDE_Temp = 0;
-    uint16 FOC_EKLPFMIN_Temp = 0;
-    
-    if ((mcState == mcRun) || (mcState == mcStop))
-    {
-        switch (mcFocCtrl.CtrlMode)
-        {
-            case 0:
-            {
-                if (mcFocCtrl.SpeedFlt > MOTOR_LOOP_RPM)
-                {
-                    mcFocCtrl.Mode0HoldCnt++;
-                    
-                    if (mcFocCtrl.Mode0HoldCnt > 5)
-                    {
-                        mcFocCtrl.CtrlMode  = 1;
-                        FOC_QKP             = QKP;
-                        FOC_QKI             = QKI;
-                        FOC_DKP             = DKP;
-                        FOC_DKI             = DKI;
-                        // ClrBit(FOC_CR2,UDD);
-                        mcRefRamp.OutValue_float = S_Value(2000.0);
-                        mcFocCtrl.LoopTime = LOOP_TIME;
-                        mcFocCtrl.IqRef = FOC_IQREF;
-                        FOC_IDREF = 0;
-                        PI1_UKH = IQ_Start_CURRENT;
-                    }
-                }
-                else
-                {
-                    mcFocCtrl.Mode0HoldCnt = 0;
-                }
-            }
-            break;
-            
-            case 1:
-            {
-                mcFocCtrl.LoopTime++;
-                
-                if (mcFocCtrl.LoopTime >= LOOP_TIME)
-                {
-                    mcFocCtrl.LoopTime = 0;
-                    refRampOut = Motor_Ramp(mcFocCtrl.Ref);
-                    FOC_IQREF = HW_Zero_Calc(refRampOut - mcFocCtrl.SpeedFlt);
-                    FOC_IDREF = 0;
-                }
-            }
-            break;
-        }
-    }
-}
-
 
 
 /**
-    @brief        控制给定爬坡函数
-                 以浮点进行计算,解决整数爬坡由于精度的影响,导致爬坡结果阶梯变化
-                 函数控制周期默认为闭环控制周期,建议使用默认1ms周期运行
-    @param[in]    ref 给定目标值
-    @return       爬坡结果(int16)
+    @brief        启动ATO爬坡函数,用于静止启动时候对ATO进行爬坡,提高启动可靠性
     @date         2022-07-14
 */
-int16 Motor_Ramp(int16 ref)
+void ATORamp(void)
 {
-    mcRefRamp.RefValue = ref;
-    
-    if (mcRefRamp.OutValue_float < mcRefRamp.RefValue)
+    if (mcFocCtrl.State_Count == (ATO_RAMP_PERIOD << 2))
     {
-        if (mcRefRamp.OutValue_float + mcRefRamp.IncValue < mcRefRamp.RefValue)
-        {
-            mcRefRamp.OutValue_float += mcRefRamp.IncValue;
-        }
-        else
-        {
-            mcRefRamp.OutValue_float = mcRefRamp.RefValue;
-        }
+        FOC_EKP = OBSW_KP_GAIN_RUN1;              // 估算器里的PI的KP
+        FOC_EKI = OBSW_KI_GAIN_RUN1;              // 估算器里的PI的KI
     }
-    else if (mcRefRamp.OutValue_float > mcRefRamp.RefValue)
+    else if (mcFocCtrl.State_Count == ((ATO_RAMP_PERIOD << 1) + ATO_RAMP_PERIOD))
     {
-        if (mcRefRamp.OutValue_float - mcRefRamp.DecValue > mcRefRamp.RefValue)
-        {
-            mcRefRamp.OutValue_float -= mcRefRamp.DecValue;
-        }
-        else
-        {
-            mcRefRamp.OutValue_float = mcRefRamp.RefValue;
-        }
+        FOC_EKP = OBSW_KP_GAIN_RUN2;             // 估算器里的PI的KP
+        FOC_EKI = OBSW_KI_GAIN_RUN2;             // 估算器里的PI的KI
     }
-    else
+    else if (mcFocCtrl.State_Count == (ATO_RAMP_PERIOD << 1))
     {
-        mcRefRamp.OutValue_float = mcRefRamp.RefValue;
+        FOC_EKP = OBSW_KP_GAIN_RUN3;             // 估算器里的PI的KP
+        FOC_EKI = OBSW_KI_GAIN_RUN3;             // 估算器里的PI的KI
     }
-    
-    return (int16)mcRefRamp.OutValue_float;                                                         // 输出浮点数取整
-}
-
-
-/**
-    @brief        启动ATO爬坡函数,用于静止启动时候对ATO进行爬坡,提高启动可靠性
-    @date         2022-07-14
-*/
-void ATORamp(void)
-{
-    if (mcState == mcRun)
+    else if (mcFocCtrl.State_Count <= ATO_RAMP_PERIOD && mcFocCtrl.Flg_ATORampEnd == 0)
     {
-        if (mcFocCtrl.State_Count == (ATO_RAMP_PERIOD << 2))
-        {
-            FOC_EKP = OBSW_KP_GAIN_RUN1;              // 估算器里的PI的KP
-            FOC_EKI = OBSW_KI_GAIN_RUN1;              // 估算器里的PI的KI
-        }
-        else if (mcFocCtrl.State_Count == ((ATO_RAMP_PERIOD << 1) + ATO_RAMP_PERIOD))
-        {
-            FOC_EKP = OBSW_KP_GAIN_RUN2;             // 估算器里的PI的KP
-            FOC_EKI = OBSW_KI_GAIN_RUN2;             // 估算器里的PI的KI
-        }
-        else if (mcFocCtrl.State_Count == (ATO_RAMP_PERIOD << 1))
-        {
-            FOC_EKP = OBSW_KP_GAIN_RUN3;             // 估算器里的PI的KP
-            FOC_EKI = OBSW_KI_GAIN_RUN3;             // 估算器里的PI的KI
-        }
-        else if (mcFocCtrl.State_Count <= ATO_RAMP_PERIOD && mcFocCtrl.Flg_ATORampEnd == 0)
-        {
-            FOC_EKP = OBSW_KP_GAIN_RUN4;             // 估算器里的PI的KP
-            FOC_EKI = OBSW_KI_GAIN_RUN4;             // 估算器里的PI的KI
-            mcFocCtrl.Flg_ATORampEnd = 1;            // ATO 爬坡结束
-        }
+        FOC_EKP = OBSW_KP_GAIN_RUN4;             // 估算器里的PI的KP
+        FOC_EKI = OBSW_KI_GAIN_RUN4;             // 估算器里的PI的KI
+        mcFocCtrl.Flg_ATORampEnd = 1;            // ATO 爬坡结束
     }
 }
-void TickCycle_1ms(void)
-{
-    
-}

+ 33 - 20
User/Application/Interrupt.c

@@ -1,9 +1,8 @@
 #include <MyProject.h>
 
-extern uint8 data g_1mTick;                   ///< 1ms滴答信号,每隔1ms在SYSTICK定时器被置1,需在大循环使用处清零
-
 int16 idata Power_Currt;
 
+extern bool data TickFlag;
 
 /**
     @brief        低于预警中断与过温中断
@@ -17,7 +16,7 @@ void LVW_TSD_INT(void) interrupt 0
     {
         if (ReadBit(LVSR, LVWF))
         {
-            mcFaultSource = FaultUnderVoltageDC;
+            FaultSource = MCU_LVM;
             ClrBit(LVSR, LVWF);
         }
         
@@ -64,24 +63,45 @@ void DRV_ISR(void) interrupt 3
 
 
 /**
-    @brief        定时器3中断服务函数
-    @note         本例程中用于PWM调速信号捕获
-    @date         2022-07-14
+    @function     TIM3_INT_Handle
+    @brief        定时器2中断
+    @date         2025-12-24
 */
-void TIM3_INT(void) interrupt 9
+void TIM3_INT_Handle(void) interrupt 9
 {
     if (ReadBit(TIM3_CR1, T3IR))
-    {
-        ClrBit(TIM3_CR1, T3IR);
-    }
+    { ClrBit(TIM3_CR1, T3IR); }
     
-    if (ReadBit(TIM3_CR1, T3IP))//周期中断
+    if (ReadBit(TIM3_CR1, T3IP))
     {
+        if (!inputCapture.UpdateFlag)
+        {
+            inputCapture.TimerARR = TIM3__ARR;
+            inputCapture.TimerDR = TIM3__DR;
+            inputCapture.UpdateFlag = 1;
+        }
+        
         ClrBit(TIM3_CR1, T3IP);
     }
     
     if (ReadBit(TIM3_CR1, T3IF))
     {
+        if (!inputCapture.UpdateFlag)
+        {
+            if (GP11)
+            {
+                inputCapture.TimerARR = 60000;
+                inputCapture.TimerDR = 60000;
+            }
+            else
+            {
+                inputCapture.TimerARR = 60000;
+                inputCapture.TimerDR = 0;
+            }
+            
+            inputCapture.UpdateFlag = 1;
+        }
+        
         ClrBit(TIM3_CR1, T3IF);
     }
 }
@@ -96,7 +116,7 @@ void SYStick_INT(void) interrupt 10
 {
     if (ReadBit(DRV_SR, SYSTIF))          // SYS TICK中断
     {
-        g_1mTick = 1;
+        TickFlag = true;
         
         if (mcFocCtrl.State_Count > 0)
         { mcFocCtrl.State_Count--; }
@@ -116,14 +136,7 @@ void SYStick_INT(void) interrupt 10
 void CMP3_INT(void)  interrupt 12
 {
     if (ReadBit(CMP_SR, CMP3IF))
-    {
-        if (mcState != mcPosiCheck)
-        {
-            mcFaultSource = FaultHardOVCurrent;     // 硬件过流保护
-        }
-        
-        ClrBit(CMP_SR, CMP3IF);
-    }
+    { ClrBit(CMP_SR, CMP3IF); }
 }
 
 void UART2_INT(void)  interrupt 14

+ 78 - 0
User/Application/control.c

@@ -0,0 +1,78 @@
+/**
+    @copyright None
+    @file      control.c
+    @author    Comment Vivre
+    @date      2025-12-29
+    @brief     None
+*/
+#include <MyProject.h>
+
+Start_Test_t xdata startTest;
+/**
+    @function     Motor_Start_Test
+    @brief        电机启动测试
+    @date         2025-12-23
+*/
+void Motor_Start_Test(void)
+{
+    // 有故障 结束测试
+    if (FaultSource != SYS_NO_FAULT)
+    {
+        startTest.State = TEST_FAULT;
+        isCtrlPowOn = false;
+        startTest.RunTime = 0;
+        startTest.StopTime = 0;
+    }
+    
+    switch (startTest.State)
+    {
+        // 停止
+        case TEST_MOTOR_STOP:
+            isCtrlPowOn = false;
+            loopCtrl.TargetRef = 0;
+            
+            if (++startTest.StopTime > MOTOR_STOP_TIME)
+            {
+                startTest.StopTime = 0;
+                startTest.State = TEST_MOTOR_START;
+            }
+            
+            break;
+            
+        // 启动
+        case TEST_MOTOR_START:
+            isCtrlPowOn = true;
+            loopCtrl.TargetRef = MOTOR_START_TEST_SPEED;
+            
+            if (++startTest.RunTime > MOTOR_RUN_TIME)
+            {
+                startTest.RunTime = 0;
+                startTest.State = TEST_MOTOR_STOP;
+            }
+            
+            break;
+            
+        case TEST_FAULT:
+            break;
+    }
+}
+
+/**
+    @function     Get_Target_Ref
+    @brief        速度给定
+    @date         2025-11-01
+*/
+void Get_Target_Ref(void)
+{
+    #if (SPEED_MODE == NONEMODE)
+    isCtrlPowOn = true;
+    loopCtrl.TargetRef = _Q15(3500.0 / MOTOR_SPEED_BASE);
+    #elif (SPEED_MODE == PWMMODE)
+    Input_Capture_Calc();
+    #elif (SPEED_MODE == STARTTEST)
+    Motor_Start_Test();
+    #endif
+}
+
+
+

+ 100 - 0
User/Application/control.h

@@ -0,0 +1,100 @@
+/**
+    @copyright None
+    @file      control.h
+    @author    Comment Vivre
+    @date      2025-12-29
+    @brief     None
+*/
+#ifndef __CONTROL_H_
+#define __CONTROL_H_
+
+typedef struct
+{
+    enum
+    {
+        TEST_MOTOR_STOP = 0,
+        TEST_MOTOR_START,
+        TEST_FAULT
+    } State;                                // 当前状态
+    uint16_t RunTime;                       // 运行计时
+    uint16_t StopTime;                      // 停机计时
+    uint16_t NormalStartCount;              // 成功运行计数
+} Start_Test_t;
+
+extern Start_Test_t xdata startTest;
+
+extern bool data isCtrlPowOn;
+
+typedef struct
+{
+    enum
+    {
+        OPEN_MODE = 0,
+        CLOSE_MODE
+    } State;                                // 环路状态
+    int8_t CalcTime;                        // 环路周期
+    int16_t TargetRef;                      // 给定目标转速
+    int16_t ActualRef;                      // 实际运算目标转速
+    int16_t Inc;                            // 加速
+    int16_t Dec;                            // 减速
+} Loop_Control_t;
+extern Loop_Control_t xdata loopCtrl;
+
+typedef struct
+{
+    int16_t ActSpeedFlt;                    // 估算转速
+    int16_t ISRef;                          // PI计算电流
+    // 电流环给定电流
+    int16_t IQRef;
+    int16_t IDRef;
+    // DQ轴输出电压
+    int16_t UQFlt;
+    int16_t UDFlt;
+    // DQ轴实际电流
+    int16_t IQFlt;
+    int16_t IDFlt;
+    
+    int16_t Power;                          // 估算功率
+    uint16_t BackEMF;                       // 反电动势
+    int16_t BusCurr;                        // 估算电流
+    // ADC采集数据
+    uint16_t BusVoltage;
+    uint16_t NTCTemper;
+    uint16_t AlongVoltage;
+    uint16_t ActBusCurr;
+} Estimated_Data_t;
+extern Estimated_Data_t xdata estData;
+
+typedef enum
+{
+    SYS_READY = 0,                          // 就绪状态
+    SYS_INIT = 1,                           // 初始化
+    GET_CURR_OFFSET = 2,                    // 检测偏置电压
+    PRE_DRIVER_CHARGE = 3,                  // 预充电
+    TAIL_WIND = 4,                          // 风向检测
+    MOTOR_POSI_CHECK = 5,                   // 初始位置检测
+    MOTOR_ALIGN = 6,                        // 预定位
+    MOTOR_START = 7,                        // 电机启动
+    MOTOR_RUN = 8,                          // 电机运行
+    MOTOR_STOP = 9,                         // 电机停止
+    MOTOR_BREAK = 10,                       // 刹车
+    MOTOR_FAULT = 11                        // 故障
+} Sys_State_e;
+
+extern Sys_State_e xdata sysState;
+
+void Motor_Fault_Handle(void);
+void Get_Target_Ref(void);
+void Get_LPF_Value(void);
+void Loop_Control(void);
+
+void Motor_Control_State(void);
+
+
+#define MOTOR_START_TEST_SPEED              _Q15(2000.0/MOTOR_SPEED_BASE)
+#define MOTOR_STOP_TIME                     10000
+#define MOTOR_RUN_TIME                      30000
+
+#endif
+
+

+ 117 - 0
User/Application/customer.h

@@ -0,0 +1,117 @@
+#ifndef __CUSTOMER_H_
+#define __CUSTOMER_H_
+
+#define PWM_FREQUENCY                       (16.0)
+#define PWM_DEADTIME                        (1.0)
+#define MIN_WIND_TIME                       (PWM_DEADTIME + 0.9)
+#define DLL_TIME                            (PWM_DEADTIME + 0.3)
+
+#define PWM_LEVEL_MODE                      (HIGH_LEVEL)
+
+#define Pole_Pairs                          (4.0)
+#define RS                                  (0.135)
+#define LD                                  (0.00004)
+#define LQ                                  (0.00008)
+#define MOTOR_SPEED_BASE                    (9000.0)
+#define Ke                                  (1.8)
+
+#define IR_MODE                             (CW)
+
+#define HW_AMP_MODE                         (AMP_PGA_DUAL)
+
+#define HW_RSHUNT                           (0.01)
+#define HW_AMPGAIN                          (AMP4x)
+#define HW_ADC_VREF                         (VREF5_0)
+
+#define Shunt_Resistor_Mode                 (Single_Resistor)
+
+#define VHALF_EN                            (1)
+
+
+#define RV1                                 (47.0)
+#define RV2                                 (3.3)
+
+#define CHARGE_EN                           (0)
+#define CHARGE_DUTY                         (0.1)
+#define CHARGE_TIME                         (30)
+
+
+#define ALIGN_MOME                          (ALIGN_NOMAL)
+
+
+#define AlignRamp_Time                      (800)
+#define Align_Time1                         (200)
+#define Align_Time2                         (1000)
+#define Align_Time3                         (200)
+#define AlignAll_Time                       (AlignRamp_Time+Align_Time1+Align_Time2+Align_Time3)
+#define Align_Angle1                        _Q15(90.0/180.0)                       // 第一次预定位角度
+#define Align_Angle2                        _Q15(90.0/180.0)                        // 第二次预定位角度
+#define Angle_ADD                           (int32)6*_Q15((float)-180.0/180.0)       //旋转角度
+#define Angle_AngleK                        (float)Angle_ADD/Align_Time2            //旋转系数
+
+#define DQKP_Alignment                      _Q12(2.0)
+#define DQKI_Alignment                      _Q15(0.01)
+#define ID_Align_CURRENT                    I_Value(0.0)
+#define IQ_Align_CURRENT                    I_Value(2.0)
+
+#define Open_Start_Mode                     (Omega_Start)
+#define ID_Start_CURRENT                    I_Value(0.0)
+#define IQ_Start_CURRENT                    I_Value(5.0)
+
+
+#define ATO_BW_START                        (0.0)
+#define ATO_BW_RUN1                         (150.0)
+#define ATO_BW_RUN2                         (150.0)
+#define ATO_BW_RUN3                         (150.0)
+#define ATO_BW_RUN4                         (150.0)
+
+#define ATO_START_HOLDTIME                  (10)             ///< (ms)观测器带宽的滤波值,启动第一拍ATO持续时间
+#define ATO_RAMP_PERIOD                     (20)             ///< (ms)观测器带宽的滤波值,ATO爬坡递增 间隔时间
+
+#define MOTOR_OMEGA_RAMP_ACC                (50)               ///< omega启动的增量(每个载波递增值) 
+#define MOTOR_OMEGA_RAMP_MIN                S_Value(200.0)     ///< (RPM) omega启动的最小切换转速
+#define MOTOR_OMEGA_RAMP_END                S_Value(300.0)     ///< (RPM) omega启动的限制转速
+#define MOTOR_SPEED_SMOMIN_RPM              (600.0)            ///< (RPM) SMO运行最小转速影响启动
+
+#define SPD_BW                              (20.0)            ///< 速度带宽的滤波值,经典值为5.0-40.0
+#define MOTOR_LOOP_RPM                      S_Value(400.0)    ///< (RPM) 启动后电流环切入外部环路转速阈值
+
+#define MOTOR_SPEED_MIN_RPM                 S_Value(1800.0)
+#define MOTOR_SPEED_MAX_RPM                 S_Value(3500.0)
+#define MOTOR_SPEED_STOP_RPM                S_Value(200.0)
+
+
+#define DKPStart                            _Q12(4.5)
+#define DKIStart                            _Q15(0.03)
+#define QKPStart                            DKPStart
+#define QKIStart                            DKIStart
+
+#define DKP                                 _Q12(3.5)
+#define DKI                                 _Q15(0.005)
+#define QKP                                 DKP
+#define QKI                                 DKI
+
+// DQ轴输出限幅
+#define DOUTMAX                             _Q15(0.99)
+#define DOUTMIN                             _Q15(-0.99)
+#define QOUTMAX                             _Q15(0.99)
+#define QOUTMIN                             _Q15(-0.99)
+
+
+#define SPEED_LOOP_TIME                     (1)
+
+#define SKP                                 _Q12(2.5)
+#define SKI                                 _Q15(0.01)
+#define SOUTMAX                             I_Value(20.0)
+#define SOUTMIN                             I_Value(0.01)
+
+#define MOTOR_SPEED_INC                     (1) // 约为274RPM每秒
+#define MOTOR_SPEED_DEC                     (1)
+
+
+#define SPEED_MODE                          (NONEMODE)
+
+#define EstimateAlgorithm                   (AO)
+#define OverModulation                      (0)
+
+#endif

+ 47 - 60
User/Application/main.c

@@ -1,28 +1,19 @@
 #include <MyProject.h>
 
-uint8 data g_1mTick = 0;
+bool data isCtrlPowOn = false;
+bool data TickFlag = false;
+
+
+Loop_Control_t xdata loopCtrl;          // 环路控制
+Estimated_Data_t xdata estData;         // 数据采集
+
+
+Fault_Check_t xdata faultCheck;
+
+Fault_Type_e xdata FaultSource;
+Sys_State_e xdata sysState;
 
 
-/**
-    @brief        参考电压,偏置电压配置
-    @date         2022-07-14
-*/
-void VREFConfigInit(void)
-{
-    // 配置参考电压为5V
-    SetReg(VREF_VHALF_CR, VRVSEL1 | VRVSEL0, VRVSEL0);
-    // 配置半电压为2.5V
-    SetBit(VREF_VHALF_CR, VHALFSEL1 | VHALFSEL0);
-    #if (VREF_OUT_EN)
-    SetBit(P3_AN, PIN5);
-    SetBit(P3_OE, PIN5);
-    #endif
-    #if (VHALF_EN)
-    SetBit(P3_AN, P32);
-    SetBit(VREF_VHALF_CR, VHALFEN);
-    #endif
-    SetBit(VREF_VHALF_CR, VREFEN);
-}
 /**
     @brief     硬件初始化,初始化需要使用的硬件设备配置,FOC必须配置的是运放电压、运放初始化、ADC初始化、Driver初始化
               其他的可根据实际需求加。
@@ -30,16 +21,19 @@ void VREFConfigInit(void)
 */
 void HardwareInit(void)
 {
-    VREFConfigInit();
     CMP3_Init();
     GPIO_Init();
     ADC_Init();
-    Driver_Init();
     AMP_Init();
-    Timer3_Init();
-	
+    // 附加配置 处理程序丢失问题 !>待验证
+    Config_Init();
+    Driver_Init();
+    PWM_Timer_Init();
+    // 硬件过流中断初始化
     CMP3_Interrupt_Init();
-	
+    // 调试串口初始化
+    UART1_Init();
+    // Systick定时器 使能与中断优先级配置
     ClrBit(IP2, PSYSTICK1 | PSYSTICK0);
     SetBit(DRV_SR, SYSTIE);
     EA = 1;
@@ -54,22 +48,21 @@ void HardwareInit(void)
 void SoftwareInit(void)
 {
     memset(&mcFocCtrl, 0, sizeof(FOCCTRL));
-    /* -----保护----- */
-    mcFaultSource = 0;
-    /*****电机状态机时序变量***********/
     McStaSet.SetMode = 0;
     mcCurOffset.IuOffsetSum = 16383;
     mcCurOffset.IvOffsetSum = 16383;
     mcCurOffset.Iw_busOffsetSum = 16383;
-    mcState = mcReady;
-    mcFaultSource   = FaultNoSource;
 }
 
-
-
+/**
+    @function     main
+    @brief        主函数
+    @date         2025-04-17
+*/
 void main(void)
 {
-    uint16 PowerUpCnt = 0;
+    uint16_t PowerUpCnt = 0;
+    
     for (PowerUpCnt = 0; PowerUpCnt < 55000; PowerUpCnt++);
     
     SoftwareInit();
@@ -83,37 +76,31 @@ void main(void)
         }
         else
         {
-            MC_Control();
+            Motor_Control_State();
             
-            if (g_1mTick)
+            if (TickFlag)
             {
-                if ((mcState != mcInit) && (mcState != mcReady))
-                {
-                    mcFocCtrl.SpeedFlt  = LPF_Zero_Update(FOC__EOME, mcFocCtrl.SpeedFlt, 30);
-                }
-                else
-                {
-                    mcFocCtrl.SpeedFlt = 0;
-                }
+                // 喂狗
+                SetBit(WDT_CR, WDTRF);
+                // 获取滤波数据
+                Get_LPF_Value();
+                // 输入命令处理
+                Get_Target_Ref();
+                // 故障处理与故障恢复
+                Motor_Fault_Handle();
+                
+                // 环路响应
+                if ((sysState == MOTOR_RUN) || (sysState == MOTOR_STOP))
+                { Loop_Control(); }
                 
-                SetBit(ADC_CR, ADCBSY);
-                Power_Currt = (ADC7_DR);
-                Power_Currt   = Abs_F16(Power_Currt - mcCurOffset.Iw_busOffset);
-                mcFocCtrl.mcADCCurrentbus  = LPF_Zero_Update(Power_Currt << 2, mcFocCtrl.mcADCCurrentbus, 20);
+                // 故障指示
+                LED_State_Display(FaultSource);
                 
-                if (mcState != mcRun)
-                {
-                    mcFocCtrl.mcDcbusFlt = LPF_Zero_Update(ADC2_DR, mcFocCtrl.mcDcbusFlt, 50);
-                }
-                else
-                {
-                    mcFocCtrl.mcDcbusFlt = FOC__UDCFLT;
-                }
+                // 滤波器带宽动态调整
+                if (sysState == MOTOR_RUN)
+                { ATORamp(); }
                 
-                TargetRef_Process();
-                Speed_response();
-                ATORamp();
-                g_1mTick = 0;
+                TickFlag = false;
             }
         }
     }

+ 61 - 0
User/Application/protect.h

@@ -0,0 +1,61 @@
+#ifndef __PROTECT_H_
+#define __PROTECT_H_
+
+#define PRT_SW_OVER_CURR_EN                 (1)                                 // 软件过流
+#define PRT_VDC_EN                          (1)                                 // 母线电压
+#define PRT_PHASE_LOSS_EN                   (1)                                 // 缺相
+#define PRT_OVER_POWER_EN                   (1)                                 // 过功率
+#define PRT_OVER_TEMPER_EN                  (1)                                 // 温度
+#define PRT_MOTOR_STALL_EN                  (0)                                 // 堵转
+#define PRT_PWM_LOSS_EN                     (0)                                 // PWM丢失
+#define PRT_UART_LOSS_EN                    (0)                                 // 串口信号丢失
+
+#define AUTO_RECOVER_FAULT_EN               (1)                                 // 保护自恢复
+#define PROTECT_RECOVER_TIME                (5000)                              // (unit:ms) 恢复等待时间
+
+// 硬件过流
+#define HW_OC_MODE                          (HW_OC_CMP_MODE)
+
+// 硬件过流保护比较值来源
+#define COMPARE_MODE                        (COMPARE_DAC_MODE)
+#define HW_OC_VALUE                         (30.0)
+
+// 直流母线电压保护
+#define OVER_VOLTAGE_PROTECT                UDC_Value(30)
+#define UNDER_VOLTAGE_PROTECT               UDC_Value(18)
+#define OVER_VOLTAGE_RECOVER                UDC_Value(28)
+#define UNDER_VOLTAGE_RECOVER               UDC_Value(20)
+
+// 缺相保护参数设置值
+#define PHASE_NOLOAD_CURR                   I_Value(0.1)
+#define LOSS_PHASE_CYCLE                    (50)
+
+// 过功率保护参数设置
+// FOC__POW与实际功率转换公式
+// FOC__POW = P * 32767 / HW_BOARD_VOLT_MAX / HW_BOARD_CURR_MAX
+// HW_BOARD_VOLT_MAX 732
+// HW_BOARD_CURR_MAX 6.25
+#define OVER_POWER_VALUE                    (200.0)
+#define OP_DECTTIME                         (10000)
+
+// 软件过流保护参数设置
+#define BUS_CURR_PROT_VALUE                 I_Value(20.0)
+#define BUS_CURR_DECTTIME                   (30)
+
+// 堵转保护参数设置
+// 200RPM 1310 1000RPM 5600
+#define STALL_SPEED_MAX                     S_Value(2500)
+#define STALL_SPEED_MIN                     S_Value(150)
+#define ES_THRESHOLD_LOW                    (1000.0)
+#define ES_THRESHOLD_HIGH                   (4500.0)
+#define ES_THRESHOLD_SPEED                  S_Value(1000)
+
+
+// 温度保护 (CMFA103F3950FB)
+// 80°C - 1.228kΩ | 95°C - 0.783kΩ
+// Q15 = 32767 * Res / (10K + Res)
+#define TEMP_PROT_VALUE                     TEMPER_VALUE(0.783) // (kΩ) 过温阈值 (≈95°C)
+#define TEMP_RECOVER_VALUE                  TEMPER_VALUE(1.249) // (kΩ) 恢复阈值 (≈80°C)
+#define TEMP_DECTTIME                       (10000)
+#endif
+

+ 226 - 131
User/Function/MotorControl.c

@@ -1,6 +1,5 @@
 #include <MyProject.h>
 
-MotStateType  data mcState;
 MotStaM     McStaSet;
 
 /**
@@ -8,173 +7,142 @@ MotStaM     McStaSet;
     @warning   电机的状态只能在电机状态控制中切换,禁止在其他地方切换电机状态
     @date      2022-07-14
 */
-void MC_Control(void)
+void Motor_Control_State(void)
 {
-    switch (mcState)
+    if (FaultSource != SYS_NO_FAULT)
+    { sysState = MOTOR_FAULT; }
+    else if ((isCtrlPowOn == false) && (sysState != SYS_READY))
     {
-        case mcReady:
-            Motor_Ready();
-            
-            if (mcFaultSource != FaultNoSource)
+        sysState = MOTOR_STOP;
+        
+        if (sysState == MOTOR_RUN)
+        { mcFocCtrl.State_Count = 3000; }
+    }
+    
+    switch (sysState)
+    {
+        case SYS_READY:
+            if (McStaSet.SetFlag.CalibFlag == 0)
             {
-                mcState = mcFault;
+                McStaSet.SetFlag.CalibFlag = 1;
+                MOE = 0;
+                ClrBit(DRV_CR, FOCEN);
+                ClrBit(DRV_CR, DRVEN);
+                mcCurOffset.OffsetFlag = 0;
+                mcCurOffset.OffsetCount = 0;
+                #if ( Shunt_Resistor_Mode == Single_Resistor)
+                SetBit(ADC_MASK, CH4EN | CH2EN);
+                #else
+                SetBit(ADC_MASK, CH4EN | CH2EN | CH1EN | CH0EN);
+                #endif
             }
-            else if ((mcCurOffset.OffsetFlag == 1) && isCtrlPowOn)
-            { mcState = mcInit; }
+            
+            if ((mcCurOffset.OffsetFlag == 1) && isCtrlPowOn)
+            { sysState = SYS_INIT; }
             
             break;
             
-        case mcInit:
-            if (mcFaultSource != FaultNoSource)
-            {
-                mcState = mcFault;
-            }
-            else if (isCtrlPowOn == false)
-            {
-                mcState = mcStop;
-            }
-            else
+        case SYS_INIT:
+            #if ( Shunt_Resistor_Mode == Single_Resistor)
+            ClrBit(ADC_MASK, CH4EN );
+            #else
+            ClrBit(ADC_MASK, CH4EN | CH1EN | CH0EN);
+            #endif
+            memset(&mcFocCtrl, 0, sizeof(FOCCTRL));
+            memset(&faultCheck, 0, sizeof(Fault_Check_t));
+            memset(&estData, 0, sizeof(Estimated_Data_t));
+            memset(&loopCtrl, 0, sizeof(Loop_Control_t));
+            //缺相变量清零
+            McStaSet.SetMode = 0;
+            Power_Currt = 0;
+            #if (CHARGE_EN)
+            mcFocCtrl.State_Count   = CHARGE_TIME;
+            sysState = PRE_DRIVER_CHARGE;
+            #else
             {
-                Motor_Init();
-                #if (CHARGE_EN == Enable)
-                mcFocCtrl.State_Count   = CHARGE_TIME;
-                mcState = mcCharge;
-                #else
                 #if (ALIGN_MOME != ALIGN_DSIABLE)
-                mcFocCtrl.mcPosCheckAngle           = Align_Angle1;
-                mcState                             = mcAlign;
-                mcFocCtrl.State_Count               = AlignAll_Time;
+                mcFocCtrl.mcPosCheckAngle = Align_Angle1;
+                sysState = MOTOR_ALIGN;
+                mcFocCtrl.State_Count = AlignAll_Time;
                 #else
-                mcState = mcStart;
-                #endif
+                sysState = MOTOR_START;
                 #endif
             }
-            
+            #endif
             break;
             
-        case mcCharge:
-            if (mcFaultSource != FaultNoSource)
-            {
-                mcState = mcFault;
-            }
-            else if (isCtrlPowOn == false)
-            {
-                mcState = mcStop;
-            }
-            else
-            {
-                Motor_Charge();
-                
-                if (mcFocCtrl.State_Count == 0)
-                {
-                    MOE = 0;
-                    #if (PosCheckEnable == Enable)
-                    mcState                             = mcPosiCheck;
-                    McStaSet.SetFlag.PosiCheckSetFlag   = 0;
-                    mcFocCtrl.mcPosCheckAngle           = 0xffff;                     // 角度赋初值
-                    #elif (ALIGN_MOME != ALIGN_DSIABLE)
-                    mcFocCtrl.mcPosCheckAngle           = Align_Angle1;
-                    mcState                             = mcAlign;
-                    mcFocCtrl.State_Count               = AlignAll_Time;
-                    #else
-                    mcState = mcStart;
-                    #endif
-                }
-            }
+        case PRE_DRIVER_CHARGE:
+            Motor_Charge();
             
-            break;
-            #if (ALIGN_MOME != ALIGN_DSIABLE)
-            
-        case mcAlign:
-            if (mcFaultSource != FaultNoSource)
-            {
-                mcState = mcFault;
-            }
-            else if (isCtrlPowOn == false)
-            {
-                mcState = mcStop;
-            }
-            else
+            if (mcFocCtrl.State_Count == 0)
             {
-                Motor_Align();
-                #if (ALIGN_MOME == ALIGN_TEST)
-                
-                while (1);
-                
+                MOE = 0;
+                #if (ALIGN_MOME != ALIGN_DSIABLE)
+                mcFocCtrl.mcPosCheckAngle = Align_Angle1;
+                sysState = MOTOR_ALIGN;
+                mcFocCtrl.State_Count = AlignAll_Time;
                 #else
-                
-                if (mcFocCtrl.State_Count > (Align_Time1 + Align_Time2 + Align_Time3) )
-                {
-                    mcFocCtrl.CurrentAlignStatus = 0;
-                    FOC__THETA = Align_Angle1;
-                    FOC_IQREF  = IQ_Align_CURRENT * ((float)(AlignAll_Time - mcFocCtrl.State_Count) / AlignRamp_Time);
-                }
-                else if (mcFocCtrl.State_Count > (Align_Time2 + Align_Time3))
-                {
-                    mcFocCtrl.CurrentAlignStatus = 1;
-                    FOC__THETA = Align_Angle2;
-                    FOC_IQREF  = IQ_Align_CURRENT;
-                }
-                else if (mcFocCtrl.State_Count > (Align_Time3))
-                {
-                    mcFocCtrl.CurrentAlignStatus = 2;
-                    mcFocCtrl.AngleProcess = Angle_AngleK * (Align_Time2 + Align_Time3 - mcFocCtrl.State_Count);
-                    mcFocCtrl.AngleStart = Align_Angle2 + mcFocCtrl.AngleProcess;
-                    FOC__THETA = mcFocCtrl.AngleStart;
-                }
-                else
-                {
-                    mcFocCtrl.CurrentAlignStatus = 2;
-                    mcState = mcStart;
-                }
-                
+                sysState = MOTOR_START;
                 #endif
             }
             
             break;
-            #endif
             
-        case mcStart:
-            Motor_Static_Open();
-            mcState = mcRun;
-            break;
+        case MOTOR_ALIGN:
+            Motor_Align();
+            #if (ALIGN_MOME == ALIGN_TEST)
+            
+            while (1);
             
-        case mcRun:
-            if (mcFaultSource != FaultNoSource)
+            #else
+            
+            if (mcFocCtrl.State_Count > (Align_Time1 + Align_Time2 + Align_Time3) )
             {
-                mcState = mcFault;
+                mcFocCtrl.CurrentAlignStatus = 0;
+                FOC__THETA = Align_Angle1;
+                FOC_IQREF  = IQ_Align_CURRENT * ((float)(AlignAll_Time - mcFocCtrl.State_Count) / AlignRamp_Time);
             }
-            else if (isCtrlPowOn == false)
+            else if (mcFocCtrl.State_Count > (Align_Time2 + Align_Time3))
             {
-                mcState = mcStop;
-                mcFocCtrl.State_Count = 3000;
+                mcFocCtrl.CurrentAlignStatus = 1;
+                FOC__THETA = Align_Angle2;
+                FOC_IQREF  = IQ_Align_CURRENT;
             }
-            
-            break;
-            
-        case mcStop:
-            if (mcFaultSource != FaultNoSource)
+            else if (mcFocCtrl.State_Count > (Align_Time3))
             {
-                mcState = mcFault;
+                mcFocCtrl.CurrentAlignStatus = 2;
+                mcFocCtrl.AngleProcess = Angle_AngleK * (Align_Time2 + Align_Time3 - mcFocCtrl.State_Count);
+                mcFocCtrl.AngleStart = Align_Angle2 + mcFocCtrl.AngleProcess;
+                FOC__THETA = mcFocCtrl.AngleStart;
             }
             else
             {
-                if ((mcFocCtrl.SpeedFlt < MOTOR_SPEED_STOP_RPM) && (mcFocCtrl.State_Count == 0))
-                {
-                    mcState = mcReady;
-                    ClrBit(DRV_CR, FOCEN);
-                    ClrBit(DRV_CR, DRVEN);
-                    MOE = 0;
-                }
+                mcFocCtrl.CurrentAlignStatus = 2;
+                sysState = MOTOR_START;
             }
             
+            #endif
+            break;
+            
+        case MOTOR_START:
+            Motor_Static_Open();
+            sysState = MOTOR_RUN;
             break;
             
-        case mcFault:
-            if (mcFaultSource == FaultNoSource)
+        case MOTOR_STOP:
+            if ((estData.ActSpeedFlt < MOTOR_SPEED_STOP_RPM) && (mcFocCtrl.State_Count == 0))
             {
-                mcState = mcReady;
+                sysState = SYS_READY;
+                ClrBit(DRV_CR, FOCEN);
+                ClrBit(DRV_CR, DRVEN);
+                MOE = 0;
             }
+            
+            break;
+            
+        case MOTOR_FAULT:
+            if (FaultSource == SYS_NO_FAULT)
+            { sysState = SYS_READY; }
             else
             {
                 DRV_CMR &= 0xFFC0;
@@ -186,3 +154,130 @@ void MC_Control(void)
     }
 }
 
+/**
+    @function     Get_LPF_Value
+    @brief        数据滤波处理
+    @date         2025-12-23
+*/
+void Get_LPF_Value(void)
+{
+    // 开启AD转换 等待转换结束
+    SetBit(ADC_CR, ADCBSY);
+    
+    while (ReadBit(ADC_CR, ADCBSY));
+    
+    // 读取AD转换数据
+    Power_Currt = (ADC7_DR);
+    Power_Currt = Abs_F16(Power_Currt - mcCurOffset.Iw_busOffset);
+    estData.ActBusCurr = LPF_Zero_Update(Power_Currt << 2, estData.ActBusCurr, 20);
+    
+    if (sysState != MOTOR_RUN)
+    { estData.BusVoltage = LPF_Zero_Update(ADC2_DR, estData.BusVoltage, LPF_FC(20)); }
+    else
+    { estData.BusVoltage = FOC__UDCFLT; }
+    
+    // estData.BusVoltage = LPF_Zero_Update(ADC2_DR, estData.BusVoltage, LPF_FC(20));
+    estData.AlongVoltage = LPF_Zero_Update(ADC3_DR, estData.AlongVoltage, LPF_FC(100));
+    estData.NTCTemper = LPF_Zero_Update(ADC9_DR, estData.NTCTemper, LPF_FC(20));
+    
+    // 读取观测器数据
+    if ((sysState != SYS_INIT) && (sysState != SYS_READY))
+    {
+        estData.ActSpeedFlt = LPF_Zero_Update(FOC__EOME, estData.ActSpeedFlt, LPF_FC(30.0));
+        estData.UDFlt = LPF_Zero_Update(FOC__UD, estData.UDFlt, LPF_FC(20.0));
+        estData.UQFlt = LPF_Zero_Update(FOC__UQ, estData.UQFlt, LPF_FC(20.0));
+        estData.IDFlt = LPF_Zero_Update(FOC__ID, estData.IDFlt, LPF_FC(20.0));
+        estData.IQFlt = LPF_Zero_Update(FOC__IQ, estData.IQFlt, LPF_FC(20.0));
+    }
+    else
+    { estData.ActSpeedFlt = 0; }
+    
+    if ((sysState == MOTOR_RUN) || (sysState == MOTOR_STOP))
+    {
+        estData.Power = LPF_Zero_Update(FOC__POW, estData.Power, LPF_FC(5.0));
+        estData.BackEMF = FOC__EMF;
+        estData.BusCurr = Sqrt_alpbet(FOC__IA, FOC__IBET);
+    }
+    else
+    { estData.Power = 0; }
+    
+    // 保护参数更新
+    faultCheck.ActualSpeed = estData.ActSpeedFlt;
+    faultCheck.BusVoltage = estData.BusVoltage;
+    faultCheck.NTCTemper = estData.NTCTemper;
+    faultCheck.Power = estData.Power;
+    faultCheck.BackEMF = estData.BackEMF;
+    faultCheck.BusCurr = estData.BusCurr;
+}
+
+/**
+    @function     Loop_Control
+    @brief        环路响应
+    @date         2025-12-23
+*/
+void Loop_Control(void)
+{
+    switch (loopCtrl.State)
+    {
+        case OPEN_MODE:
+        {
+            if (estData.ActSpeedFlt > MOTOR_LOOP_RPM)
+            {
+                // 切入闭环并直接开始第一次运算
+                loopCtrl.State = CLOSE_MODE;
+                // 切换电流环KPKI
+                FOC_QKP = QKP;
+                FOC_QKI = QKI;
+                FOC_DKP = DKP;
+                FOC_DKI = DKI;
+                // 禁用D轴PI
+                // ClrBit(FOC_CR2,UDD);
+                // 参数配置
+                loopCtrl.ActualRef = _Q15(2000.0 / MOTOR_SPEED_BASE);
+                loopCtrl.Inc = MOTOR_SPEED_INC;
+                loopCtrl.Dec = MOTOR_SPEED_DEC;
+                // PI计算初始化
+                HW_Zero_PI_Init();
+                // 弱磁初始化
+                #if (Filed_Weaken_En)
+                Filed_Weaken_Init();
+                #endif
+                PI0_UKH =  FOC_IQREF;
+            }
+        }
+        break;
+        
+        case CLOSE_MODE:
+        {
+            if (++loopCtrl.CalcTime > SPEED_LOOP_TIME)
+            {
+                loopCtrl.CalcTime = 0;
+                
+                // 控制命令爬坡 用于实现调速信号之间平滑过渡
+                if (loopCtrl.TargetRef > (loopCtrl.ActualRef + loopCtrl.Inc))
+                { loopCtrl.ActualRef += loopCtrl.Inc; }
+                else if (loopCtrl.TargetRef < (loopCtrl.ActualRef - loopCtrl.Dec))
+                { loopCtrl.ActualRef -= loopCtrl.Dec;}
+                else
+                { loopCtrl.ActualRef = loopCtrl.TargetRef ;}
+                
+                // 环路计算
+                estData.ISRef = HW_Zero_Calc(loopCtrl.ActualRef - estData.ActSpeedFlt);
+                // 弱磁控制
+                #if (Filed_Weaken_En)
+                fieldWeaken.ISRef = estData.ISRef;
+                File_Weaken_Control();
+                estData.IDRef = fieldWeaken.IDRef;
+                estData.IQRef = fieldWeaken.IQRef;
+                #else
+                estData.IDRef = 0;
+                estData.IQRef = estData.ISRef;
+                #endif
+                // 电流给定
+                FOC_IQREF = estData.IQRef;
+                FOC_IDREF = estData.IDRef;
+            }
+        }
+        break;
+    }
+}

+ 36 - 132
User/Function/MotorControlFunction.c

@@ -1,14 +1,3 @@
-/**
-    @copyright (C) COPYRIGHT 2022 Fortiortech Shenzhen
-    @file      MotorControlFunction.c
-    @author    Fortiortech  Appliction Team
-    @since     Create:2021-11-07
-    @date      Last modify:2022-07-14
-    @note      Last modify author is Marcel He
-    @brief
-*/
-
-
 #include <MyProject.h>
 
 CurrentOffset      xdata mcCurOffset;  ///< 电流采样偏置电压采集缓存
@@ -75,16 +64,12 @@ void FOC_Init(void)
     FOC_OMEKLPF     = SPEED_KLPF;                           // 估算器内速度低通滤波系数
     FOC_TGLI        = PWM_TGLI_LOAD;                        // 死区配置
     SetBit(FOC_CR1, SVPWMEN);                               // SVPWM模式
-    
-    
-	#if (IR_MODE == CCW)
-	SetBit(DRV_CR, DDIR);
-	#endif
-	
+    #if (IR_MODE == CCW)
+    SetBit(DRV_CR, DDIR);
+    #endif
     #if (OverModulation)
-	SetBit(FOC_CR1, OVMDL);
+    SetBit(FOC_CR1, OVMDL);
     #endif
-	
     /* 单电阻采样; 需要最小采样窗,FOC_TRGDLY为0,七段式SVPWM方式 */
     #if (Shunt_Resistor_Mode == Single_Resistor)
     {
@@ -150,38 +135,32 @@ void FOC_Init(void)
         }
         #endif // end DouRes_Sample_Mode
     }
-    #endif 
-
-	
-            #if (Shunt_Resistor_Mode == Single_Resistor)    // 单电阻校正
-            {
-                /*set ibus current sample offset*/
-                SetReg(FOC_CR2, CSOC0 | CSOC1, 0x00);
-                FOC_CSO = mcCurOffset.Iw_busOffset;         // 写入Ibus的偏置
-            }
-            #elif (Shunt_Resistor_Mode == Double_Resistor)  // 双电阻校正
-            {
-                /*set ia, ib current sample offset*/
-                SetReg(FOC_CR2, CSOC0 | CSOC1, CSOC0);
-                FOC_CSO  = mcCurOffset.IuOffset;            // 写入IA的偏置
-            
-                SetReg(FOC_CR2, CSOC0 | CSOC1, CSOC1);
-                FOC_CSO  = mcCurOffset.IvOffset;            // 写入IB的偏置
-            }
-            #elif (Shunt_Resistor_Mode == Three_Resistor)   // 三电阻校正
-            {
-                /*set ibus current sample offset*/
-                SetReg(FOC_CR2, CSOC0 | CSOC1, CSOC0);
-                FOC_CSO = mcCurOffset.IuOffset;             // 写入IA的偏置
-            
-                SetReg(FOC_CR2, CSOC0 | CSOC1, CSOC1);
-                FOC_CSO = mcCurOffset.IvOffset;             // 写入IB的偏置
-            
-                SetReg(FOC_CR2, CSOC0 | CSOC1, 0x00);
-                FOC_CSO = mcCurOffset.Iw_busOffset;         // 写入IC的偏置
-            }
-            #endif 
-			
+    #endif
+    #if (Shunt_Resistor_Mode == Single_Resistor)    // 单电阻校正
+    {
+        /*set ibus current sample offset*/
+        SetReg(FOC_CR2, CSOC0 | CSOC1, 0x00);
+        FOC_CSO = mcCurOffset.Iw_busOffset;         // 写入Ibus的偏置
+    }
+    #elif (Shunt_Resistor_Mode == Double_Resistor)  // 双电阻校正
+    {
+        /*set ia, ib current sample offset*/
+        SetReg(FOC_CR2, CSOC0 | CSOC1, CSOC0);
+        FOC_CSO  = mcCurOffset.IuOffset;            // 写入IA的偏置
+        SetReg(FOC_CR2, CSOC0 | CSOC1, CSOC1);
+        FOC_CSO  = mcCurOffset.IvOffset;            // 写入IB的偏置
+    }
+    #elif (Shunt_Resistor_Mode == Three_Resistor)   // 三电阻校正
+    {
+        /*set ibus current sample offset*/
+        SetReg(FOC_CR2, CSOC0 | CSOC1, CSOC0);
+        FOC_CSO = mcCurOffset.IuOffset;             // 写入IA的偏置
+        SetReg(FOC_CR2, CSOC0 | CSOC1, CSOC1);
+        FOC_CSO = mcCurOffset.IvOffset;             // 写入IB的偏置
+        SetReg(FOC_CR2, CSOC0 | CSOC1, 0x00);
+        FOC_CSO = mcCurOffset.Iw_busOffset;         // 写入IC的偏置
+    }
+    #endif
     /*  -------------------------------------------------------------------------------------------------
         DRV_CTL:PWM来源选择
         OCS = 0, DRV_COMR
@@ -258,20 +237,14 @@ void Motor_Align(void)
         FOC_EKP     = OBSW_KP_GAIN_START;
         FOC_EKI     = OBSW_KI_GAIN_START;
         SetBit(FOC_CR2, UDD);
-        /*配置预定位角度*/
+        // 配置预定位角度
         FOC__THETA  = Align_Angle1;
-        /*********PLL或SMO**********/
         #if (EstimateAlgorithm == SMO )
-        {
-            FOC__ETHETA   = FOC__THETA - 4096;
-        }
+        FOC__ETHETA   = FOC__THETA - 4096;
         #else
-        {
-            FOC__ETHETA   = FOC__THETA;
-        }
-        #endif //end    EstimateAlgorithm
-        /*使能输出*/
-        //        DRV_CMR |= 0x3F;                         // U、V、W相输出
+        FOC__ETHETA   = FOC__THETA;
+        #endif
+        // DRV_CMR |= 0x3F;                         // U、V、W相输出
         MOE = 1;
     }
 }
@@ -287,7 +260,6 @@ void Motor_Static_Open(void)
     FOC_Init();
     MOE = 1;
     FOC__THETA = mcFocCtrl.AngleStart;
-    /*启动电流、KP、KI、FOC_EKP、FOC_EKI*/
     FOC_IDREF = ID_Start_CURRENT;                         // D轴启动电流
     FOC_DKP = DKPStart;
     FOC_DKI = DKIStart;
@@ -363,17 +335,6 @@ void MC_Break(void)
 }
 
 
-/**
-    @brief     控制变量上电初始化,包括保护参数的初始化、电机状态初始化
-    @brief     上电只运行一次
-    @date      2022-07-14
-*/
-void MotorcontrolInit(void)
-{
-}
-
-
-
 
 /**
     @brief     上电时,先对硬件电路的电流进行采集,写入对应的校准寄存器中。
@@ -421,14 +382,9 @@ void GetCurrentOffset(void)
     #endif
     mcCurOffset.OffsetCount++;
     
-    if (mcCurOffset.OffsetCount > Calib_Time)
+    if (mcCurOffset.OffsetCount > 1000)
     {
         mcCurOffset.OffsetFlag = 1;
-        #if (GetCurrentOffsetEnable)
-        {
-            Fault_GetCurrentOffset();     //偏置电压保护
-        }
-        #endif
     }
 }
 
@@ -440,56 +396,4 @@ void GetCurrentOffset(void)
 */
 void Motor_Ready(void)
 {
-    if (McStaSet.SetFlag.CalibFlag == 0)
-    {
-        McStaSet.SetFlag.CalibFlag = 1;
-        MOE               = 0;
-        ClrBit(DRV_CR, FOCEN);
-        ClrBit(DRV_CR, DRVEN);
-        mcCurOffset.OffsetFlag = 0;
-        mcCurOffset.OffsetCount = 0;    //偏置电压采集计数
-        #if ( Shunt_Resistor_Mode == Single_Resistor)
-        {
-            SetBit(ADC_MASK, CH4EN | CH2EN);  // 开启外部ADC采集偏置电压
-        }
-        #else
-        {
-            SetBit(ADC_MASK, CH4EN | CH2EN | CH1EN | CH0EN); // 开启外部ADC采集偏置电压
-        }
-        #endif
-    }
 }
-
-
-/**
-    @brief     电机初始化,对电机相关变量、PI进行初始化设置,关闭FOC所需要使用到的ADC
-    @note      关闭FOC所需要使用到的ADC,FOC模块会自动调用相应ADC 无需外部使能
-    @date      2022-07-14
-*/
-
-void Motor_Init(void)
-{
-    #if ( Shunt_Resistor_Mode == Single_Resistor)
-    {
-        ClrBit(ADC_MASK, CH4EN );          // 关闭软件电流采样的ADC  FOC模块会自动调用相应ADC 无需外部使能
-    }
-    #else
-    {
-        ClrBit(ADC_MASK, CH4EN | CH1EN | CH0EN);  // 关闭软件电流采样的ADC  FOC模块会自动调用相应ADC 无需外部使能
-    }
-    #endif
-    memset(&mcFocCtrl, 0, sizeof(FOCCTRL));
-    //缺相变量清零
-    McStaSet.SetMode                    = 0;
-    mcFocCtrl.CtrlMode                  = 0;
-    mcFocCtrl.ChargeStep                = 0;
-    mcFocCtrl.Flg_ATORampEnd            = 0;
-    /* -----电机功率参数初始化----- */
-    Power_Currt                         = 0;
-    HW_Zero_PI_Init();
-    HW_One_PI_Init();
-    HW_Two_PI_Init();
-    HW_Three_PI_Init();
-    // SoftPiInit();
-}
-

+ 249 - 54
User/Function/MotorProtect.c

@@ -1,73 +1,268 @@
-#include <FU68xx_5.h>
 #include <Myproject.h>
 
 
-FaultStateType data mcFaultSource;      ///< 故障类型
+/**
+    @function     Bus_Voltage_Check
+    @brief        母线电压检测
+    @date         2025-11-17
+*/
+void Bus_Voltage_Check(void)
+{
+    static uint16_t DelayCnt = 0;
+    
+    if (DelayCnt < 500)
+    {
+        DelayCnt++;
+        return;
+    }
+    
+    // 过压检测
+    FAULT_COUNT_UPDOWN(faultCheck.CheckCnt.OverVolt,
+        (faultCheck.BusVoltage > OVER_VOLTAGE_PROTECT), 300, VDC_OVER);
+    // 欠压检测
+    FAULT_COUNT_UPDOWN(faultCheck.CheckCnt.UnderVolt,
+        (faultCheck.BusVoltage < UNDER_VOLTAGE_PROTECT), 300, VDC_UNDER);
+}
 
 
+/**
+    @function     MOS_Temper_Check
+    @brief        温度检测
+    @date         2025-11-17
+*/
+void MOS_Temper_Check(void)
+{
+    FAULT_COUNT_UPDOWN(faultCheck.CheckCnt.OverTemper,
+        (faultCheck.NTCTemper <= TEMP_PROT_VALUE), TEMP_DECTTIME, TEMPER_NTC);
+}
 
+/**
+    @function     Bus_Curr_Check
+    @brief        母线电流检测
+    @date         2025-11-17
+*/
+void Bus_Curr_Check(void)
+{
+    FAULT_COUNT_UPDOWN(faultCheck.CheckCnt.OverCurr,
+        (faultCheck.BusCurr >= BUS_CURR_PROT_VALUE), BUS_CURR_DECTTIME, SOFT_OVER_CURR);
+}
 
 /**
-    @brief      偏置电压检测
+    @function     Motor_Stall_Check
+    @brief        电机堵转检测
+    @date         2025-11-17
 */
-void Fault_GetCurrentOffset(void)
+void Motor_Stall_Check(void)
 {
-    if (mcCurOffset.OffsetFlag == 1)
+    if (faultCheck.CheckCnt.DelayStall < 500)
+    { faultCheck.CheckCnt.DelayStall ++;}
+    else
     {
-        #if (VHALF_EN == Enable)                                              //有加VHALF偏置,理论值为16383
+        // 1.持续3秒反电动势过小
+        if (faultCheck.BackEMF < ES_THRESHOLD_LOW)
         {
-            #if (Shunt_Resistor_Mode == Single_Resistor)                   //单电阻模式
-            {
-                if ((mcCurOffset.Iw_busOffset < GetCurrentOffsetValueLow) || (mcCurOffset.Iw_busOffset > GetCurrentOffsetValueHigh))
-                {
-                    mcFaultSource = FaultGetOffset;
-                }
-            }
-            #elif (Shunt_Resistor_Mode == Double_Resistor)                 //双电阻模式
-            {
-                if ((mcCurOffset.IuOffset < GetCurrentOffsetValueLow) || (mcCurOffset.IuOffset > GetCurrentOffsetValueHigh)
-                    || (mcCurOffset.IvOffset < GetCurrentOffsetValueLow) || (mcCurOffset.IvOffset > GetCurrentOffsetValueHigh))
-                {
-                    mcFaultSource = FaultGetOffset;
-                }
-            }
-            #elif (Shunt_Resistor_Mode == Three_Resistor)                 //三电阻模式
-            {
-                if ((mcCurOffset.IuOffset < GetCurrentOffsetValueLow) || (mcCurOffset.IuOffset > GetCurrentOffsetValueHigh)
-                    || (mcCurOffset.IvOffset < GetCurrentOffsetValueLow) || (mcCurOffset.IvOffset > GetCurrentOffsetValueHigh)
-                    || (mcCurOffset.Iw_busOffset < GetCurrentOffsetValueLow) || (mcCurOffset.Iw_busOffset > GetCurrentOffsetValueHigh))
-                {
-                    mcFaultSource = FaultGetOffset;
-                }
-            }
-            #endif
+            faultCheck.CheckCnt.EsValue++;
+            
+            if (faultCheck.CheckCnt.EsValue > 3000)
+            { FaultSource = MOTOR_STALL; }
         }
-        #else                                                              //没加VHALF偏置,理论值在0
+        // 2.超过对应转速时 反电动势过低
+        else if ((faultCheck.BackEMF < ES_THRESHOLD_HIGH) && (faultCheck.ActualSpeed > ES_THRESHOLD_SPEED))
         {
-            #if (Shunt_Resistor_Mode == Single_Resistor)                   //单电阻模式
-            {
-                if (mcCurOffset.Iw_busOffset > GetCurrentOffsetValue)
-                {
-                    mcFaultSource = FaultGetOffset;
-                }
+            faultCheck.CheckCnt.EsValue++;
+            
+            if (faultCheck.CheckCnt.EsValue > 3000)
+            { FaultSource = MOTOR_STALL; }
+        }
+        else if (faultCheck.CheckCnt.EsValue)
+        {faultCheck.CheckCnt.EsValue--;}
         
-            }
-            #elif (Shunt_Resistor_Mode == Double_Resistor)                 //双电阻模式
-            {
-                if ((mcCurOffset.IuOffset > GetCurrentOffsetValue) || (mcCurOffset.IvOffset > GetCurrentOffsetValue))
-                {
-                    mcFaultSource = FaultGetOffset;
-                }
-            }
-            #elif (Shunt_Resistor_Mode == Three_Resistor)                 //三电阻模式
-            {
-                if ((mcCurOffset.IuOffset > GetCurrentOffsetValue) || (mcCurOffset.IvOffset > GetCurrentOffsetValue) || (mcCurOffset.Iw_busOffset > GetCurrentOffsetValue))
-                {
-                    mcFaultSource = FaultGetOffset;
-                }
-            }
-            #endif
+        // 3.速度超出极限转速
+        if (faultCheck.ActualSpeed < STALL_SPEED_MIN || faultCheck.ActualSpeed > STALL_SPEED_MAX)
+        {
+            faultCheck.CheckCnt.StallSpeed++;
+            
+            if (faultCheck.CheckCnt.StallSpeed > 3000)
+            { FaultSource = MOTOR_STALL; }
         }
+        else if (faultCheck.CheckCnt.StallSpeed)
+        {faultCheck.CheckCnt.StallSpeed -- ;}
+    }
+    
+    // 4.长时间未进闭环
+    FAULT_COUNT_UPDOWN(faultCheck.CheckCnt.CloseLoop,
+        (loopCtrl.State == OPEN_MODE), 1000, MOTOR_STALL);
+}
+
+
+/**
+    @function     Phase_Curr_Check
+    @brief        缺相检测
+    @date         2024-06-17
+*/
+void Phase_Curr_Check(void)
+{
+    // 运行后等待计数
+    if (faultCheck.CheckCnt.DelayPhase < 500)
+    {
+        faultCheck.CheckCnt.DelayPhase++;
+        return;
+    }
+    
+    // 检测
+    if (faultCheck.CheckCnt.LossPhase < LOSS_PHASE_CYCLE)
+    {
+        faultCheck.MaxIa = FOC__IAMAX;
+        faultCheck.MaxIb = FOC__IBMAX;
+        faultCheck.MaxIc = FOC__ICMAX;
+        faultCheck.CheckCnt.LossPhase++;
+        return;
+    }
+    
+    faultCheck.CheckCnt.LossPhase = 0;
+    // 各相独立更新缺相计数,逻辑与原代码完全一致
+    UPDATE_PHASE_LOSS_CNT(faultCheck.MaxIa,
+        faultCheck.MaxIb, faultCheck.MaxIc, faultCheck.CheckCnt.PhaseA);
+    UPDATE_PHASE_LOSS_CNT(faultCheck.MaxIb,
+        faultCheck.MaxIa, faultCheck.MaxIc, faultCheck.CheckCnt.PhaseB);
+    UPDATE_PHASE_LOSS_CNT(faultCheck.MaxIc,
+        faultCheck.MaxIa, faultCheck.MaxIb, faultCheck.CheckCnt.PhaseC);
+    // 清零变量
+    faultCheck.MaxIa = 0;
+    faultCheck.MaxIa = 0;
+    faultCheck.MaxIa = 0;
+    SetBit(FOC_CR2, ICLR);
+    
+    if ((faultCheck.CheckCnt.PhaseA > 10) ||
+        (faultCheck.CheckCnt.PhaseB > 10) ||
+        (faultCheck.CheckCnt.PhaseC > 10))
+    { FaultSource = FCT_LOSS_PHASE; }
+}
+
+/**
+    @function     Run_Power_Check
+    @brief        运行功率检测
+    @date         2024-06-17
+*/
+static void Run_Power_Check(void)
+{
+    FAULT_COUNT_UPDOWN(faultCheck.CheckCnt.OverPower,
+        (faultCheck.Power > OVER_POWER_VAL), OP_DECTTIME, OVER_POWER);
+}
+
+/**
+    @function     Recovery_Fault_Handle
+    @brief        故障恢复
+    @date         2025-11-19
+*/
+static void Recovery_Fault_Handle(void)
+{
+    switch (FaultSource)
+    {
+        default:
+            break;
+            
+        // 无故障 直接返回
+        case SYS_NO_FAULT:
+            return;
+            
+        // 欠压恢复
+        case VDC_UNDER:
+            if (faultCheck.BusVoltage > UNDER_VOLTAGE_RECOVER)
+            { RECOVER_DELAY(faultCheck.RecoverCnt.UnderVolt); }
+            
+            break;
+            
+        // 过压恢复
+        case VDC_OVER:
+            if (faultCheck.BusVoltage < OVER_VOLTAGE_RECOVER)
+            { RECOVER_DELAY(faultCheck.RecoverCnt.OverVolt); }
+            
+            break;
+            
+        // 过温恢复
+        case TEMPER_NTC:
+            if (faultCheck.NTCTemper >= TEMP_RECOVER_VALUE)
+            { RECOVER_DELAY(faultCheck.RecoverCnt.OverTemper); }
+            
+            break;
+            
+        // 缺相恢复
+        case FCT_LOSS_PHASE:
+            RECOVER_DELAY(faultCheck.RecoverCnt.PhaseLoss);
+            break;
+            
+        // 堵转恢复
+        case MOTOR_STALL:
+            RECOVER_DELAY(faultCheck.RecoverCnt.Stall);
+            break;
+            
+        // 过流恢复
+        case HARD_OVER_CURR:
+        case SOFT_OVER_CURR:
+            RECOVER_DELAY(faultCheck.RecoverCnt.OverCurr);
+            break;
+            
+        // 过功率恢复
+        case OVER_POWER:
+            RECOVER_DELAY(faultCheck.RecoverCnt.OverPower);
+            break;
+            
+        // PWM丢失
+        case LOSS_PWM:
+            RECOVER_DELAY(faultCheck.RecoverCnt.PWMLoss);
+            break;
+            
+        // 串口丢失
+        case LOSS_UART:
+            RECOVER_DELAY(faultCheck.RecoverCnt.UartLoss);
+            break;
+    }
+}
+
+/**
+    @function     Motor_Fault_Handle
+    @brief        故障检测与恢复
+    @date         2024-06-17
+*/
+void Motor_Fault_Handle(void)
+{
+    if ((sysState == MOTOR_RUN) || (sysState == MOTOR_STOP))
+    {
+        // 母线电流检测
+        #if (PRT_SW_OVER_CURR_EN)
+        Bus_Curr_Check();
+        #endif
+        // 相线电流检测
+        #if (PRT_PHASE_LOSS_EN)
+        Phase_Curr_Check();
+        #endif
+        // 电机运行状态检测
+        #if (PRT_MOTOR_STALL_EN)
+        Motor_Stall_Check();
+        #endif
+        // 功率检测
+        #if (PRT_OVER_POWER_EN)
+        Run_Power_Check();
         #endif
     }
+    
+    if (FaultSource == SYS_NO_FAULT)
+    {
+        // 电压检测
+        #if (PRT_VDC_EN)
+        Bus_Voltage_Check();
+        #endif
+        // 温度检测
+        #if (PRT_OVER_TEMPER_EN)
+        MOS_Temper_Check();
+        #endif
+    }
+    
+    // 故障自动恢复
+    #if (AUTO_RECOVER_FAULT_EN)
+    Recovery_Fault_Handle();
+    #endif
 }
+
+

+ 106 - 0
User/Function/led.c

@@ -0,0 +1,106 @@
+/**
+    @copyright None
+    @file      led.c
+    @author    Comment Vivre
+    @date      2025-12-19
+    @brief     None
+*/
+
+#include <MyProject.h>
+LED_State_t ledState = LED_STATE_OFF;
+LED_Count_t ledCount;
+
+
+/**
+    @function     LED_GPIO_Init
+    @brief        LED GPIO初始化
+    @date         2025-12-19
+*/
+void LED_GPIO_Init(void)
+{
+    SetBit(LED_PXIN, LED_PINX);
+    LED_OFF;
+}
+
+/**
+    @function     LED_State_Display
+    @brief        指示运行与故障代码
+    @param[in]    Xn0: [输入/出] 故障代码
+    @date         2025-05-30
+*/
+void LED_State_Display(unsigned char Xn0)
+{
+    switch (ledState)
+    {
+        // 关闭状态
+        case LED_STATE_OFF:
+        
+            // 存在故障 切入故障状态 清零计数
+            if (Xn0)
+            {
+                ledState = LED_STATE_BLINK_FAULT;
+                ledCount.PauseDelayCnt = 0;
+                ledCount.BlinkCnt = 0;
+            }
+            // 开启状态且无故障 切入开启状态并打开LED
+            else if (isCtrlPowOn)
+            {
+                ledState = LED_STATE_ON;
+                LED_ON;
+            }
+            
+            break;
+            
+        // 开启状态
+        case LED_STATE_ON:
+        
+            // 故障或停止时 关闭LED并切回关闭状态
+            if (!isCtrlPowOn || Xn0)
+            {
+                ledState = LED_STATE_OFF;
+                LED_OFF;
+            }
+            
+            break;
+            
+        // 故障指示状态
+        case LED_STATE_BLINK_FAULT:
+            ledCount.PauseDelayCnt ++;
+            
+            // 延时等待
+            if (ledCount.PauseDelayCnt >= 500)
+            {
+                ledCount.PauseDelayCnt = 0;
+                LEDPinONOFF;
+                
+                // 灭灯时计数
+                if (LED_PIN)
+                {
+                    ledCount.BlinkCnt ++;
+                    
+                    // 完成一个周期的闪烁 切换到延时等待模式
+                    if (ledCount.BlinkCnt >= Xn0)
+                    { ledState = LED_STATE_POST_FAULT_DELAY; }
+                }
+            }
+            
+            // 故障清除后 立刻回到熄灭状态
+            if (!Xn0)
+            {
+                LED_OFF;
+                ledState = LED_STATE_OFF;
+            }
+            
+            break;
+            
+        // 延迟等待
+        case LED_STATE_POST_FAULT_DELAY:
+            ledCount.PauseDelayCnt ++;
+            
+            // 延时满足或故障清除回到关闭状态
+            if ((ledCount.PauseDelayCnt >= 1000) || (!Xn0))
+            { ledState = LED_STATE_OFF; }
+            
+            break;
+    }
+}

+ 49 - 0
User/Function/led.h

@@ -0,0 +1,49 @@
+/**
+    @copyright None
+    @file      led.h
+    @author    Comment Vivre
+    @date      2025-12-19
+    @brief     None
+*/
+#ifndef __LED_H_
+#define __LED_H_
+
+
+#include <FU65_72.h>
+// LED指示灯控制
+// ------------------------------------------------------------------------
+typedef enum
+{
+    LED_STATE_OFF,                          // 熄灭
+    LED_STATE_ON,                           // 常亮
+    LED_STATE_BLINK_FAULT,                  // 故障闪烁
+    LED_STATE_POST_FAULT_DELAY              // 延时等待
+} LED_State_t;
+
+typedef struct
+{
+    uint16_t PauseDelayCnt;                 // 延时
+    uint8_t BlinkCnt;                       // 当前闪烁次数
+} LED_Count_t;
+
+extern LED_State_t ledState;
+extern LED_Count_t ledCount;
+
+void LED_GPIO_Init(void);
+void LED_State_Display(unsigned char Xn0);
+
+// IO选择
+// ------------------------------------------------------------------------
+#define LED_PXIN                            (P2_OE)                             // LED所在端口组
+#define LED_PINX                            (P21)                               // LED端口号
+#define LED_PIN                             (GP21)                              // LED IO PIN 
+#define LED_ON                              (LED_PIN = 0)                       // LED ON    
+#define LED_OFF                             (LED_PIN = 1)                       // LED OFF
+#define LEDPinONOFF                         (LED_PIN ^= 1)
+
+
+
+extern bool data isCtrlPowOn;
+
+
+#endif

+ 152 - 0
User/Function/protect_def.h

@@ -0,0 +1,152 @@
+/**
+    @copyright None
+    @file      protect_def.h
+    @author    Comment Vivre
+    @date      2025-12-29
+    @brief     None
+*/
+#ifndef __PROTECT_DEF_H_
+#define __PROTECT_DEF_H_
+
+#include <FU65_72.h>
+
+
+
+// 单相缺相计数更新宏:保持原有判定逻辑不变
+#define UPDATE_PHASE_LOSS_CNT(max_self, max_o1, max_o2, cnt) \
+    do { \
+        if ((((max_self) > ((max_o1) << 1)) || ((max_self) > ((max_o2) << 1))) &&\
+            ((max_self) > PHASE_NOLOAD_CURR)) \
+        { cnt++; } \
+        else if (cnt) \
+        { cnt--; } \
+    } while (0)
+
+/**
+    @brief 故障恢复延时计数宏
+    @param cnt  对应故障的延时计数器变量
+
+    @note  用于故障恢复逻辑,当检测条件已恢复正常后,
+          不会立刻清除故障,而是等待计数超过
+          PROTECT_RECOVER_TIME 后才解除故障,
+          以避免抖动恢复。
+*/
+#define RECOVER_DELAY(cnt) \
+    do {                                \
+        cnt++; \
+        if (cnt > PROTECT_RECOVER_TIME) { \
+            cnt = 0; \
+            FaultSource = SYS_NO_FAULT; \
+        }                               \
+    } while(0)
+
+
+/**
+    @brief 通用的故障计数检测宏
+    @param cnt        计数器变量
+    @param cond       检测条件 (true 表示异常)
+    @param threshold  连续超过阈值次数才判定故障
+    @param faultCode  触发的故障源代码
+*/
+#define FAULT_COUNT_UPDOWN(cnt, cond, threshold, faultCode) \
+    do { \
+        if (cond) { \
+            if (++(cnt) > (threshold)) { \
+                cnt = 0; \
+                FaultSource = (faultCode); \
+            } \
+        } else if (cnt) { \
+            cnt--; \
+        } \
+    } while (0)
+
+
+typedef enum
+{
+    SYS_NO_FAULT = 0x00,
+    // 过流类
+    HARD_OVER_CURR = 0x11,
+    SOFT_OVER_CURR = 0x12,
+    FAN_OVER_CURR = 0x13,
+    // 自检类
+    FCT_BUS_OFFSET = 0x21,
+    FCT_LOSS_PHASE = 0x22,
+    FCT_SHORT_UP_BRIDGE = 0x23,
+    FCT_SHORT_DOWN_BRIDGE = 0x24,
+    FCT_SHORT_PHASE = 0x25,
+    // 运行类
+    MOTOR_STALL = 0x31,
+    OVER_POWER = 0x32,
+    // 过欠压类
+    VDC_OVER = 0x41,
+    VDC_UNDER = 0x42,
+    MCU_LVM = 0x43,
+    // 温度类
+    TEMPER_NTC = 0x51,
+    TEMPER_MOTOR = 0x52,
+    TEMPER_IPM = 0x53,
+    TEMPER_TSD = 0x54,
+    // 通讯类
+    LOSS_UART = 0x61,
+    LOSS_PWM = 0x62
+} Fault_Type_e;
+
+extern Fault_Type_e xdata FaultSource;
+
+
+typedef struct
+{
+    // 检测数据
+    uint16_t BackEMF;                       // 反电动势
+    int16_t ActualSpeed;                    // 实际速度
+    uint16_t BusVoltage;                    // 母线电压
+    uint16_t NTCTemper;                     // NTC温度
+    int16_t BusCurr;                        // 估算母线电流
+    uint16_t Power;                         // 估算功率
+    // 三相电流极值
+    uint16_t MaxIa;
+    uint16_t MaxIb;
+    uint16_t MaxIc;
+    
+    // 保护检测计数
+    struct
+    {
+        uint16_t DelayPhase;
+        uint16_t LossPhase;
+        uint16_t PhaseA;
+        uint16_t PhaseB;
+        uint16_t PhaseC;
+        uint16_t CloseLoop;
+        uint16_t DelayStall;
+        uint16_t EsValue;
+        uint16_t StallSpeed;
+        uint16_t OverVolt;
+        uint16_t UnderVolt;
+        uint8_t OverCurr;
+        uint16_t OverTemper;
+        uint16_t OverPower;
+        uint16_t PwmLoss;
+        uint16_t UartLoss;
+    } CheckCnt;
+    // 恢复计数
+    struct
+    {
+        uint16_t Stall;
+        uint16_t OverVolt;
+        uint16_t UnderVolt;
+        uint16_t OverCurr;
+        uint16_t OverTemper;
+        uint16_t OverPower;
+        uint16_t PhaseLoss;
+        uint16_t PWMLoss;
+        uint16_t UartLoss;
+    } RecoverCnt;
+} Fault_Check_t;
+
+extern Fault_Check_t xdata faultCheck;
+
+
+
+
+
+#endif

+ 63 - 0
User/Function/protocol.c

@@ -0,0 +1,63 @@
+/**
+    @copyright None
+    @file      protocol.c
+    @author    Comment Vivre
+    @date      2025-12-23
+    @brief     None
+*/
+#include <MyProject.h>
+
+// 1 / 115200 * 640 *1000 ≈ 5.6 ms
+uint8_t xdata DebugDat[MAX_FRAME_LEN] = {0};
+uint8_t RecvBuffer[MAX_FRAME_LEN] = {0};
+bool RecMessageFalg = false;
+
+/**
+    @function     Dabug_Data_Update
+    @brief        调试信息上载
+    @date         2025-11-05
+*/
+void Dabug_Data_Update(void)
+{
+    static uint8_t update_delay_cnt = 0;
+    uint8_t sumcheck = 0, addcheck = 0;
+    uint16_t i = 0, switchTemp = 0;
+    
+    if (++ update_delay_cnt > 9)
+    {
+        update_delay_cnt = 0;
+        memset(&DebugDat, 0, 0x40);
+        *(_IO uint16_t xdata *)(&DebugDat +  0) = 0xABFF;
+        *(_IO uint16_t xdata *)(&DebugDat +  2) = 0xFFF1;
+        *(_IO uint16_t xdata *)(&DebugDat +  4) = 0x3800;
+        switchTemp = (uint16_t)FOC__UD;
+        *(_IO uint16_t xdata *)(&DebugDat +  6) = (switchTemp >> 8) | (switchTemp << 8);
+        switchTemp = (uint16_t)FOC__UQ;
+        *(_IO uint16_t xdata *)(&DebugDat +  8) = (switchTemp >> 8) | (switchTemp << 8);
+        switchTemp = (uint16_t)FOC__IA;
+        *(_IO uint16_t xdata *)(&DebugDat + 10) = (switchTemp >> 8) | (switchTemp << 8);
+        switchTemp = (uint16_t)FOC__EMF;
+        *(_IO uint16_t xdata *)(&DebugDat + 12) = (switchTemp >> 8) | (switchTemp << 8);
+        switchTemp = (uint16_t)FOC__IQ;
+        *(_IO uint16_t xdata *)(&DebugDat + 14) = (switchTemp >> 8) | (switchTemp << 8);
+        switchTemp = (uint16_t)FOC_IQREF;
+        *(_IO uint16_t xdata *)(&DebugDat + 16) = (switchTemp >> 8) | (switchTemp << 8);
+        switchTemp = (uint16_t)FOC__EOME;
+        *(_IO uint16_t xdata *)(&DebugDat + 18) = (switchTemp >> 8) | (switchTemp << 8);
+        switchTemp = (uint16_t)FOC__POW;
+        *(_IO uint16_t xdata *)(&DebugDat + 20) = (switchTemp >> 8) | (switchTemp << 8);
+        // UD UQ IA EMF IQ IQREF EOME POW REF
+        
+        // 计算校验
+        for (i = 0; i < 62; i++)
+        {
+            sumcheck += DebugDat[i];
+            addcheck += sumcheck;
+        }
+        
+        //将计算出来的校验数据写入数据帧
+        DebugDat[62] = sumcheck;
+        DebugDat[63] = addcheck;
+        Switch_DMA(0);
+    }
+}

+ 26 - 0
User/Function/protocol.h

@@ -0,0 +1,26 @@
+/**
+    @copyright None
+    @file      protocol.h
+    @author    Comment Vivre
+    @date      2025-12-23
+    @brief     None
+*/
+#ifndef __PROTOCOL_H_
+#define __PROTOCOL_H_
+
+#include <FU65_72.h>
+
+// 串口通讯协议定义 << 匿名协议
+#define FRAME_HEADER                    0xAB                        // 帧头
+#define MAX_FRAME_LEN                   0x40                        // 最大数据帧长度
+#define DATA_OFFSET                     0x06                        // 头(1B) + 地址(2B) + 功能码(1B) + 长度(2B)
+#define CRC_OFFSET                      0x02                        // 校验字(2B)
+
+
+extern bool RecMessageFalg;
+extern uint8_t xdata DebugDat[MAX_FRAME_LEN];
+extern uint8_t RecvBuffer[MAX_FRAME_LEN];
+
+void Dabug_Data_Update(void);
+
+#endif

+ 41 - 0
User/Function/pwm.c

@@ -0,0 +1,41 @@
+#include <MyProject.h>
+
+
+/**
+    @brief        对变量取32位的绝对值
+    @param[in]    value
+    @return       绝对值
+    @date         2022-07-13
+*/
+uint32_t Abs_F32(int32_t value)
+{
+    if (value < 0)
+    { return (-value); }
+    else
+    { return (value); }
+}
+
+
+Input_Capture_t xdata inputCapture;
+
+/**
+    @function     Input_Capture_Calc
+    @brief        输入捕获计算 频率与占空比
+    @date         2025-11-21
+*/
+void Input_Capture_Calc(void)
+{
+    if (inputCapture.UpdateFlag)
+    {
+        if ((Abs_F32(inputCapture.TimerDROld - inputCapture.TimerDR) < 0xFF) &&
+            (Abs_F32(inputCapture.TimerARROld - inputCapture.TimerARR) < 0xFF))
+        {
+            inputCapture.OutFreq = DivQ_L_MDU(0x0478, 0x68C0, inputCapture.TimerARR);
+            inputCapture.OutDuty = (float)1000 * inputCapture.TimerDR / inputCapture.TimerARR;
+        }
+        
+        inputCapture.UpdateFlag = 0;
+        inputCapture.TimerDROld = inputCapture.TimerDR;
+        inputCapture.TimerARROld = inputCapture.TimerARR;
+    }
+}

+ 29 - 0
User/Function/pwm.h

@@ -0,0 +1,29 @@
+/**
+    @copyright None
+    @file      pwm.h
+    @author    Comment Vivre
+    @date      2025-12-19
+    @brief     None
+*/
+#ifndef __PWM_H_
+#define __PWM_H_
+
+#include <FU65_72.h>
+
+typedef struct
+{
+    uint8_t UpdateFlag;
+    uint16_t TimerDR;
+    uint16_t TimerARR;
+    uint16_t TimerDROld;
+    uint16_t TimerARROld;
+    uint16_t OutFreq;
+    uint16_t OutDuty;
+} Input_Capture_t;
+
+extern Input_Capture_t xdata inputCapture;
+
+void Input_Capture_Calc(void);
+
+#endif
+

+ 35 - 37
User/Hardware/ADC.c

@@ -1,46 +1,44 @@
 #include <MyProject.h>
 
+/*
+    已使用ADC端口
+    P25 AD3  SREF  !>禁用
+    P24 AD2  OVP
+    P16 AD9  NTC
+    P34 AD7  IBUS !> AD6需要外部接入2.5V分压并开启AD6通道 否则会影响AD7采样数据
+    特殊端口
+    AD0 U相电流采样
+    AD1 V相电流采样
+    AD4 W相电流采样或者母线电流采样
+*/
 
+/**
+    @function     ADC_Init
+    @brief        ADC初始化
+    @date         2025-12-23
+*/
 void ADC_Init(void)
 {
-    /********************ADC 端口模拟功能设置************************/
-    SetBit(P2_AN , PIN0);	//AD0 P20 CH open--IU--固定
-    SetBit(P2_AN , PIN3);	//AD1 P23 CH open--IV--固定 
-    SetBit(P2_AN , PIN4);	//AD2 P24 CH open--IV--固定     
-    SetBit(P2_AN , PIN5);	//AD3 P25 CH open
-    SetBit(P2_AN , PIN7);	//AD4 P27 CH open--IBUS/IW--固定
-    SetBit(P2_AN , PIN6);	//AD11 P25 CH open
-	
-	
-//	 SetBit(P2_AN , PIN1);	//AD8 P25 CH open
-
-//	set_csr(P3_AN , PIN2);	//AD5 P32 CH open
-	SetBit(P3_AN , PIN3);	//AD6 P33 CH open
-	SetBit(P3_AN , PIN4);	//AD7 P34 CH open
-//	set_csr(P2_AN , PIN1);	//AD8 P21 CH open
-	SetBit(P1_AN , PIN6);	//AD9 P16 CH open
-//	set_csr(P1_AN , PIN4);	//AD10 P14 CH open
-//	set_csr(P2_AN , PIN6);	//AD11 P26 CH open
-//	set_csr(P1_AN , PIN3);	//AD12 P13 CH open
-	SetBit(P1_AN , PIN5);	//AD13 P15 CH open
-	
-	
-	SetBit(P1_AN , HBMOD);
-	ClrBit(P1_OE, P13);
-	
-	SetBit(P1_AN , PIN4);
-	
-    SetBit(ADC_MASK , CH0EN | CH1EN | CH2EN| CH3EN | CH4EN | CH6EN | CH7EN | CH10EN | CH11EN | CH12EN | CH13EN | CH9EN);
-    
+    // 配置参考电压为5V
+    SetReg(VREF_VHALF_CR, VRVSEL1 | VRVSEL0, VRVSEL0);
+    // 配置半电压为2.5V
+    SetBit(VREF_VHALF_CR, VHALFSEL1 | VHALFSEL0);
+    // 使能VREF输出
+    SetBit(P3_AN, PIN5);
+    SetBit(P3_OE, PIN5);
+    // 使能基准电压与半电压
+    SetBit(VREF_VHALF_CR, VREFEN | VHALFEN);
+    // 端口使能
+    SetBit(P1_AN, PIN6);
+    SetBit(P2_AN, PIN4 | PIN5);
+    SetBit(P3_AN, PIN4);
+    // 通道使能
+    SetBit(ADC_MASK, CH2EN | CH3EN | CH7EN | CH9EN);
     SetBit(ADC_MASK, 0x3000);
-    
     ADC_SCYC  = 0x33;
-    
-	
-    SetBit(ADC_CR , ADCALIGN);
-
-    ClrBit(ADC_CR , ADCIE);
-    SetBit(ADC_CR , ADCEN);
-    
+    // 高位对齐
+    SetBit(ADC_CR, ADCALIGN);
+    ClrBit(ADC_CR, ADCIE);
+    SetBit(ADC_CR, ADCEN);
 }
 

+ 8 - 8
User/Hardware/AMP.c

@@ -21,13 +21,13 @@ void AMP_Init(void)
             SetBit(P3_AN, P31);             //AMP0 Pin设置为模拟模式  +
             SetBit(P3_AN, P30);             //AMP0 Pin设置为模拟模式  -
             SetBit(P2_AN, P27);             //AMP0 Pin设置为模拟模式  O
-            
+        
             ClrBit(AMP_CR0, AMP0M_GND);     // 禁止运放反向输入端内部接地
             SetBit(AMP_CR0, AMP0EN);        //AMP0 Enable
-			
-            AMP_CR0|=(1<<6);                //使能恒功率
-          
-            
+        
+            AMP_CR0 |= (1 << 6);            //使能恒功率
+        
+        
             #if (HW_AMPGAIN == AMP2x)
             {
                 ClrBit(AMP_CR1, AMP0_GAIN2);
@@ -61,19 +61,19 @@ void AMP_Init(void)
         SetBit(P1_AN, P16);             //AMP1 Pin设置为模拟模式  +
         SetBit(P1_AN, P17);             //AMP1 Pin设置为模拟模式  -
         SetBit(P2_AN, P20);             //AMP1 Pin设置为模拟模式  O
-
+    
         SetBit(P2_AN, P21);             //AMP2 Pin设置为模拟模式  +
         SetBit(P2_AN, P22);             //AMP2 Pin设置为模拟模式  -
         SetBit(P2_AN, P23);             //AMP2 Pin设置为模拟模式  O
         ClrBit(P2_OE, P23);             //P23_OE需要强制为0,禁止DA1输出至PAD
-
+    
         SetBit(P3_AN, P31);             //AMP0 Pin设置为模拟模式  +
         SetBit(P3_AN, P30);             //AMP0 Pin设置为模拟模式  -
         SetBit(P2_AN, P27);             //AMP0 Pin设置为模拟模式  O
         SetBit(AMP_CR0, AMP0EN);         //AMP0 Enable
         SetBit(AMP_CR0, AMP1EN);         //AMP1 Enable
         SetBit(AMP_CR0, AMP2EN);         //AMP2 Enable
-        
+    
         #if (HW_AMP_MODE == AMP_NOMAL)      //外部放大
         {
             ClrBit(AMP_CR1, AMP0_GAIN2);

+ 66 - 75
User/Hardware/CMP.c

@@ -3,111 +3,102 @@
 
 void CMP3_Init(void)
 {
-   
-    /******CMP3 端口模拟功能设置*******/    
+    /******CMP3 端口模拟功能设置*******/
     #if (Shunt_Resistor_Mode == Single_Resistor)
     {
-        SetBit(P2_AN , P27);	        //CMP3 Pin设置为模拟模式  + 
-        ClrBit(CMP_CR1 , CMP3MOD1);     //00-->P27-单比较器模式    01-->P20/P23-双比较器模式
-        ClrBit(CMP_CR1 , CMP3MOD0);     //1X-->P20/P23/P27-三比较器模式
+        SetBit(P2_AN, P27);             //CMP3 Pin设置为模拟模式  +
+        ClrBit(CMP_CR1, CMP3MOD1);      //00-->P27-单比较器模式    01-->P20/P23-双比较器模式
+        ClrBit(CMP_CR1, CMP3MOD0);      //1X-->P20/P23/P27-三比较器模式
     }
     #elif (Shunt_Resistor_Mode == Double_Resistor)
     {
-        SetBit(P2_AN , P27);	        //CMP3 Pin设置为模拟模式  + 
-        ClrBit(CMP_CR1 , CMP3MOD1);     //00-->P27-单比较器模式    01-->P20/P23-双比较器模式
-        ClrBit(CMP_CR1 , CMP3MOD0);     //1X-->P20/P23/P27-三比较器模式
+        SetBit(P2_AN, P27);             //CMP3 Pin设置为模拟模式  +
+        ClrBit(CMP_CR1, CMP3MOD1);      //00-->P27-单比较器模式    01-->P20/P23-双比较器模式
+        ClrBit(CMP_CR1, CMP3MOD0);      //1X-->P20/P23/P27-三比较器模式
     }
     #elif (Shunt_Resistor_Mode == Three_Resistor)
     {
-        SetBit(P2_AN , P27 | P23 | P20);// CMP3 Pin设置为模拟模式  + 
-        SetBit(CMP_CR1 , CMP3MOD1);     // 00-->P27-单比较器模式    01-->P20/P23-双比较器模式
-        SetBit(CMP_CR1 , CMP3MOD0);     // 1X-->P20/P23/P27-三比较器模式
+        SetBit(P2_AN, P27 | P23 | P20); // CMP3 Pin设置为模拟模式  +
+        SetBit(CMP_CR1, CMP3MOD1);      // 00-->P27-单比较器模式    01-->P20/P23-双比较器模式
+        SetBit(CMP_CR1, CMP3MOD0);      // 1X-->P20/P23/P27-三比较器模式
     }
-    #endif	//end Shunt_Resistor_Mode
-    
+    #endif  //end Shunt_Resistor_Mode
     #if (COMPARE_MODE == COMPARE_HW_MODE)
     {
         /**P2.6使能其模拟功能,使能数字输出**/
-        SetBit(P2_AN , P26);
-        ClrBit(P2_OE , P26); 
-        ClrBit(DAC_CR , DAC0_1EN);
+        SetBit(P2_AN, P26);
+        ClrBit(P2_OE, P26);
+        ClrBit(DAC_CR, DAC0_1EN);
     }
     #else
     {
-//        /**P2.6使能其模拟功能,使能数字输出**/
-//        SetBit(P2_AN , P26);
-//        SetBit(P2_OE , P26); 
+        //        /**P2.6使能其模拟功能,使能数字输出**/
+        //        SetBit(P2_AN , P26);
+        //        SetBit(P2_OE , P26);
         /* DAC0 Enable */
-        SetBit(DAC_CR , DAC0_1EN);        
+        SetBit(DAC_CR, DAC0_1EN);
         /******************************
-        0: 正常模式,DAC输出电压范围为0到VREF
-        1: 半电压转换模式,DAC输出电压范围为VHALF到VREF
+            0: 正常模式,DAC输出电压范围为0到VREF
+            1: 半电压转换模式,DAC输出电压范围为VHALF到VREF
         ****************************/
-        ClrBit(DAC_CR , DACMOD);
+        ClrBit(DAC_CR, DACMOD);
         /**********设置DAC过流值,舍弃低位精度*****************/
-        
         //  DAC0_DR 是DAC0的高8位,DAC0 的最低位忽略
         DAC0_DR     =   DAC_OverCurrentValue;
     }
-    #endif	//end Compare_Mode
-    
-    SetBit(CMP_CR1 , CMP3HYS);  // CMP3 Hysteresis voltage Enable
-    /*---------------------------------------------------------------------------------
-	选择母线电流保护触发信号源,外部中断0或者比较器3中断。
-	0-CMP3,1-INT0
-    ---------------------------------------------------------------------------------*/
+    #endif  //end Compare_Mode
+    SetBit(CMP_CR1, CMP3HYS);   // CMP3 Hysteresis voltage Enable
+    /*  ---------------------------------------------------------------------------------
+        选择母线电流保护触发信号源,外部中断0或者比较器3中断。
+        0-CMP3,1-INT0
+        ---------------------------------------------------------------------------------*/
     ClrBit(EVT_FILT, INT0_MOE_EN);
+    /*  ---------------------------------------------------------------------------------
+        触发硬件保护后硬件关闭驱动输出MOE配置, 常规应用需要使能MOE自动清零
+        00--MOE不自动清零
+        01--MOE自动清零
+        ----------------------------------------------------------------------------------*/
+    ClrBit(EVT_FILT, MOEMD1);
+    SetBit(EVT_FILT, MOEMD0);
+    /*  ----------------------------------------------------------------------------------
     
     
-    /*---------------------------------------------------------------------------------
-	触发硬件保护后硬件关闭驱动输出MOE配置, 常规应用需要使能MOE自动清零
-	00--MOE不自动清零
-	01--MOE自动清零
-    ----------------------------------------------------------------------------------*/
-    ClrBit(EVT_FILT , MOEMD1);
-    SetBit(EVT_FILT , MOEMD0);
-    /*----------------------------------------------------------------------------------
-    
-    
-    母线电流保护时间滤波宽度
-    00-不滤波    
-    01-4cpu clock    
-    10-8cpu clock    
-    11-16cpu clock
-    -----------------------------------------------------------------------------------*/
-    SetBit(EVT_FILT , EFDIV1);
-    SetBit(EVT_FILT , EFDIV0);
-
-    SetBit(CMP_CR1 , CMP3EN);   //CMP3 Enable
+        母线电流保护时间滤波宽度
+        00-不滤波
+        01-4cpu clock
+        10-8cpu clock
+        11-16cpu clock
+        -----------------------------------------------------------------------------------*/
+    SetBit(EVT_FILT, EFDIV1);
+    SetBit(EVT_FILT, EFDIV0);
+    SetBit(CMP_CR1, CMP3EN);    //CMP3 Enable
 }
 
 /*****************************************************************************
- * Function      : CMP3_Interrupt_Init
- * Description   : CMP3中断配置
- * Input         : void  
- * Output        : None
- * Return        : 
- * Others        : 
- * Record
- * 1.Date        : 20190320
- *   Author      : Bruce HW&RD
- *   Modification: Created function
+    Function      : CMP3_Interrupt_Init
+    Description   : CMP3中断配置
+    Input         : void
+    Output        : None
+    Return        :
+    Others        :
+    Record
+    1.Date        : 20190320
+     Author      : Bruce HW&RD
+     Modification: Created function
 
 *****************************************************************************/
 void CMP3_Interrupt_Init(void)
 {
-    ClrBit(CMP_SR , CMP3IF);
-    /*------------------------------------------------------------------------
-    比较器中断模式配置
-    00: 不产生中断  
-    01: 上升沿产生中断  
-    10: 下降沿产生中断  
-    11: 上升/下降沿产生中断
-    ------------------------------------------------------------------------*/
-    ClrBit(CMP_CR0 , CMP3IM1);
-    SetBit(CMP_CR0 , CMP3IM0);
-    
-    SetBit(IP3 , PCMP31);    // 中断优先级别3
-    SetBit(IP3 , PCMP30);
- 				
+    ClrBit(CMP_SR, CMP3IF);
+    /*  ------------------------------------------------------------------------
+        比较器中断模式配置
+        00: 不产生中断
+        01: 上升沿产生中断
+        10: 下降沿产生中断
+        11: 上升/下降沿产生中断
+        ------------------------------------------------------------------------*/
+    ClrBit(CMP_CR0, CMP3IM1);
+    SetBit(CMP_CR0, CMP3IM0);
+    SetBit(IP3, PCMP31);     // 中断优先级别3
+    SetBit(IP3, PCMP30);
 }

+ 0 - 31
User/Hardware/CRC.c

@@ -1,31 +0,0 @@
-#include <MyProject.h>
-
-
-/****************************************************************************
-CRC16_CCITT_FALSE	硬件实现
-****************************************************************************/
-unsigned short CRC_Check(unsigned char start_sector , unsigned char offset_sector)
-{
-    unsigned short crcresult = 0x0000;
-
-    unsigned short tempH = 0x00;
-    unsigned short tempL = 0x00;
-
-    SetBit(CRC_CR , CRCVAL);		//0-->0x0000  1-->0xffff
-    SetBit(CRC_CR , CRCDINI);		//1-->init success
-
-    CRC_BEG = start_sector;	    //起始扇区
-    CRC_CNT = offset_sector;	//扇区偏移量	0-->1个扇区
-
-    //1个空扇区-->0x41E8	 2个空扇区-->0x1634
-    //1个0xFF  -->0x5B2F	 0x00~0xFF-->0x3FBD
-    SetBit(CRC_CR , AUTOINT);		//自动计算使能
-    /***************************************************/
-
-    SetBit(CRC_CR , CRCPNT);	
-    tempH = CRC_DR;
-    ClrBit(CRC_CR , CRCPNT);	
-    tempL = CRC_DR;
-    crcresult = (unsigned short)(tempH << 8) + tempL;
-    return crcresult;
-}

+ 31 - 0
User/Hardware/DMA.c

@@ -0,0 +1,31 @@
+#include <MyProject.h>
+
+/**
+    @function     Conf_DMA
+    @brief        DMA配置
+    @param[in]    DMAx: DMA通道
+**           DMAPipe: DMA管道,可选参数如下
+                    UART1_XDATA    UART1-->XDATA
+                    XDATA_UART1    UART1<--XDATA
+                    I2C_XDATA      I2C  -->XDATA
+                    XDATA_I2C      I2C  <--XDATA
+                    SPI_XDATA      SPI  -->XDATA
+                    XDATA_SPI      SPI  <--XDATA
+**           DMAAddr: 传输首地址,可取地址范围:0x0000~0x0317
+**           DMALen: DMALen    数据包大小(1~64)
+    @return       None
+    @date         2025-11-05
+*/
+void Conf_DMA(uint8_t DMAx, uint8_t DMAPipe, uint16_t DMAAddr, uint8_t DMALen)
+{
+    // 等待DMAx传输结束
+    Wait_DMA(DMAx);
+    // 禁止DMAx的传输
+    ClrBit(*(&DMA0_CR0 + DMAx), DMAEN);
+    // 设置DMAx的传输管道
+    SetReg(*(&DMA0_CR0 + DMAx), DMA_PIPE, DMAPipe);
+    // 配置长度
+    *(&DMA0_LEN + DMAx) = DMALen - 1;
+    // 配置传输地址
+    *(&DMA0_BA + 2 * DMAx) = DMAAddr & 0x07ff;
+}

+ 40 - 48
User/Hardware/DRIVER.c

@@ -3,62 +3,54 @@
 
 void Driver_Init(void)
 {
-    DRV_ARR = PWM_VALUE_LOAD ;     	// 载波频率的周期值
-    DRV_DTR = PWM_LOAD_DEADTIME;	// 死区时间
+    DRV_ARR = PWM_VALUE_LOAD ;      // 载波频率的周期值
+    DRV_DTR = PWM_LOAD_DEADTIME;    // 死区时间
     DRV_DR  = 0;
     DRV_CMR = 0x0ABF;               // UH/VH/WH UL/VL/WL 互补
     DRV_OUT = 0x00;                 // 空闲电平,默认高电平有效
-
-	/*驱动有效电平和空闲电平*/
-	#if (PWM_LEVEL_MODE == HIGH_LEVEL)
-	{
-        ClrBit(PI_CR , HINV);  // 反相关闭
-        ClrBit(PI_CR , LINV);  // 反相关闭
-	}
-	#elif (PWM_LEVEL_MODE == LOW_LEVEL)
-	{
-        SetBit(PI_CR , HINV);  // 反相打开
-        SetBit(PI_CR , LINV);  // 反相打开
-	}
-	#elif (PWM_LEVEL_MODE == UP_H_DOWN_L)
-	{
-        ClrBit(PI_CR , HINV);  // 反相关闭
-        SetBit(PI_CR , LINV);  // 反相打开
-	}
-	#elif (PWM_LEVEL_MODE == UP_L_DOWN_H)
-	{
-        SetBit(PI_CR , HINV);  // 反相打开
-        ClrBit(PI_CR , LINV);  // 反相关闭
-	}
-	#endif //end PWM_Level_Mode   
-    
-    
-    ClrBit(DRV_SR , FGIE);  //FG中断使能			0-->Disable		1-->Enable
-
+    /*驱动有效电平和空闲电平*/
+    #if (PWM_LEVEL_MODE == HIGH_LEVEL)
+    {
+        ClrBit(PI_CR, HINV);   // 反相关闭
+        ClrBit(PI_CR, LINV);   // 反相关闭
+    }
+    #elif (PWM_LEVEL_MODE == LOW_LEVEL)
+    {
+        SetBit(PI_CR, HINV);   // 反相打开
+        SetBit(PI_CR, LINV);   // 反相打开
+    }
+    #elif (PWM_LEVEL_MODE == UP_H_DOWN_L)
+    {
+        ClrBit(PI_CR, HINV);   // 反相关闭
+        SetBit(PI_CR, LINV);   // 反相打开
+    }
+    #elif (PWM_LEVEL_MODE == UP_L_DOWN_H)
+    {
+        SetBit(PI_CR, HINV);   // 反相打开
+        ClrBit(PI_CR, LINV);   // 反相关闭
+    }
+    #endif //end PWM_Level_Mode
+    ClrBit(DRV_SR, FGIE);   //FG中断使能            0-->Disable     1-->Enable
     /*清中断标志位,使能中断,配置比较值,设置中断优先级*/
-    ClrBit(DRV_SR , DCIF);  //清除DRV中断标志位
+    ClrBit(DRV_SR, DCIF);   //清除DRV中断标志位
     /**************************************************
-    DRV比较匹配中断模式
-    当计数值等于DRV_COMR时,根据DCIM的设置判断是否产生中断标记
-    00:不产生中断        01:上升方向
-    10:下降方向          11:上升/下降方向
+        DRV比较匹配中断模式
+        当计数值等于DRV_COMR时,根据DCIM的设置判断是否产生中断标记
+        00:不产生中断        01:上升方向
+        10:下降方向          11:上升/下降方向
     *************************************************/
-    SetBit(DRV_SR , DCIM1);
-    ClrBit(DRV_SR , DCIM0);
-
+    SetBit(DRV_SR, DCIM1);
+    ClrBit(DRV_SR, DCIM0);
     /*设置DRV计数器的比较匹配值,当DRV计数值与COMR相等时,根据DRV_SR寄存器的DCIM是否产生比较匹配事件*/
     DRV_COMR = (PWM_VALUE_LOAD >> 3);
-
     SetBit(IP0, PDRV1);  //中断优先级设置为1,优先级低于FO硬件过流
     SetBit(IP0, PDRV0);
-    ClrBit(DRV_SR , DCIP);	//0-->1个计数周期产生中断  1-->2个计数周期产生中断
-
-    ClrBit(DRV_CR , FOCEN);
-    /*MESEL为0,ME模块工作在BLDC模式
-    MESEL为1,ME模块工作在FOC/SVPWM/SPWM模式*/
-    SetBit(DRV_CR , MESEL);
-
-    SetBit(DRV_CR , DRVEN);	// 计数器使能		0-->Disable 1-->Enable
-    ClrBit(DRV_CR , DRPE);	// 计数器比较值预装载使能 0-->Disable		        1-->Enable
-    SetBit(DRV_CR , DRVOE);	// Driver输出使能0-->Disable		1-->Enable
+    ClrBit(DRV_SR, DCIP);   //0-->1个计数周期产生中断  1-->2个计数周期产生中断
+    ClrBit(DRV_CR, FOCEN);
+    /*  MESEL为0,ME模块工作在BLDC模式
+        MESEL为1,ME模块工作在FOC/SVPWM/SPWM模式*/
+    SetBit(DRV_CR, MESEL);
+    SetBit(DRV_CR, DRVEN);  // 计数器使能      0-->Disable 1-->Enable
+    ClrBit(DRV_CR, DRPE);   // 计数器比较值预装载使能 0-->Disable                1-->Enable
+    SetBit(DRV_CR, DRVOE);  // Driver输出使能0-->Disable        1-->Enable
 }

+ 6 - 9
User/Hardware/GPIO.c

@@ -2,8 +2,8 @@
 
 
 /**
- * @brief      为提高芯片的抗干扰能力,降低芯片功耗,请在具体项目时,将不需要用的GPIO默认都配置为输入上拉。
- */
+    @brief      为提高芯片的抗干扰能力,降低芯片功耗,请在具体项目时,将不需要用的GPIO默认都配置为输入上拉。
+*/
 void GPIO_Default_Init(void)
 {
     P2_OE = 0;
@@ -18,12 +18,9 @@ void GPIO_Default_Init(void)
     P4_PU = P40 | P41;                        // 使用步进电机时,P40,P41不能配置输入上拉,上拉会导致直通
 }
 /**
- * @brief      GPIO初始化配置,可将I/O口配置成输入或输出模式,上拉还是不上拉,模拟输出还是数字输出
- */
+    @brief      GPIO初始化配置,可将I/O口配置成输入或输出模式,上拉还是不上拉,模拟输出还是数字输出
+*/
 void GPIO_Init(void)
-{  
-   SetBit(P3_OE, P36);
-	
-	
-	
+{
+    SetBit(P3_OE, P36);
 }

+ 0 - 0
User/Hardware/PIInit.c


+ 24 - 31
User/Hardware/SMDU.c

@@ -204,11 +204,6 @@ int16_t LPF_Three_Update(int16_t Xn1, int16_t Xn0, int16_t K)
     return LPF3_YH;
 }
 
-
-
-
-
-
 /*  -------------------------------------------------------------------------------------------------
     Function Name  : Atan_Us_MDU
     Description    : XX-XX-XX
@@ -252,13 +247,13 @@ int16 DivQ_L_MDU2(uint16 Xn1, uint16 Xn0, uint16 Yn)
     return DIV1_DQL;
 }
 
-/** 
- * @brief       计算Is
- * @param[in]   i_alp: alp轴电流
- * @param[in]   i_bet: bet轴电流
- * @return      is
- * @date        2022-07-29
- */
+/**
+    @brief       计算Is
+    @param[in]   i_alp: alp轴电流
+    @param[in]   i_bet: bet轴电流
+    @return      is
+    @date        2022-07-29
+*/
 uint16 Sqrt_alpbet(int16 i_alp, int16 i_bet)
 {
     SCAT2_COS    =   i_alp;
@@ -267,31 +262,29 @@ uint16 Sqrt_alpbet(int16 i_alp, int16 i_bet)
     return SCAT2_RES1;
 }
 
-/** 
- * @brief       乘法器
- * @param[in]   Xn1: 乘数0(16位
- * @param[in]   Xn2: 乘数1(16位)
- * @return      Yn:	 结果
- * @date        2022-11-25
- */
+/**
+    @brief       乘法器
+    @param[in]   Xn1: 乘数0(16位
+    @param[in]   Xn2: 乘数1(16位)
+    @return      Yn:  结果
+    @date        2022-11-25
+*/
 
 int16 MULU_H_MDU(uint16 Xn0, uint16 Xn1)
 {
-	MUL3_MA = Xn0;
-	MUL3_MB = Xn1;
-	SMDU_RunBlock(3,UMUL);
-	return  MUL3_MCH;
-	
+    MUL3_MA = Xn0;
+    MUL3_MB = Xn1;
+    SMDU_RunBlock(3, UMUL);
+    return  MUL3_MCH;
 }
 
-void MULS_H_MDU(int16 Xn0, int16 Xn1,uint16 xdata *lusResult32)
+void MULS_H_MDU(int16 Xn0, int16 Xn1, uint16 xdata * lusResult32)
 {
-	MUL0_MA = Xn0;
-	MUL0_MB = Xn1;
-	SMDU_RunBlock(0,SMUL);
-	
-	*lusResult32 = MUL0_MCH;
-	*(lusResult32+1) = MUL0_MCL;
+    MUL0_MA = Xn0;
+    MUL0_MB = Xn1;
+    SMDU_RunBlock(0, SMUL);
+    *lusResult32 = MUL0_MCH;
+    *(lusResult32 + 1) = MUL0_MCL;
 }
 
 

+ 26 - 94
User/Hardware/TIMER.c

@@ -1,98 +1,30 @@
 #include <MyProject.h>
 
-
-void Timer2_Init(void)
-{
-	
-	ClrBit(PH_SEL , T2SEL);	    //P10
-	ClrBit(PH_SEL , T2SSEL);	//P07	
-
-	SetReg(TIM2_CR0, T2PSC0 | T2PSC1 | T2PSC2,  T2PSC0 );
-	
-	ClrBit(TIM2_CR0 , T2OCM);
-	SetBit(TIM2_CR0 , T2IRE);	//比较匹配中断/脉宽检测中断0-->Disable  1-->Enable
-	ClrBit(TIM2_CR0 , T2CES);	
-	
-	SetBit(TIM2_CR1 , T2IPE);	//输入Timer PWM周期检测中断使能 0-->Disable  1-->Enable
-	ClrBit(TIM2_CR1 , T2IFE);	//计数器上溢中断使能 0-->Disable  1-->Enable
-	ClrBit(TIM2_CR1 , T2FE);	//输入噪声滤波使能,小于4个时钟周期脉宽滤除
-	ClrBit(TIM2_CR1 , T2DIR);	//QEP&ISD&步进模式专用:当前的方向 0-->正向	1-->反向	
-
-	TIM2__CNTR = 0;
-	TIM2__DR = 1200;
-	TIM2__ARR = 24000.;
-    ClrBit(TIM2_CR0 , T2MOD1);	//00-->输入Timer模式  			01-->输出模式
-	SetBit(TIM2_CR0 , T2MOD0);	//10-->输入Counter模式  		11-->QEP&ISD&步进模式
-   
-}
-
-
-/* -------------------------------------------------------------------------------------------------
-    Function Name  : Timer3_Init
-    Description    : 定时器3初始化
-    Date           : 2021-11-08
-    Parameter      : None
-------------------------------------------------------------------------------------------------- */
-void Timer3_Init(void)
-{
-	ClrBit(TIM3_CR1 , T3EN); 
-	SetBit(PH_SEL , T3SEL);     //Timer3端口使能
-  ClrBit(PH_SEL1 , T3CT0);     //默认端口为P11,功能转移后为P01,需TIMER4转移到P00
-
-	ClrBit(TIM3_CR0 , T3PSC2);	//计数器时钟分频选择
-	ClrBit(TIM3_CR0 , T3PSC1);	//000-->24M		001-->12M		010-->6M	011-->3M
-	ClrBit(TIM3_CR0 , T3PSC0);	//100-->1.5M	101-->750K		110-->375K	111-->187.5K
-
-	SetBit(TIM3_CR0 , T3MOD);   //0-->Timer模式       1-->输出模式
-  ClrBit(TIM3_CR0 , T3OCM);
-
-	ClrBit(TIM3_CR0 , T3IRE);	//比较匹配中断/脉宽检测中断0-->Disable  1-->Enable
-	ClrBit(TIM3_CR0 , T3OPM);	//0-->计数器不停止		1-->单次模式	
-
-	ClrBit(TIM3_CR1 , T3IPE);	//输入Timer PWM周期检测中断使能 0-->Disable  1-->Enable
-	ClrBit(TIM3_CR1 , T3IFE);	//计数器上溢中断使能 0-->Disable  1-->Enable
-
-	SetBit(TIM3_CR1 , T3NM1);	//输入噪声脉宽选择
-	ClrBit(TIM3_CR1 , T3NM0);	//00-->不滤波	01-->4cycles    10-->8cycles  11-->16cycles
-
-	ClrBit(IP2,PTIM31 );
-	SetBit(IP2,PTIM30 );      //中断优先级设置为1
-
-	TIM3__CNTR = 1;
-	TIM3__ARR =1200;
-	TIM3__DR = 1200;
-
-  SetBit(TIM3_CR1 , T3EN);    //TIM3使能    0-->Disable  1-->Enable
-}
-
-
 /**
- * @brief      TIM4初始化配置
- */
-void Timer4_Init(void)
+    @function     PWM_Timer_Init
+    @brief        输入捕获 TIM3实现
+    @date         2025-12-27
+*/
+void PWM_Timer_Init(void)
 {
-    ClrBit(TIM4_CR1, T4EN);                                         // 0 - 停止计数;1 - 使能计数
-    SetBit(PH_SEL, T4SEL);  //端口复用为 Timer4
-	
-    ClrBit(PH_SEL1,T4CT0); //Timer4 功能转移 00: P0.1 为 Timer4 输入输出
-		ClrBit(PH_SEL1,T4CT1);
-		SetReg(TIM4_CR0, T4PSC0 | T4PSC1 | T4PSC2,  T4PSC0 );
-	
-    SetBit(TIM4_CR0 , T4OCM);
-    ClrBit(TIM4_CR0 , T4IRE);	//比较匹配中断/脉宽检测中断0-->Disable  1-->Enable
-    ClrBit(TIM4_CR0 , T4OPM);	//0-->计数器不停止		1-->单次模式	
-	
-    ClrBit(TIM4_CR1 , T4IPE);	//输入Timer PWM周期检测中断使能 0-->Disable  1-->Enable
-    ClrBit(TIM4_CR1 , T4IFE);	//计数器上溢中断使能 0-->Disable  1-->Enable
-    ClrBit(TIM4_CR1 , T4NM1);	//输入噪声脉宽选择
-    ClrBit(TIM4_CR1 , T4NM0);	//00-->不滤波	01-->4cycles    10-->8cycles  11-->16cycles
-
-		ClrBit(IP2,PTIM40);
-		SetBit(IP2,PTIM41);
-	
-		TIM4__DR   = 12000;                                             //输入模式,DR和ARR的值由硬件写;
-    TIM4__ARR  = 24000.;
-    TIM4__CNTR = 0;
-    SetBit(TIM4_CR0 , T4MOD);   //0-->Timer模式       1-->输出模式
-		ClrBit(TIM4_CR1 , T4EN);    //TIM4使能    0-->Disable  1-->Enable
-}
+    ClrBit(TIM3_CR1, T3EN);
+    // 配置端口复用 GP11
+    SetBit(PH_SEL, T3SEL);
+    ClrBit(PH_SEL1, T3CT1 | T3CT0);
+    // 配置定时器时钟 750KHz
+    SetReg(TIM3_CR0, T3PSC2 | T3PSC1 | T3PSC0, T3PSC2 | T3PSC0);
+    // 模式配置
+    ClrBit(TIM3_CR0, T3MOD);
+    ClrBit(TIM3_CR0, T3OCM | T3OPM);
+    // 中断配置
+    SetBit(TIM3_CR0, T3IRE);
+    ClrBit(TIM3_CR1, T3IR | T3IF | T3IP);
+    SetBit(TIM3_CR1, T3IPE | T3IFE);
+    // 计数值配置
+    TIM3__CNTR = 0;
+    TIM3__DR = 0;
+    TIM3__ARR = 0;
+    // 配置中断优先级
+    SetReg(IP2, PTIM31 | PTIM30, PTIM30);
+    SetBit(TIM3_CR1, T3EN);
+}

+ 1 - 1
User/Hardware/TSD.c

@@ -4,7 +4,7 @@ void TSD_Init(void)
 {
     // 使能中断
     TSDIE = 1;
-	// 使能TSD
+    // 使能TSD
     SetBit(TSD_CR, TSDEN);
     // 配置优先级
     SetBit(IP0, PLVW_TSD1 | PLVW_TSD0);

+ 32 - 0
User/Hardware/peripheral.h

@@ -0,0 +1,32 @@
+/**
+    @copyright None
+    @file      peripheral.h
+    @author    Comment Vivre
+    @date      2025-12-29
+    @brief     None
+*/
+#ifndef __PERIPHERAL_H_
+#define __PERIPHERAL_H_
+
+void TSD_Init(void);
+
+void ADC_Init(void);
+void AMP_Init(void);
+
+void CMP3_Init(void);
+void CMP3_Interrupt_Init(void);
+
+
+
+void Driver_Init(void);
+void GPIO_Init(void);
+
+void PWM_Timer_Init(void);
+
+void UART1_Init(void);
+void Conf_DMA(uint8_t DMAx, uint8_t DMAPipe, uint16_t DMAAddr, uint8_t DMALen);
+
+
+#endif
+
+

+ 0 - 121
User/include/Customer.h

@@ -1,121 +0,0 @@
-#ifndef __CUSTOMER_H_
-#define __CUSTOMER_H_
-
-#define PWM_FREQUENCY                   	(16.0)
-#define PWM_DEADTIME                    	(1.0)
-#define MIN_WIND_TIME                   	(PWM_DEADTIME + 0.9)
-#define DLL_TIME                        	(PWM_DEADTIME + 0.3)
-
-#define PWM_LEVEL_MODE                  	(HIGH_LEVEL)
-
-#define Pole_Pairs                      	(4.0)
-#define RS                              	(0.135)
-#define LD                              	(0.00004)
-#define LQ                             	 	(0.00008)
-#define MOTOR_SPEED_BASE                	(9000.0)
-#define Ke                              	(1.8)
-
-#define IR_MODE                         	(CW)
-
-#define HW_AMP_MODE                     	(AMP_PGA_DUAL)     
-
-#define HW_RSHUNT                       	(0.01) 
-#define HW_AMPGAIN                      	(AMP4x)                    
-#define HW_ADC_VREF                    		(VREF5_0)
-
-#define Shunt_Resistor_Mode            		(Single_Resistor)
-
-#define VREF_OUT_EN                     	(1)
-#define VHALF_EN                        	(1)
-
-
-#define RV1                             	(20.0)
-#define RV2                             	(3.0)
-
-#define CHARGE_EN                      		(0)
-#define CHARGE_DUTY                    		(0.1)
-#define CHARGE_TIME                    		(30)
-
-
-#define ALIGN_MOME                     		(ALIGN_NOMAL)
-
-
-#define AlignRamp_Time                  	(800)
-#define Align_Time1                     	(200)
-#define Align_Time2                     	(1000)
-#define Align_Time3                     	(200)
-#define AlignAll_Time                  		(AlignRamp_Time+Align_Time1+Align_Time2+Align_Time3)
-#define Align_Angle1                   		_Q15(90.0/180.0)                       // 第一次预定位角度
-#define Align_Angle2                   		_Q15(90.0/180.0)                        // 第二次预定位角度
-#define Angle_ADD                      		(int32)6*_Q15((float)-180.0/180.0)       //旋转角度
-#define Angle_AngleK                    	(float)Angle_ADD/Align_Time2            //旋转系数
-
-#define DQKP_Alignment                 		_Q12(2.0)
-#define DQKI_Alignment                 		_Q15(0.01)
-#define ID_Align_CURRENT               		I_Value(0.0)
-#define IQ_Align_CURRENT               		I_Value(2.0)
-
-#define Open_Start_Mode                		(Omega_Start)
-#define ID_Start_CURRENT               		I_Value(0.0)
-#define IQ_Start_CURRENT               		I_Value(5.0)
-
-
-#define ATO_BW_START                   		(0.0)
-#define ATO_BW_RUN1                    		(150.0)
-#define ATO_BW_RUN2                    		(150.0)
-#define ATO_BW_RUN3                    		(150.0)
-#define ATO_BW_RUN4                    		(150.0)
-
-#define ATO_START_HOLDTIME             		(10)             ///< (ms)观测器带宽的滤波值,启动第一拍ATO持续时间
-#define ATO_RAMP_PERIOD                		(20)             ///< (ms)观测器带宽的滤波值,ATO爬坡递增 间隔时间
-
-#define MOTOR_OMEGA_RAMP_ACC           		(50)               ///< omega启动的增量(每个载波递增值) 
-#define MOTOR_OMEGA_RAMP_MIN          		S_Value(200.0)     ///< (RPM) omega启动的最小切换转速
-#define MOTOR_OMEGA_RAMP_END           		S_Value(300.0)     ///< (RPM) omega启动的限制转速
-#define MOTOR_SPEED_SMOMIN_RPM         		(600.0)            ///< (RPM) SMO运行最小转速影响启动
-
-#define SPD_BW                         		(20.0)            ///< 速度带宽的滤波值,经典值为5.0-40.0
-#define MOTOR_LOOP_RPM                 		S_Value(400.0)    ///< (RPM) 启动后电流环切入外部环路转速阈值
-
-
-#define MOTOR_SPEED_MIN_RPM            		S_Value(1800.0)
-#define MOTOR_SPEED_MAX_RPM            		S_Value(3500.0)
-#define MOTOR_SPEED_STOP_RPM           		S_Value(200.0)
-
-
-#define DKPStart                      		_Q12(4.5)
-#define DKIStart                      		_Q15(0.03)
-#define QKPStart							DKPStart
-#define QKIStart							DKIStart
-
-#define DKP                            		_Q12(3.5)
-#define DKI                            		_Q15(0.005)
-#define QKP                            		DKP
-#define QKI                            		DKI
-
-// DQ轴输出限幅
-#define DOUTMAX                        		_Q15(0.99)
-#define DOUTMIN                        		_Q15(-0.99)
-#define QOUTMAX                        		_Q15(0.99)
-#define QOUTMIN                        		_Q15(-0.99)
-
-
-#define LOOP_TIME                       	(1)
-
-#define SKP                            		_Q12(2.5)
-#define SKI                            		_Q15(0.01)
-#define SOUTMAX                       	 	I_Value(10.0)
-#define SOUTMIN                        		I_Value(0.01)
-
-#define SPEEDRAMPINC                    	(S_Value(250.0))/(1000/LOOP_TIME)
-#define SPEEDRAMPDEC                    	(S_Value(250.0))/(1000/LOOP_TIME)
-
-#define SPEEDRAMPSTARTINC               	(S_Value(1000.0))/(1000/LOOP_TIME)
-#define SPEEDRAMPSTARTDEC               	(S_Value(500.0))/(1000/LOOP_TIME)
-
-#define SPEED_MODE                     		(NONEMODE)
-
-#define EstimateAlgorithm              		(AO)
-#define OverModulation                 		(0)
-
-#endif

+ 36 - 173
User/include/MyProject.h

@@ -1,17 +1,27 @@
 #ifndef __MYPROJECT_H_
 #define __MYPROJECT_H_
 
-#include "FU6522_72.h"
+#include <FU6522_72.h>
+#include <FU6522_72_MDU.h>
+
+#include <customer.h>
+#include <protect.h>
+
 
 #include <Definition.h>
-#include <Customer.h>
 #include <Parameter.h>
-#include <Protect.h>
 
 
-#include <FU6522_72_MDU.h>
+#include <peripheral.h>
+
+#include <control.h>
+#include <protect_def.h>
+
+#include <led.h>
+#include <pwm.h>
+#include <protocol.h>
+
 
-#define     S_Value(SpeedValue)             _Q15(SpeedValue/MOTOR_SPEED_BASE)
 typedef struct
 {
     int16   IuOffset;       //Iu的偏置电压
@@ -23,9 +33,6 @@ typedef struct
     
     int16   IbusOffset;   //Ibus的偏置电压
     int32   IbusOffsetSum;//IIbus的偏置电压总和
-    
-    
-    
     int16   OffsetCount;    //偏置电压采集计数
     int8    OffsetFlag;     //偏置电压结束标志位
     
@@ -34,196 +41,52 @@ typedef struct
 extern CurrentOffset xdata mcCurOffset;
 extern int16 idata Power_Currt;
 
-extern uint8 Drv_SectionCheak(void);
+void GetCurrentOffset(void);
 
-extern void GetCurrentOffset(void);
-extern void Motor_Ready (void);
-extern void Motor_Init (void);
+void FOC_Init(void);
+void Motor_Charge(void);
+void Motor_Static_Open(void);
 
-extern void FOC_Init(void);
-extern void Motor_Charge(void);
-extern void MC_Stop(void);
-extern void MC_Break(void);
-extern void Motor_Static_Open(void);
-
-
-extern void Motor_Align(void);
-
-typedef enum
-{
-    mcReady     = 0,    ///< 准备状态,该状态电机空闲,等待控制命令
-    mcInit      = 1,    ///< 初始化,该状态进行启动前的变量初始化
-    mcCharge    = 2,    ///< 预充电,电机启动前给自举电容充电,一般用于高压驱动,低压驱动一般不需要
-    mcTailWind  = 3,    ///< 顺逆风检测,该状态下电机进行顺逆风检测
-    mcPosiCheck = 4,    ///< 初始位置检测
-    mcAlign     = 5,    ///< 预定位
-    mcStart     = 6,    ///< 启动,用于配置启动代码
-    mcRun       = 7,    ///< 运行,
-    mcStop      = 8,    ///< 停止
-    mcFault     = 9,    ///< 故障状态
-    mcBrake     = 10,   ///< 刹车
-} MotStateType;
 
+void Motor_Align(void);
 
 
 typedef union
 {
-    uint8 SetMode;                                                              ///< 整个配置模式使能位
+    uint8 SetMode;
     struct
     {
-        uint8 CalibFlag        : 1;                                             ///< 电流校准的标志位
-        uint8 ChargeSetFlag    : 1;                                             ///< 预充电配置标志位
-        uint8 AlignSetFlag     : 1;                                             ///< 预定位配置标志位
-        uint8 TailWindSetFlag  : 1;                                             ///< 顺逆风配置标志位
-        uint8 StartSetFlag     : 1;                                             ///< 启动配置标志位
-        uint8 PosiCheckSetFlag : 1;                                             ///< 位置检测配置标志位
-        uint8 Break            : 1;                                             ////</ 刹车配置标志位
-    } SetFlag;
+        uint8 CalibFlag        : 1;
+        uint8 ChargeSetFlag    : 1;
+        uint8 AlignSetFlag     : 1;
+		uint8 StartSetFlag     : 1;
+	} SetFlag;
 } MotStaM;
 
 
-
-extern MotStateType data mcState;
-
 extern MotStaM    McStaSet;
 
-extern void MC_Control(void);
-
 
 typedef struct
 {
-    uint16 mcDcbusFlt;                                                          // 母线电压
-    uint16 mcADCCurrentbus;                                                     // 母线电流
-    int16  NTCTempFlt;                                                          // NTC电压值
-    uint16 PowerCal;
-    uint32 PowerFlt;                                                            // 功率滤波后的值
-    int16  SpeedFlt;                                                            // 当前速度滤波后的值
-    
-    uint8  ChargeStep;                                                          // 预充电的步骤
-    uint8  CtrlMode;                                                            // 控制模式
-    
-    int16  Max_ia;
-    int16  Max_ib;
-    int16  Max_ic;
-    
-    uint8  Flg_ATORampEnd;
-    
-    uint16 Ref;                                                                 // 控制目标给定
-    uint16  IqRef;                                                               // Q轴给定电流
-    uint16  IdRef;                                                               // D轴给定电流
-    uint16  IqSpeedRef;                                                          // Q轴速度给定电流
-    
-    uint8  Mode0HoldCnt;                                                        // MODE0切MODE1延迟时间计数
-    
-    uint16  EMFsquare;                                                             // 当前FOC_ESQU滤波后的值
-    
-    uint32 RunStateCnt;                                                         // 运行状态计数
-    
-    uint8 LoopTime;                                                            // 外环控制周期计数
-    
-    uint16 State_Count;                                                         // 电机各个状态的时间计数
-    
-    
-    int16  mcPosCheckAngle;                                                   // 位置检测的角度
-    
-    
-    uint8  CurrentAlignStatus;           // 预定位状态
-    
-    
+    uint8  ChargeStep;
+	uint8  Flg_ATORampEnd;
+
+    uint16 State_Count;
+    int16  mcPosCheckAngle; 
+    uint8  CurrentAlignStatus;
     float AngleProcess;
     int16 AngleStart;
 } FOCCTRL;
 
 
+extern FOCCTRL xdata mcFocCtrl;
 
-typedef struct
-{
-    uint16  RefValue;
-    float  OutValue_float;
-    float  IncValue;
-    float  DecValue;
-} MCRAMP;
-
-extern bool isCtrlPowOn;
-extern FOCCTRL              xdata mcFocCtrl;
-extern MCRAMP               xdata mcRefRamp;
-
-extern void   HardwareInit(void);
-extern void   SoftwareInit(void);
-
-extern void   TargetRef_Process(void);
-extern void   Speed_response(void);
-extern int16  Motor_Ramp(int16 ref);
+void ATORamp(void);
 
-extern void   ATORamp(void);
-
-extern void   TickCycle_1ms(void);
-
-extern void   Fault_GetCurrentOffset(void);
-extern uint32 Abs_F32(int32 value);
 extern uint16 Abs_F16(int16 value);
 
-extern int16  PowerSpeedLimit(int16 speedref);
-
-#define TIM3_Fre                       (750000.0)                                // TIM0计数频率750KHz
-#define TIM4_Fre                       (12000000.0)                               // TIM4计数频率
-
-/*************************************************************************************///External Function
-extern void Timer1_Init(void);
-extern void Timer2_Init(void);
-extern void Timer3_Init(void);
-extern void Timer4_Init(void);
-extern void TIM4_Init_RF(void);
-
-
-
-extern void PI_Init(void);
-extern void PI2_Init(void);
-extern void PI3_Init(void);
-
-
-
-
-void TSD_Init(void);
-
-void ADC_Init(void);
-void AMP_Init(void);
-
-void CMP3_Init(void);
-void CMP3_Interrupt_Init(void);
-
-unsigned short CRC_Check(unsigned char start_sector, unsigned char offset_sector);
-
-void Driver_Init(void);
-
-void GPIO_Init(void);
-
-typedef enum
-{
-    FaultNoSource           = 0,  ///< 无故障
-    FaultHardOVCurrent      = 1,  ///< 硬件过流
-    FaultSoftOVCurrent      = 2,  ///< 软件过流
-    FaultOverVoltageDC      = 3,  ///< 过压
-    FaultUnderVoltageDC     = 4,  ///< 欠压
-    FaultPhaseLost          = 5,  ///< 缺相
-    FaultStall              = 6,  ///< 堵转
-    FaultStart              = 7,  ///< 启动保护
-    FaultFan                = 8,  ///< 风机保护
-    FaultOverHeat           = 9,  ///< 电机过热保护
-    FaultOverTemperature    = 10, ///< NTC过温保护
-    FaultOverPower          = 11, ///<过功率
-    FaultLVW                = 12, ///< LVW
-    FaultGetOffset          = 13, ///< 偏置电压保护
-    FaultOverSpeed          = 14,
-    FaultOverLSensor        = 15,
-    FaultOverRSensor        = 16,
-    FaultUartLost           = 17,
-    FaultFan1               = 18,
-    
-    FaultE      = 18,
-} FaultStateType;
-
-
-extern FaultStateType data mcFaultSource;
+void Config_Init(void);
 
+	
 #endif

+ 0 - 16
User/include/Parameter.h

@@ -65,20 +65,10 @@
 #define RV                              ((RV1 + RV2 ) / RV2)               ///< 分压比
 
 
-/* -----时间设置值----- */
-#define Calib_Time                     (1000)  
-/* -----保护参数值----- */
-#define GetCurrentOffsetValueLow     (16383-GetCurrentOffsetValue)                                                      ///<  偏置电压差值低点 
-#define GetCurrentOffsetValueHigh    (16383+GetCurrentOffsetValue)                                                      ///<  偏置电压差值高点 
-/* -----过温保护值设置----- */
-#define   Tempera_Value(NTC_Value) 		_Q15((5.0*NTC_Value/(10.0+NTC_Value))/HW_ADC_REF)									    ///< 10K上拉电阻时,NTC阻值对应Q15_AD值,单位:KΩ
-/*电流基准的电路参数*/
 #define HW_BOARD_CURR_MAX               (HW_ADC_REF / 2 / HW_AMPGAIN / HW_RSHUNT)                     ///< 最大采样电流
 #define HW_BOARD_CURR_MIN               (-HW_BOARD_CURR_MAX)                                          ///< 最小采样电流
 #define HW_BOARD_CURR_BASE              (HW_BOARD_CURR_MAX * 2)                                       ///< 电流基准
 
-/*hardware voltage sample Parameter*/
-/*母线电压采样分压电路参数*/
 #define HW_BOARD_VOLT_MAX               (HW_ADC_REF * RV)                           ///< (V)  ADC可测得的最大母线电压
 #define HW_BOARD_VOLTAGE_BASE           (HW_BOARD_VOLT_MAX / 1.732)                 ///< 电压基准
 
@@ -156,10 +146,4 @@
 #define OBS_KSLIDE                     _Q15(0.85)       ///< SMO算法里的滑膜增益值
 #define E_BW                           (450.0)          ///< PLL算法里的反电动势滤波值
 
-/* 功率保护 */
-#define PowerProtectValue                P_Value(105)               // 功率上限值 6500:300W
-#define PowerProtectValue1               P_Value(120)               // 功率上限值 6500:300W
-#define PowerLimit                       (100.0)                                      // 功率上限值 6500:300W
-#define PowerLimitK                      (18.0)
-
 #endif

+ 0 - 129
User/include/Protect.h

@@ -1,129 +0,0 @@
-/**
- * @copyright (C) COPYRIGHT 2022 Fortiortech Shenzhen
- * @file      Protect.h
- * @author    Marcel He
- * @note      Last modify author is Marcel He
- * @since     2022-07-01
- * @date      2022-07-14
- * @brief     This file contains protection parameter used for Motor Control.
- */
-
-
-/* Define to prevent recursive inclusion -------------------------------------------------------- */
-#ifndef __PROTECT_H_
-#define __PROTECT_H_
-
-#define OC_SW_ProtectEn                     (1)                                  // 软件过流保护使能: 0,不使能;1,使能
-#define OV_ProtectEn                        (1)                                  // 过欠压保护使能:   0,不使能;1,使能
-#define LP_ProtectEn                        (1)                                  // 缺相保护使能:     0,不使能;1,使能
-#define OT_ProtectEn                        (1)                                  // 过温保护使能:     0,不使能;1,使能
-#define Start_ProtectEn                     (1)
-#define Stall_ProtectEn                     (1)                                  // 堵转失速保护使能: 0,不使能;1,使能
-#define GetCurrentOffsetEnable              (1)                                  // 偏置电压保护,0,不使能;1,使能
-#define OverSpeedProtectEnable				(0)                                  //< 超速保护使能
-#define OverLSensorEnable                   (0)
-#define OverRSensorEnable                   (0)
-#define OverUARTEnable                      (0)
-#define OverPowerEnable                     (1)
-
-/*  保护重启参数设置  */
-#define OC_RecoveryTimes                    (100)                                  // 重启次数,设定值<255 达到重启次数后不再重启,设定值>=255,一直重启
-#define OC_RecoveryDelayTime                (1000)                               // (ms)重启延迟时间
-
-#define OV_RecoveryTimes                    (10)                                   // 重启次数,设定值<255 达到重启次数后不再重启,设定值>=255,一直重启
-#define OV_RecoveryDelayTime                (1000)                                // (ms)重启延迟时间
-
-#define LP_RecoveryTimes                    (3)                                   // 重启次数,设定值<255 达到重启次数后不再重启,设定值>=255,一直重启
-
-#define LP_RecoveryDelayTime                (3000)                                // (ms)重启延迟时间
-#define LP_RecoveryDelayTime1               (60000)
-
-#define OT_RecoveryTimes                    (20)                                   // 重启次数,设定值<255 达到重启次数后不再重启,设定值>=255,一直重启
-#define OT_RecoveryDelayTime                (60000)                                // (ms)重启延迟时间
-#define Start_RecoveryTimes                 (0)                                 // 重启次数,设定值<255 达到重启次数后不再重启,设定值>=255,一直重启
-#define Stall_RecoveryTimes                 (255)                                 // 重启次数,设定值<255 达到重启次数后不再重启,设定值>=255,一直重启
-#define Stall_RecoveryDelayTime             (60000*5)                                // (ms)重启延迟时间
-
-#define LRS_RecoveryTimes                    (200)                                   // 重启次数,设定值<255 达到重启次数后不再重启,设定值>=255,一直重启
-#define LRS_RecoveryDelayTime                (1000)                                // (ms)重启延迟时间
-/* HW current protection */
-#define HW_OC_FO_MODE                       (1)                                     ///< 硬件FO过流保护使能,适用于IPM有FO保护的场合
-#define HW_OC_CMP_MODE                      (2)                                     ///< 硬件CMP比较过流保护使能,适用于MOS管应用场合
-#define HW_OC_FOCMP_MODE                    (3)                                     ///< 硬件CMP比较和FO过流保护都使能
-#define HW_OC_DISABLE                       (4)                                     ///< 硬件过流保护禁止,用于测试
-#define HW_OC_MODE                          (HW_OC_CMP_MODE)                        ///< 硬件过流保护实现方式
-
-/* 硬件过流保护比较值来源 */
-#define COMPARE_DAC_MODE                    (0)                                     ///< DAC设置硬件过流值
-#define COMPARE_HW_MODE                     (1)                                     ///< 硬件设置硬件过流值
-#define COMPARE_MODE                        (COMPARE_DAC_MODE)                      ///< 硬件过流值的来源
-#define HWOCValue                           (40.0)                                  ///< (A) DAC模式下的硬件过流值//18.0
-
-/* 软件过流保护参数设置 */
-#define SW_OC_CurrentVal                    I_Value(35.0)                            ///< (A)软件过流值
-#define SW_OC_DectTime                      (100)                                    ///< (ms)软件过流检测时间
-
-/* -----偏置电压保护----- */
-#define GetCurrentOffsetValue              _Q14(0.05)                               // (单位:100%)偏置电压保护误差范围,超过该范围保护   
-
-/* 直流母线电压保护参数设置值 */
-
-#define OVER_VOLTAGE_DECTTIME               (100)                                   ///< (ms) 直流母线电压过压检测保持时间
-#define UNDER_VOLTAGE_DECTTIME              (100)                                   ///< (ms) 直流母线电压欠压检测保持时间
-
-#define OVER_VOLTAGE_PROTECT                UDC_Value(30.0)                          ///< (V) 直流母线电压过压保护值
-#define UNDER_VOLTAGE_PROTECT               UDC_Value(9.6)                          ///< (V) 直流母线电压欠压保护值
-
-#define OVER_VOLTAGE_RECOVER                UDC_Value(28.0)                          ///< (V) 直流母线电压过压保护恢复值            
-#define UNDER_VOLTAGE_RECOVER               UDC_Value(10.6)                          ///< (V) 直流母线电压欠压保护恢复值
-
-/* 启动保护参数设置值 */
-#define StartProtectRestartTimes       (10)                                       // 启动保护重启次数,单位:次
-#define StartRecoverDelay1             (30)
-#define StartRecoverDelay2             (12000)//12000
-#define StartProtect1EsValue           (5500)
-#define StartProtect2EsValue           (2500)//500
-
-/* 堵转保护参数设置值 */
-#define STALL_SPEED_MAX                     S_Value(4500)                                                     //150000 can enter the water shortage protection 
-#define STALL_SPEED_MIN                     S_Value(2200)
-
-#define EsThresholdValueL                   (2600.0)
-#define EsThresholdValueH                   (2900.0)
-#define EsThresholdSpeed                    S_Value(2500)                          ///< (RPM) 电机转速
-
-
-/* 缺相保护参数设置值 */
-#define LP_NoLoadCurrentValue               I_Value(0.3)                            ///< (A) 相电流峰值小于该电流值则不进行缺相判断
-#define LP_DectDealyTIME                    (500)                                   ///< (ms) 启动延迟检测时间,用于屏蔽启动阶段
-#define LP_DectCycleTIME                    (10)                                    ///< (ms) 峰值检测周期时间,需满足覆盖至少一个完整电周期(50)
-
-/* -----NTC过温保护----- */
-#define TemperatureProtectTime                          (60000)                                                             // (ms)温度保护检测时间
-#define TempeValue(res)                (short)(_Q15((5*(1000.0/(res+1000.0)))/HW_ADC_REF)) //  温度保护AD值换算
-#define OVER_Temperature                                TempeValue(1014)                                        // 过温保护值100℃  100度(1014)
-//#define UNDER_Temperature                             (800.0)                                     // 过温保护恢复阈值,根据NTC曲线设定,10K上拉电阻,70℃
-#define OvertempRec_Value                   TempeValue(1878)
-#define OvertempLimit_Value                 TempeValue(1369)
-
-/* -----堵入风口超速保护----- */
-#define  MOTOR_SPEED_OVER_RPM                           (70000)                                 ///< (RPM) 超速保护速度
-#define  OVER_SpeedDetectTime                               (1400)                                  ///< (ms) 超速保护检测时间
-
-/* -----堵入风口限速功能----- */
-#define  OverSpeedLimitEnable                               (1)                                     ///< 限速功能,0,不使能,1使能
-#define  MOTOR_SPEED_LIMIT_RPM                  (8000.0)                                ///< (RPM) 限速保护最大速度
-
-#define  MOTOR_SPEED_LIMIT0_RPM                 (700.0)                                 ///< (RPM) For Lack water protection 
-
-/* 故障显示 */
-#define LED_ONTime                          (100)
-#define LED_OFFTime                         (233)
-#define LED_ONOFFTime                       (LED_ONTime + LED_OFFTime)
-#define LED_DelayTime                       (200)
-#define LED_IntervalTime                    (1000)
-
-
-
-#endif
-

+ 7 - 2
User/include/definition.h

@@ -18,6 +18,7 @@
 #define S_Value(Xn0)             		_Q15((float)(Xn0) / MOTOR_SPEED_BASE)
 #define A_Value(Xn0)                  	_Q15((float)(Xn0) / 180.0)
 #define UDC_Value(Xn0)                  _Q15((Xn0)/ HW_BOARD_VOLT_MAX)
+#define TEMPER_VALUE(Xn0)           	_Q15(5.0*Xn0/(10+Xn0)/HW_ADC_REF)
 
 #define Disable                         (0)                              
 #define Enable                          (1)
@@ -68,7 +69,7 @@
 #define NONEMODE                        (0xA0)          ///< 直接给定值,不调速
 #define PWMMODE                         (0xB0)          ///< PWM调速
 #define SREFMODE                        (0xC0)          ///< 模拟调速
-#define ONOFFTEST                       (0xD0)          ///< 启停测试工具
+#define STARTTEST                   	(0xD0)
 
 #define SMO                             (0xA0)          ///< SMO ,滑膜估算
 #define PLL                             (0xB0)          ///< PLL ,锁相环
@@ -87,6 +88,10 @@
 #define Long_Inject                     (0)             ///< 脉冲注入时间长于2ms,若时间长于4ms,则要修改定时器分频
 #define Short_Inject                    (1)             ///< 脉冲注入时间低于2ms
 
-
+// FOC__POW转换实际功率公式
+// P = FOC__POW/32767*HW_BOARD_VOLT_MAX*HW_BOARD_CURR_MAX 732 6.25
+#define OVER_POWER_VAL              	((float)(32767/HW_BOARD_VOLT_MAX/HW_BOARD_CURR_MAX*OVER_POWER_VALUE))
+#define LPF_FC(Xn0)                	 	_Q15(_2PI / 1000 * Xn0)
+#define S_Value(Xn0)             		_Q15(Xn0/MOTOR_SPEED_BASE)
 
 #endif

Some files were not shown because too many files changed in this diff