CMP.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. @copyright None
  3. @file CMP.c
  4. @author Comment Vivre
  5. @date 2025-11-03
  6. @brief None
  7. */
  8. #include <Myproject.h>
  9. void CMP0_Init(void)
  10. {
  11. /******CMP0/1/2 端口模拟功能设置*******/
  12. SetBit(P1_AN, PIN4); //CMP0 Pin设置为模拟模式 +
  13. SetBit(P1_AN, PIN6); //CMP1 Pin设置为模拟模式 +
  14. SetBit(P2_AN, PIN1); //CMP2 Pin设置为模拟模式 +
  15. ClrBit(P1_PU, PIN4); //P14上拉关闭
  16. ClrBit(CMP_CR0, CMP2IM1); //CMP2中断模式
  17. ClrBit(CMP_CR0, CMP2IM0); //00-->No Interrupt 01-->Rising 10-->Falling 11-->Rising/Falling
  18. ClrBit(CMP_CR0, CMP1IM1); //CMP1中断模式
  19. ClrBit(CMP_CR0, CMP1IM0); //00-->No Interrupt 01-->Rising 10-->Falling 11-->Rising/Falling
  20. ClrBit(CMP_CR0, CMP0IM1); //CMP0中断模式
  21. ClrBit(CMP_CR0, CMP0IM0); //00-->No Interrupt 01-->Rising 10-->Falling 11-->Rising/Falling
  22. SetBit(CMP_CR1, CMP0HYS2); //CMP0迟滞电压配置
  23. ClrBit(CMP_CR1, CMP0HYS1); //111-->±12mV 110-->+12mV 101-->-12mV 100-->±6mV
  24. ClrBit(CMP_CR1, CMP0HYS0); //011-->+6mV 010-->-6mV 001-->±3mV 000-->No HYS
  25. ClrBit(CMP_CR2, CMP0MOD1); //00-->无内置电阻3比较器 01-->有内置电阻3比较器模式
  26. SetBit(CMP_CR2, CMP0MOD0); //10-->3差分比较器 11-->2比较器
  27. ClrBit(CMP_CR2, CMP0SEL1); //00-->3比较器轮询 01-->P14+固定选择CMP0OUT
  28. ClrBit(CMP_CR2, CMP0SEL0); //10-->P16+固定选择CMP1OUT 11-->P21+固定选择CMP2OUT
  29. ClrBit(CMP_CR2, CMP0CSEL1); //00-->正常轮询(0.66us) 01-->快速轮询(0.33us)
  30. ClrBit(CMP_CR2, CMP0CSEL0); //10-->低速轮询(2.67us) 11-->偏低速轮询(1.33us)
  31. ClrBit(CMP_CR3, CMPSEL2); //011-->P07作为CMP2输出
  32. SetBit(CMP_CR3, CMPSEL1); //010-->P07作为CMP1输出
  33. SetBit(CMP_CR3, CMPSEL0); //001-->P07作为CMP0输出
  34. ClrBit(CMP_CR4, CMP0_FS); //CMP1/2功能转移 仅CMP0_MOD=01时有效
  35. SetBit(CMP_CR2, CMP0EN); //CMP0 Enable
  36. /****************************************************************/
  37. ClrBit(IP1, PCMP1);
  38. ClrBit(IP1, PCMP0);
  39. /***********************比较器采样设置***************************/
  40. CMP_SAMR = 0x42; //延迟开启采样时间[7:4]&关闭采样时间[3:0]
  41. //延迟时间= CSOND x 41.67 x 8ns CSOND 必须>= CSOFFD
  42. SetBit(CMP_CR3, SAMSEL1); //CMP0/1/2 & ADC 采样时机配置
  43. ClrBit(CMP_CR3, SAMSEL0); //00-->0N&0FF采样,无延迟 01-->0FF采样,延迟CMP_SAMR
  44. //10-->0N采样,延迟CMP_SAMR 11-->0N&0FF采样,延迟CMP_SAMR
  45. ClrBit(CMP_CR3, CMPDTEN); //比较器死区采样使能 0-->Disable 1-->Enable
  46. ClrBit(CMP_CR3, DBGSEL1); //DEBUG信号选择-->输出至P01
  47. SetBit(CMP_CR3, DBGSEL0); //00-->Disable 01-->方波屏蔽续流结束和检测到过零点信号
  48. //10-->ADC trigger信号 11-->比较器采样区间
  49. /****************************************************************/
  50. }
  51. /*****************************************************************************
  52. Function : CMP3_Init
  53. Description : CMP3_INIT
  54. Input : void
  55. Output : None
  56. Return :
  57. Others :
  58. Record
  59. 1.Date : 20190320
  60. Author : Bruce HW&RD
  61. Modification: Created function
  62. *****************************************************************************/
  63. void CMP3_Init(void)
  64. {
  65. /******CMP3 端口模拟功能设置*******/
  66. #if (Shunt_Resistor_Mode == Single_Resistor)
  67. {
  68. SetBit(P2_AN, P27); //CMP3 Pin设置为模拟模式 +
  69. ClrBit(CMP_CR1, CMP3MOD1); //00-->P27-单比较器模式 01-->P20/P23-双比较器模式
  70. ClrBit(CMP_CR1, CMP3MOD0); //1X-->P20/P23/P27-三比较器模式
  71. }
  72. #elif (Shunt_Resistor_Mode == Double_Resistor)
  73. {
  74. SetBit(P2_AN, P27); //CMP3 Pin设置为模拟模式 +
  75. ClrBit(CMP_CR1, CMP3MOD1); //00-->P27-单比较器模式 01-->P20/P23-双比较器模式
  76. ClrBit(CMP_CR1, CMP3MOD0); //1X-->P20/P23/P27-三比较器模式
  77. }
  78. #elif (Shunt_Resistor_Mode == Three_Resistor)
  79. {
  80. SetBit(P2_AN, P27 | P23 | P20); //CMP3 Pin设置为模拟模式 +
  81. SetBit(CMP_CR1, CMP3MOD1); //00-->P27-单比较器模式 01-->P20/P23-双比较器模式
  82. ClrBit(CMP_CR1, CMP3MOD0); //1X-->P20/P23/P27-三比较器模式
  83. }
  84. #endif //end Shunt_Resistor_Mode
  85. #if (Compare_Mode == Compare_Hardware)
  86. {
  87. /**P2.6使能其模拟功能,使能数字输出**/
  88. SetBit(P2_AN, P26);
  89. ClrBit(P2_OE, P26);
  90. ClrBit(DAC_CR, DAC0_1EN);
  91. }
  92. #else
  93. {
  94. /**P2.6使能其模拟功能,使能数字输出**/
  95. ClrBit(P2_AN, P26);
  96. ClrBit(P2_OE, P26);
  97. /******************************
  98. 0: 正常模式,DAC输出电压范围为0到VREF
  99. 1: 半电压转换模式,DAC输出电压范围为VHALF到VREF
  100. ****************************/
  101. ClrBit(DAC_CR, DACMOD);
  102. /**********设置DAC过流值*****************/
  103. if (DAC_OvercurrentValue < 255)
  104. {
  105. DAC0_DR = (uint8)DAC_OvercurrentValue;
  106. }
  107. else
  108. {
  109. DAC0_DR = 255;
  110. }
  111. /**********DAC0 Enable******************/
  112. SetBit(DAC_CR, DAC0_1EN);
  113. }
  114. #endif //end Compare_Mode
  115. SetBit(CMP_CR1, CMP3HYS); // CMP3 Hysteresis voltage Disable
  116. /* ---------------------------------------------------------------------------------
  117. 触发硬件保护后硬件关闭驱动输出MOE配置
  118. 00--MOE不自动清零
  119. 01--MOE自动清零
  120. ----------------------------------------------------------------------------------*/
  121. ClrBit(EVT_FILT, MOEMD1);
  122. SetBit(EVT_FILT, MOEMD0);
  123. /* ----------------------------------------------------------------------------------
  124. 母线电流保护时间滤波宽度
  125. 00-不滤波
  126. 01-6cpu clock
  127. 10-12cpu clock
  128. 11-24cpu clock
  129. -----------------------------------------------------------------------------------*/
  130. SetBit(EVT_FILT, EFDIV1);
  131. SetBit(EVT_FILT, EFDIV0);
  132. SetBit(CMP_CR1, CMP3EN); //CMP3 Enable
  133. }
  134. /*****************************************************************************
  135. Function : CMP3_Interrupt_Init
  136. Description : CMP3中断配置
  137. Input : void
  138. Output : None
  139. Return :
  140. Others :
  141. Record
  142. 1.Date : 20190320
  143. Author : Bruce HW&RD
  144. Modification: Created function
  145. *****************************************************************************/
  146. void CMP3_Interrupt_Init(void)
  147. {
  148. ClrBit(CMP_SR, CMP3IF);
  149. /* ------------------------------------------------------------------------
  150. 比较器中断模式配置
  151. 00: 不产生中断
  152. 01: 上升沿产生中断
  153. 10: 下降沿产生中断
  154. 11: 上升/下降沿产生中断
  155. ------------------------------------------------------------------------*/
  156. ClrBit(CMP_CR0, CMP3IM1);
  157. SetBit(CMP_CR0, CMP3IM0);
  158. SetBit(IP3, PCMP31);
  159. SetBit(IP3, PCMP30); // 中断优先级别3
  160. }
  161. void CMP5_Init(void)
  162. {
  163. SetBit(P1_AN, P13); // CMP3
  164. /* -------------------------------------------------------------------------------------------------
  165. 电流保护时间滤波宽度
  166. 00-不滤波 01-4cpu clock 10-8cpu clock 11-16cpu clock
  167. -------------------------------------------------------------------------------------------------*/
  168. SetReg(PFC_CR0, CMP5DIV1 | CMP5DIV0, CMP5DIV0); // 限流滤波选择10 330US
  169. /**********使能DAC******************/
  170. SetBit(DAC_CR, DAC2EN );
  171. /**********设置DAC过流值*****************/
  172. DAC2_DR = 0;
  173. /*比较器使能-------------------------------------------------------------------------------------*/
  174. SetBit(CMP_CR4, CMP5EN);
  175. }