Skip to content

Commit 955f4d5

Browse files
authored
fix: update ADMUX at end of ADC ISR to meet ATmega328P timing (#126)
* fix: update ADMUX at end of ADC ISR to meet ATmega328P timing Move ADMUX write to the end of the ADC_vect ISR so at least 128 CPU cycles have passed since the trigger event (per ATmega328P datasheet). Prevents premature channel switching and ensures correct sampling timing. * #126 (comment)
1 parent cd32cb1 commit 955f4d5

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

Mk2_3phase_RFdatalog_temp/processing.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,8 +1193,6 @@ ISR(ADC_vect)
11931193
{
11941194
uint16_t adc_raw = ADC;
11951195

1196-
ADMUX = _ctx->admux;
1197-
11981196
if ((_ctx->index & 1) == 0)
11991197
{ // even=voltage, odd=current
12001198
// process voltage channel
@@ -1206,5 +1204,9 @@ ISR(ADC_vect)
12061204
processCurrentRawSample(_ctx->index >> 1, adc_raw);
12071205
}
12081206

1207+
// Set ADMUX at the end of the interrupt to ensure at least 128 CPU cycles
1208+
// have passed since the trigger event (ATmega328p datasheet requirement)
1209+
ADMUX = _ctx->admux;
1210+
12091211
_ctx = _ctx->next;
12101212
} // end of ISR

0 commit comments

Comments
 (0)