0
\$\begingroup\$

I'm trying to read an analog value using the ADC of a ATSAM4E microcontroller. The problem i have is that it always reads 2047 or 2046 when the pin is connected to GND and 4096 when connected to VCC. I don't have it configured in differential mode.

Here is the code for it:

void adc_init(void){
    struct afec_config afec_cfg;
    afec_enable(AFEC0);
    afec_get_config_defaults(&afec_cfg);
    afec_init(AFEC0, &afec_cfg);
    afec_set_trigger(AFEC0, AFEC_TRIG_SW);
    afec_channel_enable(AFEC0, AFEC_CHANNEL_0);
}

//every 100ms
printf("CHANNEL 0: %d\r\n\n", afec_channel_get_value(AFEC0, AFEC_CHANNEL_0));
afec_start_software_conversion(AFEC0);

I don't know what the problem is. The ADC clock is 6MHz, so waiting 100ms between conversions should be sufficient to ensure it is finished.

It is a bit suspicious that the value is always so close to half. Least i got was 1790 but these "smaller" values occur after disconnecting the pin from VCC.

Anyone have any idea what it might be?

\$\endgroup\$
  • \$\begingroup\$ Looks like a differential ADC. \$\endgroup\$ – Gregory Kornblum Apr 17 '17 at 17:04
2
\$\begingroup\$

Did you read and do this...

The analog offset of the AFE is configured in the AOFF field in the Channel Offset Compensation register (AFEC_COCR). The offset is only available in Single-ended mode. The field AOFF must be configured to 2048 (mid scale of the DAC) when there is no offset error to compensate.To compensate for an offset error of n LSB (positive or negative), the field AOFF must be configured to 2048 + n.

Page 1276 of the spec sheet.

\$\endgroup\$
  • 1
    \$\begingroup\$ Yep, this was it.I didn't read this in the datasheet thinking that i didn't need to use it, but apparently it starts with an offset of 2048 by default. Writing 0 to the offset fixed it. Thanks for your help \$\endgroup\$ – user3808318 Apr 18 '17 at 15:53
  • \$\begingroup\$ @user3808318 yes, I would not have intuitively thought that either, but then again it makes sense to allow the user to apply a max +- half range offset. Though you would think it would reset to 2048... \$\endgroup\$ – Trevor_G Apr 18 '17 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.