Don't ad-block us - support your favorite websites. We have safe, unobstrusive, robotics related ads that you actually want to see - see here for more.
0 Members and 1 Guest are viewing this topic.
/*! \file a2d.c \brief Analog-to-Digital converter function library. *///*****************************************************************************//// File Name : 'a2d.c'// Title : Analog-to-digital converter functions// Author : Lefteris Provatos - Copyright (C) 2004 - 2010// Created : 2010-05-20// Revised : // Version : 1.0// Target MCU : Atmel AVR series// Editor Tabs : 4//// This code is distributed under the GNU Public License// which can be found at http://www.gnu.org/licenses/gpl.txt////*****************************************************************************// defines// A2D clock prescaler select// *selects how much the CPU clock frequency is divided// to create the A2D clock frequency// *lower division ratios make conversion go faster// *higher division ratios make conversions more accurate#define ADC_PRESCALE_DIV2 0x00 ///< 0x01,0x00 -> CPU clk/2#define ADC_PRESCALE_DIV4 0x02 ///< 0x02 -> CPU clk/4#define ADC_PRESCALE_DIV8 0x03 ///< 0x03 -> CPU clk/8#define ADC_PRESCALE_DIV16 0x04 ///< 0x04 -> CPU clk/16#define ADC_PRESCALE_DIV32 0x05 ///< 0x05 -> CPU clk/32#define ADC_PRESCALE_DIV64 0x06 ///< 0x06 -> CPU clk/64#define ADC_PRESCALE_DIV128 0x07 ///< 0x07 -> CPU clk/128// default value#define ADC_PRESCALE ADC_PRESCALE_DIV128// do not change the mask value#define ADC_PRESCALE_MASK 0x07// A2D voltage reference select// *this determines what is used as the// full-scale voltage point for A2D conversions#define ADC_REFERENCE_AREF 0x00 ///< 0x00 -> AREF pin, internal VREF turned off#define ADC_REFERENCE_AVCC 0x01 ///< 0x01 -> AVCC pin, internal VREF turned off#define ADC_REFERENCE_RSVD 0x02 ///< 0x02 -> Reserved#define ADC_REFERENCE_256V 0x03 ///< 0x03 -> Internal 2.56V VREF// default value#define ADC_REFERENCE ADC_REFERENCE_AREF// do not change the mask value#define ADC_REFERENCE_MASK 0xC0// bit mask for A2D channel multiplexer#define ADC_MUX_MASK 0x1F// ADLAR settings#define ADC_DATA_RIGHT_ADJ 0x00#define ADC_DATA_LEFT_ADJ 0x20// Set the library to wait for the// ADC to complete convertion or not#define WAITING_EN//*****************************************************************************// turn off a2d convertervoid adcOff(void){ cbi(ADCSRA, ADIE); // disable ADC interrupts cbi(ADCSRA, ADEN); // disable ADC (turn off ADC power)}// Setting the Prescallervoid setadcPresc(u08 Presc){ ADCSRA &= ~ADC_PRESCALE_MASK; ADCSRA |= (Presc & ADC_PRESCALE_MASK);}//Setting the Voltage Referencevoid setadcVolta(u08 voltR){ ADMUX &= ~ADC_REFERENCE_MASK; ADMUX |= (voltR & ADC_REFERENCE_MASK);}void setadcdatao(u08 direction){// If told set the data to left adjustif (direction == ADC_DATA_LEFT_ADJ){ ADMUX |= ADC_DATA_LEFT_ADJ;}// Else set the data to right adjustelse{ ADMUX &= ~ADC_DATA_LEFT_ADJ;}} // Initialize ADCvoid adcInit (void){// Enable the ADC converterADCSRA |= 0x80;// Ensure that we have the single convertion optionADCSRA &= ~0x20;// Set ADC default Prescaller setadcPresc(ADC_PRESCALE);// Set ADC reference VoltagesetadcVolta(ADC_REFERENCE);// Ensure data are right adjusted//setadcdatao(ADC_DATA_RIGHT_ADJ);}// Get ADC channel valueu16 Getadc(u08 ch){u16 temp;// Set channel for convertionif (ch > 7){ ch -= 7; // If the microcontroller has more channels // then it should have ADCSRB register // If not this offers compatibility with other // microcontrollers #ifdef ADCSRB ADCSRB |= 0x08; #endif} // Set ADCMUXADMUX = ((ADMUX & ~ADC_MUX_MASK) | (ch & ADC_MUX_MASK));sbi(ADCSRA, ADIF); // clear hardware "conversion complete" flag sbi(ADCSRA, ADSC); // start conversion// if you define the library to wait for the convertion to completewhile( ADCSRA & 0x40 );// if the program has indeed waited then it should have // cleared the ADSC so return the data else// return to the program and wait for the data theretemp = ADCL;temp |= (ADCH<<8);return temp; // read ADC (full 10 bits);}
A basic tutorial on ADC and how it is configured and the code.
[...] and 8 bit ADC will be resolution 2^8 = 1024
Hi,Quote from: Actives on May 21, 2010, 12:54:37 AM[...] and 8 bit ADC will be resolution 2^8 = 10242^8 used to be 256 when I was young... But everything seems to be rising these days