HW_ADC_ATY.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * @file HW_ADC_ATY.c
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2025 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief Familiar functions of uart for STC51
  20. *
  21. * @version
  22. * - 1_01_221231 > ATY
  23. * -# Preliminary version, first Release
  24. * -Undone: over with "\r\n" type
  25. ********************************************************************************
  26. */
  27. #ifndef __HW_ADC_ATY_C
  28. #define __HW_ADC_ATY_C
  29. #include "HW_ADC_ATY.h"
  30. /******************************* For user *************************************/
  31. #if defined (__STC51_ATY)
  32. /**
  33. * @brief ADC init
  34. */
  35. void ADC_Init(void)
  36. {
  37. #ifndef ADCTIM
  38. #define ADCTIM (*(unsigned char volatile xdata *)0xfea8)
  39. #endif
  40. P_SW2 |= 0x80; // change reg sfr
  41. ADCTIM = 0x3F; // set ADC internal timing sequence
  42. P_SW2 &= 0x7F; // stop change reg sfr
  43. // ADCCFG = 0x0F; // set SYSCLK/2/16 as ADC timer
  44. ADCCFG = 0x00; // set SYSCLK/2/1 as ADC timer
  45. ADC_CONTR = 0x80; // enable ADC
  46. }
  47. #elif defined(__STM32_HAL_ATY)
  48. #endif /* PLATFORM */
  49. /******************************************************************************/
  50. #if defined (__STC51_ATY)
  51. /**
  52. * @brief ADC get mcu vref
  53. * @return mcu vref data
  54. */
  55. float ADC_GetVref(void)
  56. {
  57. uint16_t dataVref = 0;
  58. float resultVref = 0.0;
  59. int* BGV;
  60. BGV = (int __IDATA*)0xEF;
  61. dataVref = ADC_Get(0x0F);
  62. resultVref = (1024L * (*BGV) / dataVref);
  63. return resultVref;
  64. }
  65. /**
  66. * @brief ADC get data at channel
  67. * @param channel channel
  68. * @return adc data
  69. */
  70. uint16_t ADC_Get(uint8_t channel)
  71. {
  72. uint16_t errCount = 1000;
  73. ADC_CONTR = ((ADC_CONTR & 0xF0) | (channel & 0x0F));
  74. ADC_CONTR |= 0x40; // start ADC
  75. __NOP_ATY;
  76. __NOP_ATY;
  77. while((!(ADC_CONTR & 0x20)) && errCount)
  78. errCount--;
  79. if(errCount)
  80. {
  81. ADC_CONTR &= ~0x20; // clear over flag
  82. return ((ADC_RES << 2) + (ADC_RESL >> 6));
  83. }
  84. return 0x01;
  85. }
  86. #elif defined(__STM32_HAL_ATY)
  87. #include "adc.h"
  88. // set channel to 0 for scand mode
  89. uint16_t ADC_Get(ADC_HandleTypeDef* adc, uint32_t channel)
  90. {
  91. #if defined (__STM32F4xx_ADC_H)
  92. #define SAMPETIME ADC_SAMPLETIME_480CYCLES
  93. #elif defined (__STM32F1xx_HAL_ADC_H)
  94. #define SAMPETIME ADC_SAMPLETIME_239CYCLES_5
  95. #endif
  96. if(channel != 0){
  97. ADC_ChannelConfTypeDef sConfig = {0};
  98. sConfig.Channel = channel;
  99. sConfig.Rank = 1U;
  100. sConfig.SamplingTime = SAMPETIME; // do not set faster than 28 at f103
  101. if(HAL_ADC_ConfigChannel(adc, &sConfig) != HAL_OK)
  102. {
  103. Error_Handler();
  104. }
  105. }
  106. HAL_ADC_Start(adc);
  107. HAL_ADC_PollForConversion(adc, 1000);
  108. if(HAL_IS_BIT_SET(HAL_ADC_GetState(adc), HAL_ADC_STATE_REG_EOC))
  109. return HAL_ADC_GetValue(adc);
  110. return 0.1;
  111. }
  112. #endif /* PLATFORM */
  113. #endif /* __HW_ADC_ATY_C */
  114. /******************************** End Of File *********************************/