diff --git a/src/machine/machine_atsamd21.go b/src/machine/machine_atsamd21.go index e886f937..76d6c5d4 100644 --- a/src/machine/machine_atsamd21.go +++ b/src/machine/machine_atsamd21.go @@ -433,7 +433,18 @@ func (a ADC) Get() uint16 { sam.ADC.CTRLA.ClearBits(sam.ADC_CTRLA_ENABLE) waitADCSync() - return uint16(val) << 4 // scales from 12 to 16-bit result + // scales to 16-bit result + switch (sam.ADC.CTRLB.Get() & sam.ADC_CTRLB_RESSEL_Msk) >> sam.ADC_CTRLB_RESSEL_Pos { + case sam.ADC_CTRLB_RESSEL_8BIT: + val = val << 8 + case sam.ADC_CTRLB_RESSEL_10BIT: + val = val << 6 + case sam.ADC_CTRLB_RESSEL_16BIT: + val = val << 4 + case sam.ADC_CTRLB_RESSEL_12BIT: + val = val << 4 + } + return val } func (a ADC) getADCChannel() uint8 { diff --git a/src/machine/machine_atsamd51.go b/src/machine/machine_atsamd51.go index a251f670..0ce476cc 100644 --- a/src/machine/machine_atsamd51.go +++ b/src/machine/machine_atsamd51.go @@ -863,7 +863,18 @@ func (a ADC) Get() uint16 { for bus.SYNCBUSY.HasBits(sam.ADC_SYNCBUSY_ENABLE) { } - return uint16(val) << 4 // scales from 12 to 16-bit result + // scales to 16-bit result + switch (bus.CTRLB.Get() & sam.ADC_CTRLB_RESSEL_Msk) >> sam.ADC_CTRLB_RESSEL_Pos { + case sam.ADC_CTRLB_RESSEL_8BIT: + val = val << 8 + case sam.ADC_CTRLB_RESSEL_10BIT: + val = val << 6 + case sam.ADC_CTRLB_RESSEL_16BIT: + val = val << 4 + case sam.ADC_CTRLB_RESSEL_12BIT: + val = val << 4 + } + return val } func (a ADC) getADCBus() *sam.ADC_Type {