NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Project Help and Ideas » Question about the temp sensor project

April 08, 2010
by CyberGod
CyberGod's Avatar

In the code for the temp sensor project we set analog to digital converter to be enabled, with a clock prescale of 1/128 so that the ADC clock runs at 115.2kHz. Why exactly 1/128. I see that the register we set this value can accept other values as well. Is this related to how the temperature sensor works ? Also can someone elaborate more on how exactly the formula in the sampleToFahrenheit function works: return sample * (5000.0 / 1024.0 / 10.0);

Thanks!

April 08, 2010
by CyberGod
CyberGod's Avatar

One more thing if I may: can we use PC1 pin instead of PC0 and how does that change things ?

April 08, 2010
by CyberGod
CyberGod's Avatar

About the pin, correct me if I am wrong but if I want to use PC1 I have to change the value of the ADMUX register to 1 according to the data sheet, right ?

April 08, 2010
by CyberGod
CyberGod's Avatar

In regards to the ADC clock running at 115.2kHz or 1/128 (of the main clock?), does that mean it will take 115200 measurements from the temp sensor a second ?

April 08, 2010
by CyberGod
CyberGod's Avatar

The questions just keep popping in my head I apologize if it's too much. Here's one more: how come we don't setup the PC0 explicitly as an input with DDRC &= ~(1<<PC0); How does the controller knows that it will be an input and not an output ?

April 08, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi Cybergod,

Glad you are up and running and you are starting to have good questions. That means learning is happening!

You are exactly right about ADMUX. It can be used to change the input on which the ADC module reads its voltage from.

The clock prescaler too generate the ADC clock frequency 1/128 due to the limitations of the ADC itself. It you take a look at page 248 of the datasheet under prescaling and conversion timing, it mentions that in order to be able to use all 10 bits of precision the ADC must have a clock frequency between 50kHz and 200kHz. 115.2kHz is as fast as we can make it which still being in that range using the crystal we are using.

The 115.2kHz is not how fast the ADC takes measurements, it is just how fast its clock is ticking. Page 249 of the datasheet has timing diagrams that show how long conversions take. Figure 23-5 shows that a normal ADC conversion will take about 13 ADC clock cycles.

The formula for converting the sample to degrees goes like this. You are using a 10-bit ADC. Which means all the analog voltage levels between your 0V of GND and 5V at Vreff are evenly split up amongst all the numbers you can represent with 10-bits. With 10 bits you can represent 1024 numbers. So given a voltage (Vin), the ADC turns that into a number given by the formula

sample = Vin*1024/Vref (also found on page 256 of the datasheet).

But you want to do do the opposite - to figure out the voltage Vin given the sample you get from the ADC, so you turn it around and do

sample*Vref/1024 to get Vin in volts.

Now if we switch over to the LM34 datasheet you see that it is calibrated to output 10mv/degrees F. In order to make the units match up we write in Vreff in millivolts, so 5000mV.

sample*5000mV/1024/10(mV/degF) gives you the result in degrees F.

You don't need to set PC0 explicitly as an input node because unless otherwise noted all registers default to 0. In most cases this default is the safest option. If you just leave the DDRx register untouched its pins will all be input pins and wont be actively driving anything. Sometimes it is good practice to set the register explicitly, if only as a reminder to yourself of where you have the input.

I hope that about covers your questions!

Humberto

April 09, 2010
by CyberGod
CyberGod's Avatar

Thank you for all the answers Humberto ! This is why I love the nerdkit and will recommend it to my colleagues and friends, it's not only the hands on experience you get but the help from the forum members and the stuff members like you. Cheers!

Post a Reply

Please log in to post a reply.

Did you know that you need to think about wires differently when you're transmitting signals more than a few inches? Learn more...