CMP.c 8.0 KB

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