CMP.c 8.6 KB

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