GithubHelp home page GithubHelp logo

atsamd21-adc's Introduction

ATSAMD21-ADC

Functions for many of the ADC capabilities of the Atmel SAMD21 series. Currently this requires some of the functions given in the Arduino IDE, but I will make it optional in the future. Tested on the Arduino MKR1000. Datasheet.

Analog Read Functions

uint8_t analogReadExtended(uint8_t bits)

Allows for the ADC to read 8, 10, or 12 bits normally or 13-16 bits using oversampling and decimation. This works with all analog read functions, including analogRead(uint8_t pin) given in the Arduino IDE. See pages 853 & 862 of the datasheet. Arduino has 10 as default.

  • 8, 10, 12 bit = 1 sample
  • 13 bit = 4 samples
  • 14 bit = 16 samples
  • 15 bit = 64 samples
  • 16 bit = 256 samples

int16_t analogDifferential(uint8_t pos_pin, uint8_t neg_pin)

Reads a differential voltage with pos_pin and neg_pin. These pins correspond to the values given in the Arduino headers. For example, to connect A0 and A3 on the Arduino use analogDifferential(A0,A3). neg_pin cannot be A1 or A2 on the Arduino MKR1000, as these are connected internally to different pins on the main chip. See pages 869-870 of the datasheet for more information on that. This code performs very similarly to analogRead(uint8_t pin) given in the Arduino IDE, but puts it into differential mode instead of single-ended. It also shuts off the DAC if one of the pins you selected is connected to it internally.

int16_t analogDifferentialRaw(uint8_t mux_pos, uint8_t mux_neg)

This is very similar to the analogDifferential(uint8_t pos_pin, uint8_t neg_pin) function above, but instead takes in takes in the hex values for the pins you want to connect. See pages 869-870 of the datasheet for more information on this. Note that the internal pin values and the pins broken out on the Arduino boards do not necessarily match. This also doesn't check that the pin used is internally connected to the DAC.

mux_pos values can be found on page 870 of the datasheet. These are:

  • 0-19 - corresponding to ADC AIN 0-19 pins.
  • ADC_PIN_TEMP - Temperature reference
  • ADC_PIN_BANDGAP - Bandgap voltage
  • ADC_PIN_SCALEDCOREVCC - 1/4 scaled core supply
  • ADC_PIN_SCALEDIOVCC - 1/4 scaled I/O supply
  • ADC_PIN_DAC - DAC output

mux_neg values can be found on page 869 of the datasheet. These are:

  • 0-7 - corresponding to ADC AIN 0-7 pins.
  • ADC_PIN_GND - Internal ground
  • ADC_PIN_IOGND - I/O ground

uint8_t internalPinValue(uint8_t pin)

Returns the internal value of an external analog pin. Useful for determining pin numbers for the analogDifferentialRaw function above.

ADC Settings

void analogGain(uint8_t gain)

Sets the gain of the ADC. See pages 868-869 of the datasheet. Arduino has ADC_GAIN_DIV2 as default. Possible options are:

  • ADC_GAIN_1 - 1x
  • ADC_GAIN_2 - 2x
  • ADC_GAIN_4 - 4x
  • ADC_GAIN_8 - 8x
  • ADC_GAIN_16 - 16x
  • ADC_GAIN_DIV2 - 1/2x

void analogCalibrate()

Copies the values from the NVM software calibration registry to the ADC CALIB registry. See page 882 of the datasheet. Note that I used the method done by the Arduino IDE to read the NVM software calibration registry, but I cannot verify it was done so correctly. Any help with this would be appreciated.

void analogReference2(uint8_t ref)

This is similar to the analogReference(uint8_t ref) function defined in the Arduino IDE, but that version alters the gain depending on the desired reference. This does not. Arduino has ADC_REF_VCC1 as default. See page 861 of the datasheet. Possible options are:

  • ADC_REF_INT1V - 1.0V reference
  • ADC_REF_VCC0 - 1/1.48 VDDANA
  • ADC_REF_VCC1 - 1/2 VDDANA
  • ADC_REF_VREFA - external reference
  • ADC_REF_VREFB - external reference

void analogReferenceCompensation(uint8_t val)

Increases accuracy of gain stage by enabling the reference buffer offset compensation. Takes longer to start. See datasheet page 861. Arduino has it disabled by default.

  • 0 - disable the reference buffer offset compensation
  • 1 - enable the reference buffer offset compensation

void analogPrescaler(uint8_t val)

Sets the ADC clock relative to the peripheral clock. See page 864 of the datasheet. Arduino default is ADC_PRESCALER_DIV512

  • ADC_PRESCALER_DIV4
  • ADC_PRESCALER_DIV8
  • ADC_PRESCALER_DIV16
  • ADC_PRESCALER_DIV32
  • ADC_PRESCALER_DIV64
  • ADC_PRESCALER_DIV128
  • ADC_PRESCALER_DIV256
  • ADC_PRESCALER_DIV512

void analogReset()

Resets all ADC registers except DBGCTRL back to their original values. Note that it resets them to the cpu defaults, not the Arduino IDE defaults. See page 860 of the datasheet.

atsamd21-adc's People

Contributors

molorius avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

atsamd21-adc's Issues

analogDifferential 11 bits only?

I'm not quite clear if this is a bug or if I'm missing something here, any help to get to the bottom of this would be great.

Is there any way to obtain a true 12bit differential result using analogDifferential()?

For instance if we do a single ended analogRead of a 3V input the 12bit ADC result is the expected ~3720 (of a max 4095).
Simply switching ADC->CTRLB.reg to CTRL DIFFMODE and setting the MUXPOS/MUXNEGs, then applying 3V of difference between these differential inputs, produce half of the expected value. The reference is unchanged in both cases and is the default analogReference(AR_DEFAULT) which should be 3.3V. If I change the reference to not do a DIV2 then the result saturates to 2047 (11bit).

In this Arduino forum post you seem to suggest the true 12bit value in differential reads is still available but I could not find a way to obtain it. Or maybe differential mode shifts the reference to half of whatever it is set to, and then yields a signed 12bit (11bits + 1 sign bit) result from that? That would seem quite wrong.

Multiple ADC differential measurement

I have ran into a problem which I have tried to find the cause for, but not being able to.

I am trying to establish 3 ADC differential channels (tot. 6 pins) for measuring 3 different analog signals. However this is not working if I have all the three analog signals connected to the 3 ADC differential channels.

If I disconnect all the analog signals. Connect only one at a time and measure it, and disconnect the signal after measurement, then all the three ADC differential channels will work. See detailed explanation below,
AS# = Analog signal
ADCD# = ADC differential (each channel has 2 pins)

// Results of all three ADC are wrong
AS1 - connected - ADCD1
AS2 - connected - ADCD2
AS3 - connected - ADCD3

// Works
Step 1
AS1 - connected - ADCD1
AS2 - disconnected - ADCD2
AS3 - disconnected - ADCD3

Step 2
AS1 - disconnected - ADCD1
AS2 - connected - ADCD2
AS3 - disconnected - ADCD3

Step 3
AS1 - disconnected - ADCD1
AS2 - disconnected - ADCD2
AS3 - connected - ADCD3

My code:
/*
Name: AdcDifferential.ino
Created: 11/16/2020 9:26:38 AM
Author: SherjeelTon
*/

#include "ATSAMD21_ADC.h";

#define Negative1 A0
#define Positive1 A3

#define Negative2 A4
#define Positive2 A6

#define Negative3 A5
#define Positive3 A7

int16_t signedResult;
float result;
float voltRef = 2.048;
float elevenBitResolution = 2047;
float fifteenBitResolution = 32767;

// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(9600);
analogReference2(ADC_REF_VREFA);
analogReadExtended(16);
analogPrescaler(ADC_PRESCALER_DIV512);
analogGain(ADC_GAIN_1);
analogCalibrate();
}

// the loop function runs over and over again until power down or reset
void loop() {
signedResult = analogDifferential(Positive1, Negative1);
result = signedResult * voltRef / fifteenBitResolution;
Serial.print("Channel 1: ");
Serial.println(result, 6);
delay(1000);

signedResult = analogDifferential(Positive2, Negative2);
result = signedResult * voltRef / fifteenBitResolution;
Serial.print("Channel 2: ");
Serial.println(result, 6);
delay(1000);

signedResult = analogDifferential(Positive3, Negative3);
result = signedResult * voltRef / fifteenBitResolution;
Serial.print("Channel 3: ");
Serial.println(result, 6);
delay(1000);

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.