Interrupt.c 4.7 KB

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