Advertisement
SuperBrainAK

precise adc values from the vref pin.

Feb 24th, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. getting precise adc readings, originally from http://hacking.majenko.co.uk/node/57
  2.  
  3. long readVcc() {
  4.  long result;
  5.  // Read 1.1V reference against AVcc ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); delay(2);
  6.  // Wait for Vref to settle ADCSRA |= _BV(ADSC);
  7.  // Convert while (bit_is_set(ADCSRA,ADSC));
  8.  result = ADCL;
  9.  result |= ADCH<<8;
  10.  result = 1125300L / result;
  11.  // Back-calculate AVcc in mV return result;
  12.  }
  13.  
  14.  void setup() {
  15.  Serial.begin(9600);
  16.  }
  17.  void loop() {
  18.  Serial.println( readVcc(), DEC );
  19.  delay(1000);
  20.  }
  21.  
  22. So now, using that, your ADC code could now look like this:
  23. unsigned int ADCValue;
  24.  double Voltage;
  25.  double Vcc;
  26.  Vcc = readVcc()/1000.0;
  27.  ADCValue = analogRead(0);
  28.  Voltage = (ADCValue / 1023.0) * Vcc;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement