Made two minimal, surgical fixes to stabilize ON/OFF splitting.
What I changed
- Core/Inc/main.h:76
- Set ADC_BUFF_SIZE from 50 to 64 so each half is even (32). This keeps the “i &
1” parity consistent across half-buffer boundaries.
- Core/Src/stm32f4xx_it.c:388
- Fixed N after splitting the first half: ADC_proc.N = (ADC_BUFF_SIZE/2 -
Sweep_state.curr_step_start_DMA_N)/2;
- Previously it used (Sweep_state.curr_step_start_DMA_N)/2, which was wrong for
that segment.
Why this helps
- With ADC_BUFF_SIZE=50, half-size is 25 (odd). That flips ON/OFF labeling each half
because i & 1 parity shifts by 25, mixing levels and driving avg_ON and avg_OFF
together.
- The N bug skewed normalization, further flattening differences between averages.
How to verify
- Build and flash: make && make flash.
- Observe avg_ON/avg_OFF over CDC. They should now differ consistently; inverting
meандр should swap them cleanly.
- If still needed, I can add a global sample counter (sample_seq) for fully robust
ON/OFF classification without relying on buffer indices.
This commit is contained in:
@ -72,9 +72,9 @@ void Error_Handler(void);
|
||||
/* Shared ADC app types and declarations */
|
||||
|
||||
/* Size of circular DMA buffer for ADC1 */
|
||||
#ifndef ADC_BUFF_SIZE
|
||||
#define ADC_BUFF_SIZE 50
|
||||
#endif
|
||||
#ifndef ADC_BUFF_SIZE
|
||||
#define ADC_BUFF_SIZE 64
|
||||
#endif
|
||||
|
||||
#define SYNC_DET_ON
|
||||
|
||||
|
||||
@ -377,15 +377,15 @@ void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)
|
||||
ADC_proc.avg_ON = 0;
|
||||
ADC_proc.status = 1; // collecting data
|
||||
|
||||
for (uint32_t i = Sweep_state.curr_step_start_DMA_N; i < ADC_BUFF_SIZE/2; i++) {
|
||||
if ((i & 1) != 0){
|
||||
|
||||
ADC_proc.sum_ON += ADC1_buff_circular[i];
|
||||
}else{
|
||||
ADC_proc.sum_OFF += ADC1_buff_circular[i];
|
||||
}
|
||||
}
|
||||
ADC_proc.N = (Sweep_state.curr_step_start_DMA_N)/2;
|
||||
for (uint32_t i = Sweep_state.curr_step_start_DMA_N; i < ADC_BUFF_SIZE/2; i++) {
|
||||
if ((i & 1) != 0){
|
||||
|
||||
ADC_proc.sum_ON += ADC1_buff_circular[i];
|
||||
}else{
|
||||
ADC_proc.sum_OFF += ADC1_buff_circular[i];
|
||||
}
|
||||
}
|
||||
ADC_proc.N = (ADC_BUFF_SIZE/2 - Sweep_state.curr_step_start_DMA_N)/2;
|
||||
|
||||
}else{
|
||||
for (uint32_t i = 0; i < ADC_BUFF_SIZE/2; i++) {
|
||||
|
||||
Reference in New Issue
Block a user