Hi EEVbloggers,
Here's a quick start guide to use the Analog Digital Converter of most ATmega processors.
Some times you just want something to work quickly without having to mess around with the datasheets.
This works on the ATmega32 series, but most of the ATmegas work generally the same.
FirstConnect your pin you want to measure.
In case you don't know how to get a voltage from a sensor:
VCC
_|_
| | Your sensor
|_|
|______To the ADC pin
_|_
| | A resistor that matches approximately the sensors "normal" value
|_|
_|_
_
-
GND
At the ATmega32, the ADCs are located at the pins at PORT A (those are the top right pins).
Then, just put this function in your project.
The codeDatasheet page numbers can be found in
http://www.atmel.com/Images/doc8155.pdfint getADC(int nr/* The zero based ID of the ADC*/)
{
ADMUX = (1 << REFS0) | nr; // For settings see page 225 of the datasheet
ADCSRA = (1 << ADEN /* ADC enable */) | (1 << ADSC /* Start ADC */);
while (ADCSRA & (1 << ADSC)); // Wait till the ADC finished
return ADC;
}
Thanks for the modifications in readability by psycho0815And you're all set!
That really was everything you had to do!
This function will delay until it gets the value from the ADC. So don't blame me when your aircraft crashed while it was waiting for an ADC value.