analog_input.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "at32f403a_407.h"
  2. #include "analog_input.h"
  3. #include "shift_reg.h"
  4. #include "utility.h"
  5. #include "FreeRTOS.h"
  6. #include "task.h"
  7. #include <stdio.h>
  8. uint8_t input_mux; // выход сдвигового регистра U1010 (управляет MUX 301, 401)
  9. // мультиплексоры отвечат за коммутацию аналоговых входов и АЦП
  10. uint16_t input_mode; // режим измерения аналоговых каналов (ток или напряжение)
  11. // 0000 0000 0000 0000
  12. // младшие 6 (с 1..6) бит - каналы с 1 по 6 соответственно
  13. // биты 9..14 - каналы с 7 по 12 соответственно
  14. //en_crnt_alrm_in
  15. // Подключить канал к АЦП
  16. // Одновременно могут быть подключены только 2 канала из наборов:
  17. // 1: AN_INP_1, AN_INP_2, AN_INP_3, AN_INP_4, AN_INP_5, AN_INP_6, V_ISO_CL,
  18. // V_ISO
  19. //
  20. // 2: AN_INP_7, AN_INP_8, AN_INP_9, AN_INP_10, AN_INP_11, AN_INP_12,
  21. // CRNT_LIM_U_BFR_R, CRNT_LIM_U_ABFR_R
  22. void ai_connect_channel(uint8_t channel)
  23. {
  24. if (channel < MUX_401_CH)
  25. {
  26. input_mux &= 0x70;
  27. switch (channel)
  28. {
  29. case AN_INP_1: input_mux |= 0x03; break; // U301 Y3
  30. case AN_INP_2: input_mux |= 0x00; break; // U301 Y0
  31. case AN_INP_3: input_mux |= 0x05; break; // U301 Y5
  32. case AN_INP_4: input_mux |= 0x07; break; // U301 Y7
  33. case AN_INP_5: input_mux |= 0x06; break; // U301 Y6
  34. case AN_INP_6: input_mux |= 0x04; break; // U301 Y4
  35. case V_ISO_CL: input_mux |= 0x01; break; // U301 Y1
  36. case V_ISO : input_mux |= 0x02; break; // U301 Y2
  37. default: break;
  38. }
  39. }
  40. else
  41. {
  42. input_mux &= 0x07;
  43. switch (channel)
  44. {
  45. case AN_INP_7: input_mux |= (0x04 << 4); break; // U401 Y4
  46. case AN_INP_8: input_mux |= (0x06 << 4); break; // U401 Y6
  47. case AN_INP_9: input_mux |= (0x07 << 4); break; // U401 Y7
  48. case AN_INP_10:input_mux |= (0x05 << 4); break; // U401 Y5
  49. case AN_INP_11:input_mux |= (0x02 << 4); break; // U401 Y2
  50. case AN_INP_12:input_mux |= (0x01 << 4); break; // U401 Y1
  51. case CRNT_LIM_U_BFR_R: input_mux |= (0x00 << 4); break; // U401 Y0
  52. case CRNT_LIM_U_ABFR_R: input_mux |= (0x03 << 4); break; // U401 Y3
  53. default: break;
  54. }
  55. }
  56. sh_ai_connect(input_mux);
  57. printf("Analog input connect register: ");
  58. print_binary_byte(input_mux);
  59. }
  60. // Утсновить режим измерения канала (ток или напряжение)
  61. void ai_set_mode(MEAS_CHAN_MODE_t mode, uint8_t channel)
  62. {
  63. if (mode == MEAS_CURRENT)
  64. {
  65. if (channel < 7)
  66. input_mode |= (1 << (channel - 1));
  67. else
  68. input_mode |= (1 << (channel + 1));
  69. }
  70. else
  71. if (channel < 7)
  72. input_mode &= ~(1 << (channel - 1));
  73. else
  74. input_mode &= ~(1 << (channel + 1));
  75. sh_ai_mode(input_mode);
  76. printf("Analog input mode: ");
  77. print_binary_half_word(input_mode);
  78. }
  79. //
  80. void ai_connect_test(void)
  81. {
  82. ai_connect_channel(AN_INP_1);
  83. ai_connect_channel(AN_INP_2);
  84. ai_connect_channel(AN_INP_3);
  85. ai_connect_channel(AN_INP_4);
  86. ai_connect_channel(AN_INP_5);
  87. ai_connect_channel(AN_INP_6);
  88. ai_connect_channel(V_ISO_CL);
  89. ai_connect_channel(V_ISO);
  90. ai_connect_channel(AN_INP_7);
  91. ai_connect_channel(AN_INP_8);
  92. ai_connect_channel(AN_INP_9);
  93. ai_connect_channel(AN_INP_10);
  94. ai_connect_channel(AN_INP_11);
  95. ai_connect_channel(AN_INP_12);
  96. ai_connect_channel(CRNT_LIM_U_BFR_R);
  97. ai_connect_channel(CRNT_LIM_U_ABFR_R);
  98. }
  99. //
  100. void ai_mode_test(void)
  101. {
  102. for (uint8_t i = 1; i < 13; i++)
  103. {
  104. ai_set_mode(MEAS_VOLTAGE, i);
  105. }
  106. }