CMP.c 8.4 KB

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