Interrupt.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. @copyright (C) COPYRIGHT 2022 Fortiortech Shenzhen
  3. @file Interrupt.c
  4. @author Fortiortech Appliction Team
  5. @date 2022-07-13
  6. @brief This file contains interrupt function used for Motor Control.
  7. */
  8. #include <MyProject.h>
  9. extern uint8 data g_1mTick; ///< 1ms滴答信号,每隔1ms在SYSTICK定时器被置1,需在大循环使用处清零
  10. uint16 xdata spidebug[4] = {0}; ///< SPI debug 输出通道缓存,SPI调试器会将该变量值进行输出
  11. //uint16 xdata Power_Currt;
  12. /**
  13. @brief 低于预警中断与过温中断
  14. @brief 开启低压检测中断后,MCU会对输入电压进行监测,当输入电压低于设定值,则会触发中断
  15. @brief 开启过温保护中断后,MCU会对内部结温进行监测,当内部结温高于设定值,则会触发中断
  16. @date 2022-07-14
  17. */
  18. void LVW_TSD_INT(void) interrupt 0 // LVW & TSD interrupt
  19. {
  20. if (ReadBit(LVSR, LVWIF))
  21. {
  22. if (ReadBit(LVSR, LVWF))
  23. {
  24. mcFaultSource = FaultUnderVoltageDC;
  25. ClrBit(LVSR, LVWF);
  26. }
  27. ClrBit(LVSR, LVWIF);
  28. }
  29. if (TSDIF)
  30. {
  31. if (ReadBit(LVSR, TSDF))
  32. {
  33. mcFaultSource = FaultTSD;
  34. ClrBit(LVSR, TSDF);
  35. }
  36. TSDIF = 0;
  37. }
  38. }
  39. /**
  40. @brief 外部中断0
  41. @brief 一般用于响应IPM的FO过流信号
  42. @date 2022-07-14
  43. */
  44. void EXTERN0_INT(void) interrupt 1 // 外部中断0
  45. {
  46. if (IF0)
  47. {
  48. IF0 = 0; // clear P00 interrupt flag
  49. }
  50. }
  51. /**
  52. @brief FOC中断(Drv中断),每个载波周期执行一次,用于处理响应较高的程序,中断优先级第二
  53. @date 2022-07-14
  54. */
  55. void DRV_ISR(void) interrupt 3
  56. {
  57. if (ReadBit(DRV_SR, FGIF))
  58. {
  59. ClrBit(DRV_SR, FGIF);
  60. }
  61. if (ReadBit(DRV_SR, DCIF)) // 比较中断
  62. {
  63. #if (DBG_MODE == DBG_SPI_SW) // 软件调试模式
  64. spidebug[0] = mcFocCtrl.SPIVar3; // mcFocCtrl.mcDcbusFlt;
  65. spidebug[1] = FOC__UQ;
  66. // spidebug[2] = FOC__THETA; // mcBemf.BEMFSpeed;
  67. // spidebug[3] = FOC__THETA;
  68. #endif
  69. #if (VoltageCompensationEn == 1)
  70. {
  71. if (VoltageComp.cpscnt > VoltageCompensationDelayCnt)
  72. {
  73. SetBit(ADC_CR, ADCBSY);
  74. while (ReadBit(ADC_CR, ADCBSY));
  75. // mcFocCtrl.mcDcbusFlt = ADC2_DR;
  76. }
  77. VoltageCompensation(mcFocCtrl.CtrlMode,mcFocCtrl.mcDcbusFlt);
  78. }
  79. #endif
  80. DRV_SR = (DRV_SR | SYSTIF) & (~DCIF);
  81. }
  82. }
  83. /**
  84. @brief Timer2中断服务函数
  85. @note 本例程中用于RSD方式顺逆风检测
  86. @date 2022-07-14
  87. */
  88. #if (TAILWIND_MODE == RSDMethod)
  89. void TIM2_INT(void) interrupt 4
  90. {
  91. if (ReadBit(TIM2_CR1, T2IP))
  92. {
  93. RsdProcess();
  94. ClrBit(TIM2_CR1, T2IP);
  95. }
  96. if (ReadBit(TIM2_CR1, T2IF)) // 溢出中断,用于判断静止,时间为349ms。
  97. {
  98. mcRsd.State = STATIC;
  99. mcRsd.Period = 65535;
  100. mcRsd.Speed = 0;
  101. mcRsd.SpeedUpdate = 1;
  102. ClrBit(TIM2_CR1, T2IF);
  103. }
  104. if (ReadBit(TIM2_CR1, T2IR))
  105. {
  106. ClrBit(TIM2_CR1, T2IR);
  107. }
  108. }
  109. #endif
  110. /**
  111. @brief Timer1中断服务函数
  112. @note 本例程中用于BEMF方式顺逆风检测
  113. @date 2022-07-14
  114. */
  115. #if (TAILWIND_MODE == BEMFMethod)
  116. void CMP012_INT(void) interrupt 7
  117. {
  118. if (CMP_SR & 0x70)
  119. {
  120. BemfProcess();
  121. CMP_SR = CMP_SR & 0x8F;
  122. }
  123. }
  124. void TIM2_INT(void) interrupt 4
  125. {
  126. if (ReadBit(TIM2_CR1, T2IP))
  127. {
  128. ClrBit(TIM2_CR1, T2IP);
  129. }
  130. if (ReadBit(TIM2_CR1, T2IF)) // 溢出中断,用于判断静止,时间为349ms。
  131. {
  132. mcBemf.PeriodTime = 65535;
  133. mcBemf.BEMFSpeed = 0;
  134. mcBemf.SpeedUpdate = 1;
  135. ClrBit(TIM2_CR1, T2IF);
  136. }
  137. if (ReadBit(TIM2_CR1, T2IR))
  138. {
  139. ClrBit(TIM2_CR1, T2IR);
  140. }
  141. }
  142. #endif
  143. void TIM2_INT(void) interrupt 4
  144. {
  145. if (ReadBit(TIM2_CR1, T2IP))
  146. {
  147. ClrBit(TIM2_CR1, T2IP);
  148. }
  149. if (ReadBit(TIM2_CR1, T2IF)) // 溢出中断,用于判断静止,时间为349ms。
  150. {
  151. ClrBit(TIM2_CR1, T2IF);
  152. }
  153. if (ReadBit(TIM2_CR1, T2IR))
  154. {
  155. ClrBit(TIM2_CR1, T2IR);
  156. }
  157. }
  158. /**
  159. @brief 定时器3中断服务函数
  160. @note 本例程中用于PWM调速信号捕获
  161. @date 2022-07-14
  162. */
  163. void TIM3_INT(void) interrupt 9
  164. {
  165. static uint16 ScrOnTime = 0; // 发热开启时间
  166. if (ReadBit(TIM3_CR1, T3IR))
  167. {
  168. ClrBit(TIM3_CR1, T3IR);
  169. }
  170. if (ReadBit(TIM3_CR1, T3IP)) // 周期中断
  171. {
  172. ClrBit(TIM3_CR1, T3IP);
  173. }
  174. if (ReadBit(TIM3_CR1, T3IF))
  175. {
  176. ClrBit(TIM3_CR1, T3IF);
  177. }
  178. }
  179. /**
  180. @brief 滴答定时器,默认用于产生1ms定时间隔
  181. @date 2022-07-14
  182. */
  183. void SYStick_INT(void) interrupt 10
  184. {
  185. if (ReadBit(DRV_SR, SYSTIF)) // SYS TICK中断
  186. {
  187. g_1mTick = 1;
  188. DRV_SR = (DRV_SR | DCIF) & (~SYSTIF);
  189. }
  190. }
  191. /**
  192. @brief 比较器硬件过流保护,该中断仅提供 故障码 赋值,用于状态机的切换。
  193. 需要开启比较器CMP3 发生过流 自动清除MOE功能
  194. @date 2022-07-14
  195. */
  196. void CMP3_INT(void) interrupt 12
  197. {
  198. if (ReadBit(CMP_SR, CMP3IF))
  199. {
  200. if (mcState != mcPosiCheck)
  201. {
  202. mcFaultSource = FaultHardOVCurrent; // 硬件过流保护
  203. }
  204. ClrBit(CMP_SR, CMP3IF);
  205. }
  206. }