I just got an Arduino Due, and in testing the ADC and DAC functionality came across an issue I couldn't resolve.
I tested the ADC/DAC by connecting A0 and DAC0 together. I then set the output of the DAC0 in the loop between the values of 0 and 4095. Then I read A0 with analogRead. A funny thing happens with the read though. While I set the DAC to go between 0-4095, the values I read with the ADC range from 663-3400 (approximately 600 bits off either rail). I have generated an image showing this (http://i50.tinypic.com/2hcp8wx.png).
I could map to the acquired range to the desired range, but this doesn't really fix the issue. I'm hoping someone can help me understand why this is happening and offer a solution to reading values between 0 and 4095. When I probe VCC and GND with the ADC I read 4080 and 0 respectively.
My code is below:
const uint32_t DACPIN = DAC0;
const uint32_t ADCPIN = A0;
void setup( void ) {
// Set up serial port
Serial.begin( 115200 );
// Set up ADC/DAC
analogWriteResolution( 12 );
analogReadResolution( 12 );
pinMode( DACPIN, OUTPUT );
}
// Main loop
uint16_t val = 0;
void loop( void ) {
val = (val+1) & 0x0FFF;
// Write and read the values
analogWrite( DACPIN, val );
delay( 1 );
const int adcVal = analogRead( ADCPIN );
delay( 1 );
Serial.print( "[" );
Serial.print( val );
Serial.print( "," );
Serial.print( adcVal );
Serial.print( "],\n" );
}