PDA

View Full Version : Temperature on analogRead(38)



mbustosorg
01-22-2013, 02:44 AM
Hi,

I'm having trouble getting a read from the on chip temp sensor. Is there any additional setup that has to be performed? I can't find anything in the datasheet or accompanying app note.

Thanks,
mauricio

Jp3141
01-22-2013, 01:33 PM
Isn't it channel 26 (decimal) ? I think you interpreted 26 as hex and converted to decimal (-> 38) ?
Also, the result is not directly in degrees -- you have to calibrate each part, but this is start:

temperatureC = 432 - analogRead(temperatureChannel)*0.02936;

mbustosorg
01-23-2013, 05:05 AM
Thanks for the response. I see the channel 26 but I also see 38 getting mapped to 26 in analog.c. What I think the problem was is that I wasn't properly calibrating. Working now!

Thanks again,
mauricio



static const uint8_t channel2sc1a[] = {
5, 14, 8, 9, 13, 12, 6, 7, 15, 4,
0, 19, 3, 21, 26, 22
};

int analogRead(uint8_t pin)
{
int result;

if (pin >= 14) {
if (pin <= 23) {
pin -= 14; // 14-23 are A0-A9
} else if (pin >= 34 && pin <= 39) {
pin -= 24; // 34-37 are A10-A13, 38 is temp sensor, 39 is vref
} else {
return 0; // all others are invalid
}
}
//serial_print("analogRead");
//return 0;
if (calibrating) wait_for_cal();
//pin = 5; // PTD1/SE5b, pin 14, analog 0

ADC0_SC1A = channel2sc1a[pin];
while ((ADC0_SC1A & ADC_SC1_COCO) == 0) {
yield();
// wait
//serial_print(".");
}
//serial_print("\n");
result = ADC0_RA >> analog_right_shift;
//serial_phex16(result >> 3);
//serial_print("\n");
return result;
}

Jp3141
01-24-2013, 05:56 AM
It's physically channel 26 (dec) in the MCU; Paul's code maps this from channel 38 in an analogRead() call. It's just a (deliberate) coincidence that 0x26 = 38 decimal, so analogRead(0x26) actually works correctly !

PaulStoffregen
01-25-2013, 12:08 AM
I just tried it with this....



void setup() {
analogReference(INTERNAL);
analogReadResolution(12);
analogReadAveraging(32);
}

void loop() {
Serial.println(analogRead(38));
delay(10);
}


I'm running this right now, and it's printing 2391 and 2392. When I touched the edge of a cold bottle of water to the top of the chip, it went up to 2411 & 2412. When I touch the top of the chip with my soldering iron (in the center, not near any pins), it went down rapidly... somewhere around 2050 before I took the iron way after several seconds.

Of course this is an extremely imprecise test, but hopefully it helps?

Jp3141
01-25-2013, 12:33 AM
Internally, the temperature sensor is just a diode (~ 0.7V at room temperature), and this falls at about -1.8 mV/C. With 12 bits, a code of 2400 represents 2400/4096 * 1.2, or 0.7 V -- as you find (1.2 is approximately the internal reference value).

Each ADC code represents 1/4096*1.2 = 0.3 mV, or about 1/6 of a degree.

Freescale doesn't guarantee the room temperature value or temperature sensitivity, but it is quite easy to get << 1 degree resolution from this.