diff --git a/Core/Src/freertos.c b/Core/Src/freertos.c index 000a6f0..bbe51f9 100644 --- a/Core/Src/freertos.c +++ b/Core/Src/freertos.c @@ -50,6 +50,7 @@ // 声明外部变量 extern ad7124_analog_t ad7124_analog[AD7124_CHANNEL_EN_MAX]; +extern ad7124_analog_t ad7124_analog2[AD7124_CHANNEL_EN_MAX]; // 添加第二个AD7124的采样数据结构体 /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -166,6 +167,22 @@ osSemaphoreId tca6416_semaphoreHandle; // 添加TCA6416信号量句柄 DMA_HandleTypeDef hdma_uart6_rx; // UART6接收DMA句柄 DMA_HandleTypeDef hdma_uart6_tx; // UART6发送DMA句柄 +// ADC滤波相关定义 +#define ADC_FILTER_COUNT 4 // 减少滤波点数,从10改为4,加快响应速度 +#define ADC_CHANNEL_MAX AD7124_CHANNEL_EN_MAX // 通道数量 +#define ADC_STARTUP_SAMPLES 20 // 启动时的快速采样次数 +#define ADC_STARTUP_INTERVAL 1 // 启动时的采样间隔(ms) +#define ADC_NORMAL_INTERVAL 5 // 正常采样间隔(ms) + +// 为每个ADC设备创建缓冲区 +static int32_t adc1_buffer[ADC_CHANNEL_MAX][ADC_FILTER_COUNT]; // 第一个AD7124的缓冲区 +static int32_t adc2_buffer[ADC_CHANNEL_MAX][ADC_FILTER_COUNT]; // 第二个AD7124的缓冲区 +static uint8_t adc1_buffer_index[ADC_CHANNEL_MAX] = {0}; // 第一个AD7124的缓冲区索引 +static uint8_t adc2_buffer_index[ADC_CHANNEL_MAX] = {0}; // 第二个AD7124的缓冲区索引 +static int32_t adc1_filtered_data[ADC_CHANNEL_MAX] = {0}; // 第一个AD7124的滤波后数据 +static int32_t adc2_filtered_data[ADC_CHANNEL_MAX] = {0}; // 第二个AD7124的滤波后数据 +static uint8_t adc_startup_count = 0; // 启动采样计数器 + /* USER CODE END Variables */ /* Private function prototypes -----------------------------------------------*/ @@ -420,25 +437,76 @@ void start_adc_task(void const *argument) { /* USER CODE BEGIN start_adc_task */ // 初始化两个AD7124设备 - ad7124_setup(1); // 初始化两个AD7124设备 - ad7124_setup(2); // 初始化两个AD7124设备 + ad7124_setup(1); // 初始化第一个AD7124设备 + ad7124_setup(2); // 初始化第二个AD7124设备 + + // 初始化缓冲区 + memset(adc1_buffer, 0, sizeof(adc1_buffer)); + memset(adc2_buffer, 0, sizeof(adc2_buffer)); + memset(adc1_buffer_index, 0, sizeof(adc1_buffer_index)); + memset(adc2_buffer_index, 0, sizeof(adc2_buffer_index)); + memset(adc1_filtered_data, 0, sizeof(adc1_filtered_data)); + memset(adc2_filtered_data, 0, sizeof(adc2_filtered_data)); + adc_startup_count = 0; /* Infinite loop */ for (;;) { - // 读取第一个AD7124的所有通道 - for (uint8_t ch = AD7124_AIN0; ch < AD7124_CHANNEL_EN_MAX; ch++) - { + // 启动时的快速采样模式 + if (adc_startup_count < ADC_STARTUP_SAMPLES) { + // 快速采样所有通道 + for (uint8_t ch = AD7124_AIN0; ch < AD7124_CHANNEL_EN_MAX; ch++) { + // 第一个AD7124 + ad7124_get_analog(ch, 1); + adc1_buffer[ch][adc1_buffer_index[ch]] = ad7124_analog[ch].data; + adc1_buffer_index[ch] = (adc1_buffer_index[ch] + 1) % ADC_FILTER_COUNT; + + // 第二个AD7124 + ad7124_get_analog(ch, 2); + adc2_buffer[ch][adc2_buffer_index[ch]] = ad7124_analog2[ch].data; + adc2_buffer_index[ch] = (adc2_buffer_index[ch] + 1) % ADC_FILTER_COUNT; + } + adc_startup_count++; + vTaskDelay(ADC_STARTUP_INTERVAL); // 启动时使用更短的采样间隔 + continue; + } + + // 正常采样模式 + for (uint8_t ch = AD7124_AIN0; ch < AD7124_CHANNEL_EN_MAX; ch++) { + // 第一个AD7124 ad7124_get_analog(ch, 1); + adc1_buffer[ch][adc1_buffer_index[ch]] = ad7124_analog[ch].data; + adc1_buffer_index[ch] = (adc1_buffer_index[ch] + 1) % ADC_FILTER_COUNT; + + // 计算平均值 + int64_t sum = 0; + for (uint8_t i = 0; i < ADC_FILTER_COUNT; i++) { + sum += adc1_buffer[ch][i]; + } + adc1_filtered_data[ch] = (int32_t)(sum / ADC_FILTER_COUNT); + + // 更新滤波后的数据 + ad7124_analog[ch].data = adc1_filtered_data[ch]; } - // 读取第二个AD7124的所有通道 - for (uint8_t ch = AD7124_AIN0; ch < AD7124_CHANNEL_EN_MAX; ch++) - { + // 第二个AD7124 + for (uint8_t ch = AD7124_AIN0; ch < AD7124_CHANNEL_EN_MAX; ch++) { ad7124_get_analog(ch, 2); + adc2_buffer[ch][adc2_buffer_index[ch]] = ad7124_analog2[ch].data; + adc2_buffer_index[ch] = (adc2_buffer_index[ch] + 1) % ADC_FILTER_COUNT; + + // 计算平均值 + int64_t sum = 0; + for (uint8_t i = 0; i < ADC_FILTER_COUNT; i++) { + sum += adc2_buffer[ch][i]; + } + adc2_filtered_data[ch] = (int32_t)(sum / ADC_FILTER_COUNT); + + // 更新滤波后的数据 + ad7124_analog2[ch].data = adc2_filtered_data[ch]; } - vTaskDelay(10); + vTaskDelay(ADC_NORMAL_INTERVAL); // 使用更短的正常采样间隔 } /* USER CODE END start_adc_task */ } diff --git a/Public/PCBA测试需求说明书 V1.1.docx b/Public/PCBA测试需求说明书 V1.1.docx index dfb1fd6..5297d77 100644 Binary files a/Public/PCBA测试需求说明书 V1.1.docx and b/Public/PCBA测试需求说明书 V1.1.docx differ diff --git a/User/application/src/tcpserverc.c b/User/application/src/tcpserverc.c index 63c29ac..aa09b01 100644 --- a/User/application/src/tcpserverc.c +++ b/User/application/src/tcpserverc.c @@ -386,7 +386,7 @@ uint16_t handle_type_86(const uint8_t *body, uint16_t body_len, uint8_t *tx) // 校验和 uint16_t checksum = 0; - for (int i = 4; i < 28; ++i) // 4~27(源地址+目标地址+类型+24字节) + for (int i = 4; i < total_len - 2; ++i) // 4~29(源地址+目标地址+类型+24字节) { checksum += tx[i]; } diff --git a/User/driver/ad7124.c b/User/driver/ad7124.c index fbd7e5e..850b143 100644 --- a/User/driver/ad7124.c +++ b/User/driver/ad7124.c @@ -437,17 +437,21 @@ int32_t ad7124_read_data(uint8_t device_num) void ad7124_get_analog(uint8_t channel_nr, uint8_t device_num) { int32_t read_data; - ad7124_regs[AD7124_CHANNEL_0].value = ad7124_channel_regs[channel_nr].value; - ad7124_write_register(&ad7124_regs[AD7124_CHANNEL_0], device_num); + ad7124_st_reg_t *regs = (device_num == 1) ? ad7124_regs : ad7124_regs2; + ad7124_st_reg_t *channel_regs = (device_num == 1) ? ad7124_channel_regs : ad7124_channel_regs2; + ad7124_analog_t *analog = (device_num == 1) ? ad7124_analog : ad7124_analog2; + + regs[AD7124_CHANNEL_0].value = channel_regs[channel_nr].value; + ad7124_write_register(®s[AD7124_CHANNEL_0], device_num); while (ad7124_wait_for_conv_ready(AD7124_RDY, device_num)); - ad7124_analog[channel_nr].channel = channel_nr; + analog[channel_nr].channel = channel_nr; read_data = ad7124_read_data(device_num); - ad7124_analog[channel_nr].data = read_data; - //ad7124_analog[channel_nr].voltage = (float)(read_data * VREF / GAIN / AD_CODE); + analog[channel_nr].data = read_data; + //analog[channel_nr].voltage = (float)(read_data * VREF / GAIN / AD_CODE); - ad7124_regs[AD7124_CHANNEL_0].value = 0; - ad7124_write_register(&ad7124_regs[AD7124_CHANNEL_0], device_num); + regs[AD7124_CHANNEL_0].value = 0; + ad7124_write_register(®s[AD7124_CHANNEL_0], device_num); }