Skip to content

Instantly share code, notes, and snippets.

@DurvalMenezes
Last active July 6, 2023 15:42
Show Gist options
  • Save DurvalMenezes/4465976a07dd99475fb35c27d217bf31 to your computer and use it in GitHub Desktop.
Save DurvalMenezes/4465976a07dd99475fb35c27d217bf31 to your computer and use it in GitHub Desktop.
#Fixes ATTiny ADC setup/config issues
#See https://budgetlightforum.com/t/anduril-2-feature-change-suggestions/218045/467
#2023/07/05 Written [by @SammysHP on https://budgetlightforum.com/]
--- a/ToyKeeper/spaghetti-monster/fsm-adc.c
+++ b/ToyKeeper/spaghetti-monster/fsm-adc.c
@@ -83,11 +83,6 @@ inline void adc_sleep_mode() {
// attiny1634
set_sleep_mode(SLEEP_MODE_ADC);
#elif defined(AVRXMEGA3) // ATTINY816, 817, etc
- // set the RUNSTDBY bit so ADC will run in standby mode
- ADC0.CTRLA |= 1;
- // set a INITDLY value because the AVR manual says so
- // (section 30.3.5)
- ADC0.CTRLD |= (1 << 5);
set_sleep_mode(SLEEP_MODE_STANDBY);
#else
#error No ADC sleep mode defined for this hardware.
@@ -133,10 +128,10 @@ inline void ADC_on()
ADCSRA = (1 << ADEN) | (1 << ADSC) | (1 << ADATE) | ADC_PRSCL;
//ADCSRA |= (1 << ADSC); // start measuring
#elif defined(AVRXMEGA3) // ATTINY816, 817, etc
- set_admux_voltage();
VREF.CTRLA |= VREF_ADC0REFSEL_1V1_gc; // Set Vbg ref to 1.1V
- ADC0.CTRLA = ADC_ENABLE_bm | ADC_FREERUN_bm; // Enabled, free-running (aka, auto-retrigger)
- ADC0.COMMAND |= ADC_STCONV_bm; // Start the ADC conversions
+ ADC0.CTRLA = ADC_ENABLE_bm | ADC_FREERUN_bm | ADC_RUNSTBY_bm; // Enable ADC, free running mode, run in standby
+ ADC0.CTRLD |= ADC_INITDLY_DLY16_gc; // Delay first measurement to stabilize reference
+ set_admux_voltage();
#else
#error Unrecognized MCU type
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment