CMP.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* --------------------------- (C) COPYRIGHT 2021 Fortiortech ShenZhen -----------------------------
  2. File Name : CMP.c
  3. Author : Fortiortech Appliction Team
  4. Version : V1.0
  5. Date : 2021-04-11
  6. Description : This file contains .C file function used for Motor Control.
  7. ----------------------------------------------------------------------------------------------------
  8. All Rights Reserved
  9. ------------------------------------------------------------------------------------------------- */
  10. #include <MyProject.h>
  11. //#include <definition.h>
  12. //#include <Customer.h>
  13. //#include <Parameter.h>
  14. //#include <Protect.h>
  15. //#include <CMP.h>
  16. void CMP0_Init(void)
  17. {
  18. }
  19. /*****************************************************************************
  20. * Function : CMP3_Init
  21. * Description : CMP3_INIT
  22. * Input : void
  23. * Output : None
  24. * Return :
  25. * Others :
  26. * Record
  27. * 1.Date : 20190320
  28. * Author : Bruce HW&RD
  29. * Modification: Created function
  30. *****************************************************************************/
  31. void CMP3_Init(void)
  32. {
  33. /******CMP3 端口模拟功能设置*******/
  34. #if (Shunt_Resistor_Mode == Single_Resistor)
  35. {
  36. SetBit(P2_AN , P27); //CMP3 Pin设置为模拟模式 +
  37. ClrBit(CMP_CR1 , CMP3MOD1); //00-->P27-单比较器模式 01-->P20/P23-双比较器模式
  38. ClrBit(CMP_CR1 , CMP3MOD0); //1X-->P20/P23/P27-三比较器模式
  39. }
  40. #elif (Shunt_Resistor_Mode == Double_Resistor)
  41. {
  42. SetBit(P2_AN , P27); //CMP3 Pin设置为模拟模式 +
  43. ClrBit(CMP_CR1 , CMP3MOD1); //00-->P27-单比较器模式 01-->P20/P23-双比较器模式
  44. ClrBit(CMP_CR1 , CMP3MOD0); //1X-->P20/P23/P27-三比较器模式
  45. }
  46. #elif (Shunt_Resistor_Mode == Three_Resistor)
  47. {
  48. SetBit(P2_AN , P27 | P23 | P20);// CMP3 Pin设置为模拟模式 +
  49. SetBit(CMP_CR1 , CMP3MOD1); // 00-->P27-单比较器模式 01-->P20/P23-双比较器模式
  50. SetBit(CMP_CR1 , CMP3MOD0); // 1X-->P20/P23/P27-三比较器模式
  51. }
  52. #endif //end Shunt_Resistor_Mode
  53. #if (COMPARE_MODE == COMPARE_HW_MODE)
  54. {
  55. /**P2.6使能其模拟功能,使能数字输出**/
  56. SetBit(P2_AN , P26);
  57. ClrBit(P2_OE , P26);
  58. ClrBit(DAC_CR , DAC0_1EN);
  59. }
  60. #else
  61. {
  62. // P2.6使能其模拟功能,使能数字输出
  63. // SetBit(P2_AN , P26);
  64. // SetBit(P2_OE , P26);
  65. // DAC0 Enable
  66. SetBit(DAC_CR , DAC0_1EN);
  67. /******************************
  68. 0: 正常模式,DAC输出电压范围为0到VREF
  69. 1: 半电压转换模式,DAC输出电压范围为VHALF到VREF
  70. ****************************/
  71. ClrBit(DAC_CR , DACMOD);
  72. /**********设置DAC过流值,舍弃低位精度*****************/
  73. // DAC0_DR 是DAC0的高8位,DAC0 的最低位忽略
  74. DAC0_DR = DAC_OverCurrentValue;
  75. }
  76. #endif //end Compare_Mode
  77. SetBit(CMP_CR1 , CMP3HYS); // CMP3 Hysteresis voltage Enable
  78. /*---------------------------------------------------------------------------------
  79. 选择母线电流保护触发信号源,外部中断0或者比较器3中断。
  80. 0-CMP3,1-INT0
  81. ---------------------------------------------------------------------------------*/
  82. ClrBit(EVT_FILT, INT0_MOE_EN);
  83. /*---------------------------------------------------------------------------------
  84. 触发硬件保护后硬件关闭驱动输出MOE配置, 常规应用需要使能MOE自动清零
  85. 00--MOE不自动清零
  86. 01--MOE自动清零
  87. ----------------------------------------------------------------------------------*/
  88. ClrBit(EVT_FILT , MOEMD1);
  89. SetBit(EVT_FILT , MOEMD0);
  90. /*----------------------------------------------------------------------------------
  91. 母线电流保护时间滤波宽度
  92. 00-不滤波
  93. 01-4cpu clock
  94. 10-8cpu clock
  95. 11-16cpu clock
  96. -----------------------------------------------------------------------------------*/
  97. SetBit(EVT_FILT , EFDIV1);
  98. SetBit(EVT_FILT , EFDIV0);
  99. SetBit(CMP_CR1 , CMP3EN); //CMP3 Enable
  100. }
  101. /*****************************************************************************
  102. * Function : CMP3_Interrupt_Init
  103. * Description : CMP3中断配置
  104. * Input : void
  105. * Output : None
  106. * Return :
  107. * Others :
  108. * Record
  109. * 1.Date : 20190320
  110. * Author : Bruce HW&RD
  111. * Modification: Created function
  112. *****************************************************************************/
  113. void CMP3_Interrupt_Init(void)
  114. {
  115. ClrBit(CMP_SR , CMP3IF);
  116. /*------------------------------------------------------------------------
  117. 比较器中断模式配置
  118. 00: 不产生中断
  119. 01: 上升沿产生中断
  120. 10: 下降沿产生中断
  121. 11: 上升/下降沿产生中断
  122. ------------------------------------------------------------------------*/
  123. ClrBit(CMP_CR0 , CMP3IM1);
  124. SetBit(CMP_CR0 , CMP3IM0);
  125. SetBit(IP3 , PCMP31); // 中断优先级别3
  126. SetBit(IP3 , PCMP30);
  127. }