fw_cmp.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2021 IOsetting <iosetting(at)outlook.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ___FW_CMP_H___
  15. #define ___FW_CMP_H___
  16. #include "fw_conf.h"
  17. #include "fw_types.h"
  18. /**
  19. * STC8 MCU analog comparator
  20. *
  21. * 2P: 2 positive input options
  22. * 4P: 4 positive input options
  23. *
  24. * | | 2P+2N | 4P+2N |
  25. * | STC8G1K08 | Y | |
  26. * | STC8G1K08-8Pin | N | |
  27. * | STC8G1K08A | N | |
  28. * | STC8G2K64S4 | Y | |
  29. * | STC8G2K64S2 | Y | |
  30. * | STC8G1K08T | Y | |
  31. * | STC15H2K64S4 | Y | |
  32. * | STC8H1K08 | Y | |
  33. * | STC8H1K28 | Y | |
  34. * | STC8H3K64S4 | Y | |
  35. * | STC8H3K64S2 | Y | |
  36. * | STC8H2K64T | Y | |
  37. * | STC8H8K64U | | Y |
  38. * | STC8H4K64TLR | | Y |
  39. * | STC8H4K64TLCD | | Y |
  40. * | STC8H4K64LCD | | Y |
  41. */
  42. typedef enum
  43. {
  44. CMP_OLD_PositiveInput_P37 = 0x00,
  45. CMP_OLD_PositiveInput_ADC = 0x01,
  46. } CMP_OLD_PositiveInput_t;
  47. typedef enum
  48. {
  49. CMP_OLD_NegativeInput_RefV = 0x00,
  50. CMP_OLD_NegativeInput_P36 = 0x01,
  51. } CMP_OLD_NegativeInput_t;
  52. typedef enum
  53. {
  54. CMP_PositiveInput_P37 = 0x00,
  55. CMP_PositiveInput_P50 = 0x01,
  56. CMP_PositiveInput_P51 = 0x02,
  57. CMP_PositiveInput_ADC = 0x03,
  58. } CMP_PositiveInput_t;
  59. typedef enum
  60. {
  61. CMP_NegativeInput_P36 = 0x00,
  62. CMP_NegativeInput_RefV = 0x01,
  63. } CMP_NegativeInput_t;
  64. typedef enum
  65. {
  66. CMP_Hysteresis_0mV = 0x00,
  67. CMP_Hysteresis_10mV = 0x01,
  68. CMP_Hysteresis_20mV = 0x02,
  69. CMP_Hysteresis_30mV = 0x03,
  70. } CMP_Hysteresis_t;
  71. #define CMP_SetEnabled(__STATE__) SFR_ASSIGN(CMPCR1, 7, __STATE__)
  72. #define CMP_ClearInterrupt() SFR_ASSIGN(CMPCR1, 6, 0)
  73. /**
  74. * for 2P+2N series.
  75. * Set positive input pin, 0:P36, 1:ADC
  76. */
  77. #define CMP_OLD_SetPositiveInput(__INPUT__) SFR_ASSIGN(CMPCR1, 3, __INPUT__)
  78. /**
  79. * for 4P+2N series.
  80. * Set positive input pin, 0:P36, 1:P50, 2:P51, 3:ADC
  81. */
  82. #define CMP_SetPositiveInput(__INPUT__) do{ \
  83. SFRX_ON(); \
  84. CMPEXCFG = CMPEXCFG & ~(0x03) | (__INPUT__ & 0x03); \
  85. SFRX_OFF(); \
  86. } while(0)
  87. /**
  88. * for 2P+2N series.
  89. * Set negative input pin, 0:1.19V ref, 1:P36
  90. */
  91. #define CMP_OLD_SetNegativeInput(__INPUT__) SFR_ASSIGN(CMPCR1, 2, __INPUT__)
  92. /**
  93. * for 4P+2N series.
  94. * Set negative input pin, 0:P36, 1:1.19Vref
  95. */
  96. #define CMP_SetNegativeInput(__INPUT__) SFRX_ASSIGN(CMPEXCFG, 2, __INPUT__)
  97. /**
  98. * Result output
  99. * 0:no output, 1:output to P34 or P41
  100. */
  101. #define CMP_SetResultOutputState(__STATE__) SFR_ASSIGN(CMPCR1, 1, __STATE__)
  102. #define CMP_ReadResultOutput() (CMPCR1 & 0x01)
  103. /**
  104. * 0: P34/P41 output high when result is 1
  105. * 1: P34/P41 output low when result is 1
  106. */
  107. #define CMP_SetResultOutputInvert(__STATE__) SFR_ASSIGN(CMPCR2, 7, __STATE__)
  108. /**
  109. * 0: Enable 0.1us analog filter
  110. * 1: Disable 0.1us analog filter
  111. */
  112. #define CMP_SetFilterDisabled(__STATE__) SFR_ASSIGN(CMPCR2, 6, __STATE__)
  113. /**
  114. * Comparator result will be delayed for (__CLOCKS__ + 2) clocks
  115. */
  116. #define CMP_SetDebouncingClocks(__CLOCKS__) CMPCR2 = CMPCR2 & ~(0x3F) | (__CLOCKS__ & 0x3F)
  117. /**
  118. * Set comparator hysteresis for producing stable switching behavior.
  119. */
  120. #define CMP_SetHysteresis(__HYST__) do{ \
  121. SFRX_ON(); \
  122. CMPEXCFG = CMPEXCFG & ~(0x03 << 6) | (__HYST__ << 6); \
  123. SFRX_OFF(); \
  124. } while(0)
  125. #endif