fw_adc.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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_ADC_H___
  15. #define ___FW_ADC_H___
  16. #include "fw_conf.h"
  17. #include "fw_types.h"
  18. #define ADC_SetPowerState(__STATE__) SFR_ASSIGN(ADC_CONTR, 7, __STATE__)
  19. #define ADC_Start() SFR_SET(ADC_CONTR, 6)
  20. #define ADC_SamplingFinished() (ADC_CONTR & (0x01 << 5))
  21. #define ADC_ClearInterrupt() SFR_RESET(ADC_CONTR, 5)
  22. #define ADC_SetPWMTriggerState(__STATE__) SFR_ASSIGN(ADC_CONTR, 4, __STATE__)
  23. /**
  24. * ADC input channels selection
  25. * Set PxM0/PxM1 to high-impedance input for selected I/O port, and set PxIE
  26. * to shutdown digital input if you want to enable ADC in powerdown mode
  27. *
  28. * STC8H8K64U
  29. * STC8H3K64S4 STC8H2K64T STC8H4K64TLCD STC8G1K08 STC8G2K64S4
  30. * STC8H1K28: STC8H1K08 STC8H3K64S2 STC8H4K64TLR STC8H4K64LCD STC8G1K08A STC8G1K08T STC8G2K64S2
  31. * 0000 P1.0/ADC0 P1.0/ADC0 P1.0/ADC0 P1.0/ADC0 P1.0/ADC0 P3.0/ADC0 P1.0/ADC0 P1.0/ADC0
  32. * 0001 P1.1/ADC1 P1.1/ADC1 P1.1/ADC1 P1.1/ADC1 P1.1/ADC1 P3.1/ADC1 P1.1/ADC1 P1.1/ADC1
  33. * 0010 P1.2/ADC2 N/A P1.2/ADC2 P5.4/ADC2 P5.4/ADC2 P3.2/ADC2 P1.2/ADC2 P1.2/ADC2
  34. * 0011 P1.3/ADC3 N/A N/A P1.3/ADC3 P1.3/ADC3 P3.3/ADC3 P1.3/ADC3 P1.3/ADC3
  35. * 0100 P1.4/ADC4 N/A N/A P1.4/ADC4 P1.4/ADC4 P5.4/ADC4 P1.4/ADC4 P1.4/ADC4
  36. * 0101 P1.5/ADC5 N/A N/A P1.5/ADC5 P1.5/ADC5 P5.5/ADC5 P1.5/ADC5 P1.5/ADC5
  37. * 0110 P1.6/ADC6 N/A P1.6/ADC6 P1.6/ADC6 P6.2/ADC6 N/A P1.6/ADC6 P1.6/ADC6
  38. * 0111 P1.7/ADC7 N/A P1.7/ADC7 P1.7/ADC7 P6.3/ADC7 N/A P1.7/ADC7 P1.7/ADC7
  39. * 1000 P0.0/ADC8 P3.0/ADC8 P0.0/ADC8 P0.0/ADC8 P0.0/ADC8 N/A P3.0/ADC8 P0.0/ADC8
  40. * 1001 P0.1/ADC9 P3.1/ADC9 P0.1/ADC9 P0.1/ADC9 P0.1/ADC9 N/A P3.1/ADC9 P0.1/ADC9
  41. * 1010 P0.2/ADC10 P3.2/ADC10 P0.2/ADC10 P0.2/ADC10 P0.2/ADC10 N/A P3.2/ADC10 P0.2/ADC10
  42. * 1011 P0.3/ADC11 P3.3/ADC11 P0.3/ADC11 P0.3/ADC11 P0.3/ADC11 N/A P3.3/ADC11 P0.3/ADC11
  43. * 1100 N/A P3.4/ADC12 P0.4/ADC12 P0.4/ADC12 P0.4/ADC12 N/A P3.4/ADC12 P0.4/ADC12
  44. * 1101 N/A P3.5/ADC13 P0.5/ADC13 P0.5/ADC13 P0.5/ADC13 N/A P3.5/ADC13 P0.5/ADC13
  45. * 1110 N/A P3.6/ADC14 P0.6/ADC14 P0.6/ADC14 P0.6/ADC14 N/A P3.6/ADC14 P0.6/ADC14
  46. * 1111 Internal 1.19V voltage reference
  47. */
  48. #define ADC_SetChannel(__CHANNEL__) (ADC_CONTR = ADC_CONTR & ~0x0F | ((__CHANNEL__ & 0x0F) << 0))
  49. /**
  50. * 10-bit in [ADC_RES,ADC_RESL]: STC8H1K28,STC8H1K08
  51. * 12-bit in [ADC_RES,ADC_RESL]: STC8H3K64S4,STC8H3K64S2,STC8H8K64U,STC8H2K64T,STC8H4K64TLR,STC8H4K64TLCD,STC8H4K64LCD
  52. */
  53. #define ADC_SetResultAlignmentLeft() SFR_RESET(ADCCFG, 5)
  54. #define ADC_SetResultAlignmentRight() SFR_SET(ADCCFG, 5)
  55. /**
  56. * Time of one complete ADC conversion:
  57. * 10-bit ADC: (Switch + 1) + (Hold + 1) + (Sample + 1) + 10
  58. * 12-bit ADC: (Switch + 1) + (Hold + 1) + (Sample + 1) + 12
  59. *
  60. * ADC conversion frequency:
  61. * 10-bit ADC Frequency = SYSCLK / 2 / (__PRESCALER__ + 1) / [(Switch + 1) + (Hold + 1) + (Sample + 1) + 10]
  62. * 12-bit ADC Frequency = SYSCLK / 2 / (__PRESCALER__ + 1) / [(Switch + 1) + (Hold + 1) + (Sample + 1) + 12]
  63. *
  64. */
  65. /**
  66. * ADC clock = SYSclk/2/(__PRESCALER__+1)
  67. */
  68. #define ADC_SetClockPrescaler(__PRESCALER__) (ADCCFG = ADCCFG & ~0x0F | ((__PRESCALER__) << 0))
  69. /**
  70. * Channel switch time,
  71. * 0: 1 ADC clock;
  72. * 1: 2 ADC clocks
  73. */
  74. #define ADC_SetChannelSwitchTime(__CLKS_1OR2__) SFR_ASSIGN(ADCTIM, 7, __CLKS_1OR2__)
  75. /**
  76. * Channel hold time,
  77. * 0x00: 1 ADC clock;
  78. * 0x01: 2 ADC clocks
  79. * 0x02: 3 ADC clocks
  80. * 0x03: 4 ADC clocks
  81. */
  82. #define ADC_SetChannelHoldTime(__CLKS_1TO4__) (ADCTIM = ADCTIM & ~(0x03 << 5) | ((__CLKS_1TO4__) << 5))
  83. /**
  84. * Sample time
  85. * value should be equal or larger than 10
  86. * 0x00: 1 ADC clock;
  87. * 0x01: 2 ADC clocks
  88. * ...
  89. * 0x1F: 32 ADC clocks
  90. */
  91. #define ADC_SetSampleTime(__CLKS_1TO32__) (ADCTIM = ADCTIM & ~(0x1F << 0) | ((__CLKS_1TO32__) << 0))
  92. /**
  93. * Start ADC conversion, and return 8-bit result
  94. */
  95. uint8_t ADC_Convert(void);
  96. /**
  97. * Start ADC conversion, and return 16-bit high precision result
  98. */
  99. uint16_t ADC_ConvertHP(void);
  100. #endif