HW_ADC_ATY.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 - 2023 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. P0M0 &= 0xF0; // ADC IO P00 P01 P02 P03
  41. P0M1 |= 0x0F; // ADC IO P00 P01 P02 P03
  42. P_SW2 |= 0x80; // change reg sfr
  43. ADCTIM = 0x3F; // set ADC internal timing sequence
  44. P_SW2 &= 0x7F; // stop change reg sfr
  45. ADCCFG = 0x0F; // set SYSCLK/2/16 as ADC timer
  46. // ADCCFG = 0x00; // set SYSCLK/2/1 as ADC timer
  47. ADC_CONTR = 0x80; // enable ADC
  48. }
  49. #elif defined(__STM32_HAL_ATY)
  50. #endif /* PLATFORM */
  51. /******************************************************************************/
  52. #if defined(__STC51_ATY)
  53. /**
  54. * @brief ADC get mcu vref
  55. * @return mcu vref data
  56. */
  57. float ADC_GetVref(void)
  58. {
  59. uint16_t dataVref = 0;
  60. float resultVref = 0.0;
  61. int* BGV;
  62. BGV = (int __IDATA*)0xEF;
  63. dataVref = ADC_Get(0x0F);
  64. resultVref = (1024L * (*BGV) / dataVref);
  65. return resultVref;
  66. }
  67. /**
  68. * @brief ADC get data at channel
  69. * @param channel channel
  70. * @return adc data
  71. */
  72. uint16_t ADC_Get(uint8_t channel)
  73. {
  74. uint16_t errCount = 1000;
  75. ADC_CONTR = ((ADC_CONTR & 0xF0) | (channel & 0x0F));
  76. ADC_CONTR |= 0x40; // start ADC
  77. __NOP_ATY;
  78. __NOP_ATY;
  79. while((!(ADC_CONTR & 0x20)) && errCount)
  80. errCount--;
  81. if(errCount)
  82. {
  83. ADC_CONTR &= ~0x20; // clear over flag
  84. return ((ADC_RES << 2) + (ADC_RESL >> 6));
  85. }
  86. return 0x01;
  87. }
  88. #elif defined(__STM32_HAL_ATY)
  89. uint16_t ADC_Get(uint8_t channel)
  90. {
  91. uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef * hadc);
  92. }
  93. #endif /* PLATFORM */
  94. #endif /* __HW_ADC_ATY_C */
  95. /******************************** End Of File *********************************/