/*
 * Copyright(C) Paul und Scherer (mct.de/mct.net)
 *
 * This example demonstrates how to...
 *
 *  ... use the ADC as die thermometer.
 */

#include <stdio.h>
#include <target.h>
#include "delay.h"

/*
 * One ADC digit equals Vref (=2500mV)/4096.
 * The ADC value x represents x*2500mV/4096.
 *
 * At 25 degree Celsius, 780mV are delivered
 * from the temperature sensor. With delta U
 * = -1.3mV/degree, the temperature is:
 *
 *  T = U[mV]*-10/13[degree Celsius/mV]
 *              +625[degree Celsius]
 *
 *  where U is x*2500mV/4096.
 */
#define T(x)	((long)(x)*2500*-10/4096/13+625)

int
main(void)
{
	/*
	 * Reference voltage
	 *
	 * Setting refcon to 1 activates the internal
	 * reference and connects it to the Vref pin.
	 *
	 * Note: A 0.47uF capacitor is required (from
	 * Vref to GND) for proper operation!
	 */
	Intern_refcon = 1;

	Intern_adccp  = 0x10;			// select temp. sensor
	Intern_adccon = 0xa4;			// continuous conversion

	while (1) {
		delay(1000);			// pause to smooth output
		while (!Intern_adcsta) ;	// wait for result ready

		printf("Tdie: %ld [degree Celsius]\n", T((Intern_adcdat>>16)&0xfff));
	}
}
