123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- #include "at32f403a_407.h"
- #include "analog_input.h"
- #include "shift_reg.h"
- #include "utility.h"
- #include "FreeRTOS.h"
- #include "task.h"
- #include <stdio.h>
- uint8_t input_mux; // выход сдвигового регистра U1010 (управляет MUX 301, 401)
- // мультиплексоры отвечат за коммутацию аналоговых входов и АЦП
- uint16_t input_mode; // режим измерения аналоговых каналов (ток или напряжение)
- // 0000 0000 0000 0000
- // младшие 6 (с 1..6) бит - каналы с 1 по 6 соответственно
- // биты 9..14 - каналы с 7 по 12 соответственно
- //en_crnt_alrm_in
- // Подключить канал к АЦП
- // Одновременно могут быть подключены только 2 канала из наборов:
- // 1: AN_INP_1, AN_INP_2, AN_INP_3, AN_INP_4, AN_INP_5, AN_INP_6, V_ISO_CL,
- // V_ISO
- //
- // 2: AN_INP_7, AN_INP_8, AN_INP_9, AN_INP_10, AN_INP_11, AN_INP_12,
- // CRNT_LIM_U_BFR_R, CRNT_LIM_U_ABFR_R
- void ai_connect_channel(uint8_t channel)
- {
- if (channel < MUX_401_CH)
- {
- input_mux &= 0x70;
-
- switch (channel)
- {
- case AN_INP_1: input_mux |= 0x03; break; // U301 Y3
- case AN_INP_2: input_mux |= 0x00; break; // U301 Y0
- case AN_INP_3: input_mux |= 0x05; break; // U301 Y5
- case AN_INP_4: input_mux |= 0x07; break; // U301 Y7
- case AN_INP_5: input_mux |= 0x06; break; // U301 Y6
- case AN_INP_6: input_mux |= 0x04; break; // U301 Y4
- case V_ISO_CL: input_mux |= 0x01; break; // U301 Y1
- case V_ISO : input_mux |= 0x02; break; // U301 Y2
- default: break;
- }
- }
- else
- {
- input_mux &= 0x07;
-
- switch (channel)
- {
- case AN_INP_7: input_mux |= (0x04 << 4); break; // U401 Y4
- case AN_INP_8: input_mux |= (0x06 << 4); break; // U401 Y6
- case AN_INP_9: input_mux |= (0x07 << 4); break; // U401 Y7
- case AN_INP_10:input_mux |= (0x05 << 4); break; // U401 Y5
- case AN_INP_11:input_mux |= (0x02 << 4); break; // U401 Y2
- case AN_INP_12:input_mux |= (0x01 << 4); break; // U401 Y1
- case CRNT_LIM_U_BFR_R: input_mux |= (0x00 << 4); break; // U401 Y0
- case CRNT_LIM_U_ABFR_R: input_mux |= (0x03 << 4); break; // U401 Y3
- default: break;
- }
- }
-
- sh_ai_connect(input_mux);
-
- printf("Analog input connect register: ");
- print_binary_byte(input_mux);
- printf("\r\n");
- }
- // Утсновить режим измерения канала (ток или напряжение)
- void ai_set_mode(MEAS_CHAN_MODE_t mode, uint8_t channel)
- {
- if (mode == MEAS_CURRENT)
- {
- if (channel < 7)
- input_mode |= (1 << (channel - 1));
- else
- input_mode |= (1 << (channel + 1));
- }
- else
- if (channel < 7)
- input_mode &= ~(1 << (channel - 1));
- else
- input_mode &= ~(1 << (channel + 1));
-
- sh_ai_mode(input_mode);
-
- printf("Analog input mode: ");
- print_binary_byte(input_mode >> 8);
- printf("");
- print_binary_byte(input_mode & 0xFF);
- printf("\r\n");
- }
- //
- void ai_connect_test(void)
- {
- ai_connect_channel(AN_INP_1);
- ai_connect_channel(AN_INP_2);
- ai_connect_channel(AN_INP_3);
- ai_connect_channel(AN_INP_4);
- ai_connect_channel(AN_INP_5);
- ai_connect_channel(AN_INP_6);
- ai_connect_channel(V_ISO_CL);
- ai_connect_channel(V_ISO);
-
- ai_connect_channel(AN_INP_7);
- ai_connect_channel(AN_INP_8);
- ai_connect_channel(AN_INP_9);
- ai_connect_channel(AN_INP_10);
- ai_connect_channel(AN_INP_11);
- ai_connect_channel(AN_INP_12);
- ai_connect_channel(CRNT_LIM_U_BFR_R);
- ai_connect_channel(CRNT_LIM_U_ABFR_R);
- }
- //
- void ai_mode_test(void)
- {
- for (uint8_t i = 1; i < 13; i++)
- {
- ai_set_mode(MEAS_CURRENT, i);
- }
- }
|