Calibration of band gap voltage on teensy

Im working on creating a range of board based around a teensey 3.2. I can make everything work happily but cant get the band gap calibration to run .
When compiling it boms out at the below if anyone has any ideas that would be most appreciated.

Arduino: 1.8.5 (Windows 10), TD: 1.41, Board: “Teensy 3.2 / 3.1, Serial, 96 MHz (overclock), Faster, US English”

sketch_mar10a: In function ‘long int readVcc()’:
sketch_mar10a:120: error: ‘ADCSRA’ was not declared in this scope
ADCSRA |= _BV(ADSC); // Convert

^

In file included from C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/wiring.h:38:0,

             from C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/WProgram.h:45,

             from C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/Arduino.h:3,

             from C:\Users\Dafydd\Documents\Arduino\sketch_mar10a\sketch_mar10a.ino:31:

sketch_mar10a:120: error: ‘ADSC’ was not declared in this scope
ADCSRA |= _BV(ADSC); // Convert

             ^

C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/core_pins.h:47:22: note: in definition of macro ‘_BV’

#define _BV(n) (1<<(n))

                  ^

sketch_mar10a:121: error: ‘bit_is_set’ was not declared in this scope
while (bit_is_set(ADCSRA,ADSC));

                            ^

sketch_mar10a:122: error: ‘ADCL’ was not declared in this scope
result = ADCL;

        ^

sketch_mar10a:123: error: ‘ADCH’ was not declared in this scope
result |= ADCH<<8;

         ^

‘ADCSRA’ was not declared in this scope

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

Those are predefined in one of the Arduino header files for the Atmel 328P
Your Teensy is a different processor, so you can’t expect that code to work.

You’ll need to look at somewhere like this: Using the analog to digital converter (ADC) on Teensy, with C language
and convert each part where you read an analogue input.

What exactly are you trying to convert anyway. As you mention “band gap”, I’m guessing emonLib as that’s the only place we use it.

Ive been running a modified version of emonlib for a while on teensey’s and find them much more stable than arduino based builds. I was trying to adjust the calibration to see if i could improve things farther . Following the sketch in Calibrating the Internal Reference Voltage.it was the calvref sketch from Learn | OpenEnergyMonitor.

Its strange as i can compile on a teensey 2.0 with no issues . looks to be missing references in core_pins.h.

It’s been a while since that was needed! We no longer use the band-gap reference. I’ve never been able to find a specification for its stability, the change to using the regulated VCC supply as a reference was I believe based on the assumption that the long-term stability and the initial value were the same at about ±10% tolerance.

What that sketch does is the same as emonLib does - it reads the voltage of the internal band-gap reference (by switching the multiplexer to read that) using the VCC supply as its reference. You are independently measuring the supply, the sketch then simply works backwards to give you the band-gap voltage in terms of a calibration constant.

What you’ll need to do is look at how you can get the Teensy to read its own internal reference with respect to the reference you’re using for the normal ADC conversions, OR the inverse of that: read the normal ADC reference with respect to the band-gap reference.

Or I presume you could, if you know the initial value and stability of the band-gap voltage and it’s acceptable, use that as the reference voltage always and if necessary adjust the input circuitry to suit. The point I’m making is there’s no need to copy the Atmel 328P way if there’s something better.

[Edit]
Having read a bit more about the Teensy, it would appear that your best solution for accuracy is to make certain you use an accurate and stable regulated power supply and use that, as we do with the emonTx V3.4, and forget the band-gap reference.
The PJRC page says that like the Atmel 328P, the initial value has quite a wide tolerance and no value for stability is specified.