Search This Blog

Thursday, March 18, 2010

Read ADC voltage from 0V to 5V on PORTA PIN 0, ATMEGA16, CODEVISION

SET PORTA AS INPUT
-------------------------------

#define ADC_VREF_TYPE 0x20

--------------------------------------------


unsigned char read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCH;
}

// Analog Comparator initialization
// Analog Comparator:
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0xA6;
SFIOR&=0x1F;
--------------------------------

-----------------------------------------------------
TO READ
unsigned char adc_volt;
//in while loop
adc_volt=read_adc(0);

No comments:

Post a Comment