#include "ProximitySwitches.h" #include "main.h" uint32_t dma_adc1_buffer[ADC1_NUM_CHANNELS * NUM_SAMPLES_PER_CHANNEL]; uint32_t dma_adc2_buffer[ADC2_NUM_CHANNELS * NUM_SAMPLES_PER_CHANNEL]; uint32_t dma_adc3_buffer[ADC3_NUM_CHANNELS * NUM_SAMPLES_PER_CHANNEL]; uint32_t dma_adc1_buffer_av[ADC1_NUM_CHANNELS]; uint32_t dma_adc2_buffer_av[ADC2_NUM_CHANNELS]; uint32_t dma_adc3_buffer_av[ADC3_NUM_CHANNELS]; uint32_t samples_data[NUM_SAMPLES_PER_CHANNEL]; float prox_switch_A_vol[11]; float prox_switch_B_vol[11]; uint32_t prox_switch_A_t[4][10]; uint32_t prox_switch_B_t[4][10]; proxi_switch_t proxi_switch_A[10]; proxi_switch_t proxi_switch_B[10]; senors_error_t senors_A; senors_error_t senors_B; void fun_ini_adc() { HAL_ADC_Start_DMA(&hadc1, (uint32_t *)&dma_adc1_buffer, ADC1_NUM_CHANNELS * NUM_SAMPLES_PER_CHANNEL); HAL_ADC_Start_DMA(&hadc2, (uint32_t *)&dma_adc2_buffer, ADC2_NUM_CHANNELS * NUM_SAMPLES_PER_CHANNEL); HAL_ADC_Start_DMA(&hadc3, (uint32_t *)&dma_adc3_buffer, ADC3_NUM_CHANNELS * NUM_SAMPLES_PER_CHANNEL); } void fun_BubbleSort(uint32_t *arr, uint32_t size) { uint32_t i, j, tmp; for (i = 0; i < size - 1; i++) { for (j = 0; j < size - i - 1; j++) { if (arr[j] > arr[j + 1]) { tmp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = tmp; } } } } static uint32_t fun_filter(uint32_t *arr, uint32_t size, uint32_t num) { if ((arr == NULL) || (size == 0)) return 0; uint8_t i; uint32_t temp = 0; fun_BubbleSort(arr, size); for (i = num; i < size - num; i++) { temp += arr[i]; } return temp / (size - 2 * num); } static void fun_get_adc1(void) { for (uint8_t channel = 0; channel < ADC1_NUM_CHANNELS; channel++) { for (uint8_t i = 0; i < NUM_SAMPLES_PER_CHANNEL; i++) { samples_data[i] = dma_adc1_buffer[ADC1_NUM_CHANNELS * i + channel]; } dma_adc1_buffer_av[channel] = fun_filter(samples_data, NUM_SAMPLES_PER_CHANNEL, 5); /* 电流限幅 6mA I = ADC * (2.5/4096) / 250R <= 0.006 ADC <= 2457.6 */ if (dma_adc1_buffer_av[channel] > 2457) dma_adc1_buffer_av[channel] = 2457; } } static void fun_get_adc2(void) { for (uint8_t channel = 0; channel < ADC2_NUM_CHANNELS; channel++) { for (uint8_t i = 0; i < NUM_SAMPLES_PER_CHANNEL; i++) { samples_data[i] = dma_adc2_buffer[ADC2_NUM_CHANNELS * i + channel]; } dma_adc2_buffer_av[channel] = fun_filter(samples_data, NUM_SAMPLES_PER_CHANNEL, 5); } } static void fun_get_adc3(void) { for (uint8_t channel = 0; channel < ADC3_NUM_CHANNELS; channel++) { for (uint8_t i = 0; i < NUM_SAMPLES_PER_CHANNEL; i++) { samples_data[i] = dma_adc3_buffer[ADC3_NUM_CHANNELS * i + channel]; } dma_adc3_buffer_av[channel] = fun_filter(samples_data, NUM_SAMPLES_PER_CHANNEL, 5); /* 电流限幅 6mA I = ADC * (2.5/4096) / 250R <= 0.006 ADC <= 2457.6 */ if (dma_adc3_buffer_av[channel] > 2457) dma_adc3_buffer_av[channel] = 2457; } } void fun_get_senors_error(senors_error_t *senors) { if (!senors) return; uint16_t *error = &senors->error; if (senors == &senors_A) { if (HAL_GPIO_ReadPin(NAMUR_OC_A_GPIO_Port, NAMUR_OC_A_Pin) == GPIO_PIN_RESET) *error |= ERR_NUMAR; else *error &= ~ERR_NUMAR; } else if (senors == &senors_B) { if (HAL_GPIO_ReadPin(NAMUR_OC_B_GPIO_Port, NAMUR_OC_B_Pin) == GPIO_PIN_RESET) *error |= ERR_NUMAR; else *error &= ~ERR_NUMAR; } } void fun_get_switch_vol() { fun_get_adc1(); prox_switch_A_vol[1] = VREF * dma_adc1_buffer_av[11] / 4096; prox_switch_A_vol[2] = VREF * dma_adc1_buffer_av[10] / 4096; prox_switch_A_vol[3] = VREF * dma_adc1_buffer_av[5] / 4096; prox_switch_A_vol[4] = VREF * dma_adc1_buffer_av[4] / 4096; prox_switch_A_vol[5] = VREF * dma_adc1_buffer_av[3] / 4096; prox_switch_A_vol[6] = VREF * dma_adc1_buffer_av[2] / 4096; prox_switch_A_vol[7] = VREF * dma_adc1_buffer_av[1] / 4096; prox_switch_A_vol[8] = VREF * dma_adc1_buffer_av[0] / 4096; prox_switch_A_vol[9] = VREF * dma_adc1_buffer_av[9] / 4096; prox_switch_A_vol[10] = VREF * dma_adc1_buffer_av[8] / 4096; prox_switch_B_vol[1] = VREF * dma_adc1_buffer_av[7] / 4096; prox_switch_B_vol[2] = VREF * dma_adc1_buffer_av[6] / 4096; fun_get_adc2(); prox_switch_A_vol[0] = 4 * VREF * dma_adc2_buffer_av[1] / 4096; prox_switch_B_vol[0] = 4 * VREF * dma_adc2_buffer_av[0] / 4096; fun_get_adc3(); prox_switch_B_vol[3] = VREF * dma_adc3_buffer_av[4] / 4096; prox_switch_B_vol[4] = VREF * dma_adc3_buffer_av[3] / 4096; prox_switch_B_vol[5] = VREF * dma_adc3_buffer_av[2] / 4096; prox_switch_B_vol[6] = VREF * dma_adc3_buffer_av[1] / 4096; prox_switch_B_vol[7] = VREF * dma_adc3_buffer_av[0] / 4096; prox_switch_B_vol[8] = VREF * dma_adc3_buffer_av[7] / 4096; prox_switch_B_vol[9] = VREF * dma_adc3_buffer_av[6] / 4096; prox_switch_B_vol[10] = VREF * dma_adc3_buffer_av[5] / 4096; }