GPIO.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. @copyright None
  3. @file GPIO.c
  4. @author Comment Vivre
  5. @date 2025-11-03
  6. @brief None
  7. */
  8. #include <Myproject.h>
  9. /**
  10. @function GPIO_Init
  11. @brief IO初始化
  12. @date 2025-11-01
  13. */
  14. void GPIO_Init(void)
  15. {
  16. // LED上拉 低电平点亮
  17. SetBit(P0_OE, P03);
  18. SetBit(P0_PU, P03);
  19. GP03 = 1;
  20. // 主电源继电器驱动 高使能
  21. SetBit(P5_OE, P51);
  22. SetBit(P5_PU, P51);
  23. GP51 = 0;
  24. // 预驱复位控制 低复位
  25. SetBit(P0_OE, P04);
  26. SetBit(P0_PU, P04);
  27. GP04 = 0;
  28. }
  29. /**
  30. @function PreDriver_Falut_Init
  31. @brief 预驱故障中断
  32. @date 2025-10-31
  33. */
  34. void PreDriver_Falut_Init(void)
  35. {
  36. // 关闭输出 配置上拉
  37. ClrBit(P0_OE, PIN2);
  38. SetBit(P0_PU, PIN2);
  39. // 中断源配置为P02
  40. SetReg(LVSR, EXT0CFG0 | EXT0CFG1 | EXT0CFG2, EXT0CFG1);
  41. // 清除中断标志位
  42. IF0 = 0;
  43. // 配置为下降沿触发
  44. IT01 = 0;
  45. IT00 = 1;
  46. // 中断触发时自动关闭MOE
  47. SetBit(EVT_FILT, INT0_MOE_EN);
  48. // 不滤波
  49. ClrBit(EVT_FILT, EFDIV0 | EFDIV1);
  50. // 配置中断优先级为最高
  51. SetBit(IP0, PX00 | PX01);
  52. // 外部中断0使能
  53. EX0 = 1;
  54. }
  55. LED_Control_t ledControl = { LED_STATE_OFF, 0, 0 };
  56. /**
  57. @function LED_State_Display
  58. @brief 指示运行与故障代码
  59. @param[in] Xn0: [输入/出] 故障代码
  60. @date 2025-05-30
  61. */
  62. void LED_State_Display(uint8_t Xn0)
  63. {
  64. switch (ledControl.LedState)
  65. {
  66. // 关闭状态
  67. case LED_STATE_OFF:
  68. // 存在故障 切入故障状态 清零计数
  69. if (Xn0)
  70. {
  71. ledControl.LedState = LED_STATE_BLINK_FAULT;
  72. ledControl.PauseDelayCnt = 0;
  73. ledControl.BlinkCnt = 0;
  74. }
  75. // 开启状态且无故障 切入开启状态并打开LED
  76. else if (isCtrlPowOn)
  77. {
  78. ledControl.LedState = LED_STATE_ON;
  79. LED_ON;
  80. }
  81. break;
  82. // 开启状态
  83. case LED_STATE_ON:
  84. // 故障或停止时 关闭LED并切回关闭状态
  85. if (!isCtrlPowOn || Xn0)
  86. {
  87. ledControl.LedState = LED_STATE_OFF;
  88. LED_OFF;
  89. }
  90. break;
  91. // 故障指示状态
  92. case LED_STATE_BLINK_FAULT:
  93. ledControl.PauseDelayCnt ++;
  94. // 延时等待
  95. if (ledControl.PauseDelayCnt >= 500)
  96. {
  97. ledControl.PauseDelayCnt = 0;
  98. LED_TOGGLE;
  99. // 灭灯时计数
  100. if (LED_OFF_STATE)
  101. {
  102. ledControl.BlinkCnt ++;
  103. // 完成一个周期的闪烁 切换到延时等待模式
  104. if (ledControl.BlinkCnt >= Xn0)
  105. { ledControl.LedState = LED_STATE_POST_FAULT_DELAY; }
  106. }
  107. }
  108. // 故障清除后 立刻回到熄灭状态
  109. if (!Xn0)
  110. {
  111. LED_OFF;
  112. ledControl.LedState = LED_STATE_OFF;
  113. }
  114. break;
  115. // 延迟等待
  116. case LED_STATE_POST_FAULT_DELAY:
  117. ledControl.PauseDelayCnt ++;
  118. // 延时满足或故障清除回到关闭状态
  119. if ((ledControl.PauseDelayCnt >= 1000) || (!Xn0))
  120. { ledControl.LedState = LED_STATE_OFF; }
  121. break;
  122. }
  123. }
  124. volatile Power_Control_t powerControl = {DELAY_POWER_ON, 0};
  125. /**
  126. @function Power_In_Control
  127. @brief 上电控制与预驱复位控制
  128. @date 2025-11-01
  129. */
  130. void Power_In_Control(void)
  131. {
  132. switch (powerControl.PowerSate)
  133. {
  134. case DELAY_POWER_ON:
  135. {
  136. powerControl.PowerInCnt ++;
  137. if (powerControl.PowerInCnt > 1000)
  138. {
  139. // 继电器上电
  140. MAIN_RELAY = 1;
  141. powerControl.PowerInCnt = 0;
  142. powerControl.PowerSate = DELAY_FREE_RST;
  143. }
  144. break;
  145. }
  146. case DELAY_FREE_RST:
  147. {
  148. powerControl.PowerInCnt ++;
  149. if (powerControl.PowerInCnt > 500)
  150. {
  151. powerControl.PowerInCnt = 0;
  152. powerControl.PowerSate = POWER_RUN;
  153. }
  154. break;
  155. }
  156. case POWER_RUN:
  157. {
  158. #if 1
  159. if (mcFaultSource == FaultNoSource)
  160. { PRE_DRIVER_RST = 1; }
  161. #endif
  162. }
  163. }
  164. }
  165. /*Value--定时时间,单位ms,最小定时时间8ms,最大定时1800ms*/
  166. void WatchDogConfig(uint32 value, uint8 Status)
  167. {
  168. SetReg(CCFG1, WDT_EN, (Status ? WDT_EN : 0x00));
  169. WDT_ARR = ((uint16)(65532 - value * 32768 / 1000) >> 8);
  170. ClrBit(WDT_CR, WDTF);
  171. SetBit(WDT_CR, WDTRF);
  172. }