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 » Understanding Registers

November 19, 2010
by Proton
Proton's Avatar

I have learnt to program in C. I have been able to connect USB to unit and write code to it. I have tested the provided tempsensor program with the kit. It works fine. I am now trying to learn and understand the program and the different parts of the MCU.

Can someone explain what these registers do in simple terms, their purpose, what the individual bits in them do, and why we set them the way we do? Ex: How do we know to set bits in ADCSRA to 1? Why?

ADMUX, ADCSRA, ADEN

I understand that this is part of initializing the ADC, but I want some deeper knowledge, and the data sheet is too complex for me to understand.

Regards, Proton

November 19, 2010
by bretm
bretm's Avatar

The datasheet is complex, but the sections describing the register bits are often the most straightforward. The ADC registers are a good example.

Turn your data sheet to page 257 please. Section 23.8.2 has exactly what you need. ADCSRA is the ADC Control and Status Register A. ADEN is the ADC Enable bit. ADSC is the ADC Start Conversion bit. ADMUX is a different register described in the section just before that. It's the ADC Multiplexer Selection Register.

See if those two or three pages make sense and ask again here if they don't.

November 20, 2010
by Proton
Proton's Avatar

Thank you very much for your help. Any good link to explain how the ADC works? Pages 257 onwards were very helpful. Not exactly sure how I missed them =D

November 20, 2010
by bretm
bretm's Avatar

The registers secrtion comes at the end of each chapter. The explanation of how the ADC works is earlier in the datasheet, on the preceding pages.

Basically how it works is that it samples the input voltage for a few ticks of the ADC clock, charging a small internal capacitor to the same voltage. The for each of the next ten ticks it determines one bit of the digital result. It does this by setting each bit to 1 starting with the MSB, converting the binary value to a voltage using a DAC, and if this voltage is greater than the sample voltage the bit is cleared before moving on to the next bit.

November 20, 2010
by Proton
Proton's Avatar

LM34 output is in mV and directly proportional to temp? attempting to understand, is this then provided as input to ADC, which converts it to binary 10-bit number?

I understand each 10 mV change = 1 degree F as output from the temp sensor

please explain why the +5V is divided by 1024 in sampleToFahrenheit();

November 20, 2010
by bretm
bretm's Avatar

A ten bit number ranges from 0 to 1023, or a total of 1024 increments. 0 corresponds to 0V and 1024 corresponds to AREF which in this case is 5V. So the input voltage is the ADC value times 5.0 and divided by 1024.

November 21, 2010
by Proton
Proton's Avatar

thanks. that helps a lot. this site also helped a 13 year old understand ADC =D

http://www.allaboutcircuits.com/vol_4/chpt_13/10.html

another question

is it possible to write the data from the ATmega168 tempsensor project to a file on the computer, instead of just being displayed on screen? If so, how is it done?

November 21, 2010
by bretm
bretm's Avatar

That's a good article. A couple of points of clarification:

  1. They indicate that you should divide by 1023 since in their example 1023 corresponds to a water level of 40. It's more common (with ADCs that use an internal DAC to compare the digital estimate with the input value) for the 1024 value to correspond to the maximum, not 1023, and you should divide by 1024. The Atmega datasheet shows this on page 261 (if you solve for Vin). The most-significant bit contributes 2.5V, the second bit contributes 1.25V, the third bit contributes 0.625V, etc. (half the voltage each time). If all ten bits are set it adds up to just under 5V.

  2. They make the statement about the sampling rate:

It should be understood that the Nyquist frequency is an absolute maximum frequency limit for an ADC, and does not represent the highest practical frequency measurable. To be safe, one shouldn't expect an ADC to successfully resolve any frequency greater than one-fifth to one-tenth of its sample frequency.

But they don't explain why you can't go all the way up to half the sample frequency. In a perfect mathematical world with a noise-free signal and an infinite supply of digital samples, you can perfectly reconstruct the input signal. But the real world has noisy signals and you can only process a finite number of samples, and the calculations have rounding errors, so in practice you should sample more than twice as fast as the bandwidth of the input signal. For a slowly-changing signal like air temperature it's not really an issue.

November 21, 2010
by bretm
bretm's Avatar

You can make it store the data on the computer. The program on the microcontroller already writes the temperature data to the serial port. You just need a program on the computer to read the values from the serial port and store them. You can do this manually by just saving the contents of the terminal window if you're using a terminal program. Otherwise you'll have to write a program for the computer.

Post a Reply

Please log in to post a reply.

Did you know that signed numbers need to be sign-extended when chaging variable sizes? Learn more...