luat_adc_air101.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "luat_adc.h"
  2. #include "luat_base.h"
  3. #include "wm_adc.h"
  4. #include "wm_gpio_afsel.h"
  5. #include "wm_include.h"
  6. // 0,1 标准ADC
  7. // 3 - 内部温度传感
  8. int luat_adc_open(int ch, void *args)
  9. {
  10. switch (ch)
  11. {
  12. case 0:
  13. wm_adc_config(ch);
  14. break;
  15. case 1:
  16. wm_adc_config(ch);
  17. break;
  18. #ifdef AIR103
  19. case 2:
  20. wm_adc_config(ch);
  21. break;
  22. case 3:
  23. wm_adc_config(ch);
  24. break;
  25. #endif
  26. case 10:
  27. return 0; // 温度传感器
  28. case 11:
  29. return 0; // VBAT电压
  30. default:
  31. return 1;
  32. }
  33. return 0;
  34. }
  35. int luat_adc_read(int ch, int *val, int *val2)
  36. {
  37. int voltage = 0;
  38. switch (ch)
  39. {
  40. case 0:
  41. voltage = adc_get_inputVolt2(ch, val);
  42. break;
  43. case 1:
  44. voltage = adc_get_inputVolt2(ch, val);
  45. break;
  46. #ifdef AIR103
  47. case 2:
  48. voltage = adc_get_inputVolt2(ch, val);
  49. break;
  50. case 3:
  51. voltage = adc_get_inputVolt2(ch, val);
  52. break;
  53. #endif
  54. case 10:
  55. voltage = adc_temp();
  56. *val = voltage;
  57. *val2 = voltage;
  58. return 0;
  59. case 11:
  60. voltage = adc_get_interVolt();
  61. *val = voltage;
  62. *val2 = voltage;
  63. return 0;
  64. default:
  65. return 1;
  66. }
  67. if (*val < 46134) {
  68. *val2 = 0;
  69. }
  70. else if (*val > 123405) {
  71. *val2 = 2300;
  72. }
  73. else {
  74. *val2 = (int)((double)(*val - 46134) / (double)(32.196));
  75. }
  76. return 0;
  77. }
  78. int luat_adc_close(int ch)
  79. {
  80. switch (ch)
  81. {
  82. case 0:
  83. tls_io_cfg_set(WM_IO_PA_01, WM_IO_OPTION5);
  84. break;
  85. case 1:
  86. tls_io_cfg_set(WM_IO_PA_04, WM_IO_OPTION5);
  87. break;
  88. #ifdef AIR103
  89. case 2:
  90. tls_io_cfg_set(WM_IO_PA_03, WM_IO_OPTION5);
  91. break;
  92. case 3:
  93. tls_io_cfg_set(WM_IO_PA_02, WM_IO_OPTION5);
  94. break;
  95. #endif
  96. case 10:
  97. break; // 温度
  98. case 11:
  99. break; // 内部电压
  100. default:
  101. return 1;
  102. }
  103. return 0;
  104. }