Line 14: Line 14:
 
We will first initialize the ADC with the following code
 
We will first initialize the ADC with the following code
 
# Enable ADC by Setting ADEN bit of ADCSRA and set the sampling frequency to half of oscillator frequency (i.e 8 MHz)
 
# Enable ADC by Setting ADEN bit of ADCSRA and set the sampling frequency to half of oscillator frequency (i.e 8 MHz)
# Select channel zero by default.
+
# Select channel zero by default using the ADMUX register shown below.
 
+
<syntaxhighlight>
+
void ADC_Init()
+
{
+
  ADCSRA=(1<<ADEN) | (1<<ADPS0); /* Enable ADC , sampling freq=osc_freq/2 */
+
  ADMUX=0x00;                    /* Result right justified, select channel zero */
+
}
+
</syntaxhighlight>
+
Let us write another function to select the required channel and read the analog data as below .  
+
 
+
 
+
 
+
 
'''ADC Multiplexer Select (ADMUX)''' <br/>
 
'''ADC Multiplexer Select (ADMUX)''' <br/>
  
Line 37: Line 25:
 
|0||0||0||0||0||0||0||0
 
|0||0||0||0||0||0||0||0
 
|}
 
|}
 +
 +
 +
<syntaxhighlight>
 +
void ADC_Init()
 +
{
 +
  ADCSRA=(1<<ADEN) | (1<<ADPS0); /* Enable ADC , sampling freq=osc_freq/2 */
 +
  ADMUX=0x00;                    /* Result right justified, select channel zero */
 +
}
 +
</syntaxhighlight>
 +
Let us write another function to select the required channel and read the analog data as below :
 +
# Select the required channel passed as input to the function
 +
# Start the ADC conversion by setting ADSC bit in ADCSRA
 +
# Wait till the conversion is over by checking the ADIF bit in ADCSRA
 +
# Read the ADCW register and return the result of conversion
 +
<syntaxhighlight>
 +
uint16_t ADC_GetAdcValue(uint8_t v_adcChannel_u8)
 +
{
 +
 
 +
  ADMUX = v_adcChannel_u8;              /* Select the required channel */
 +
  DELAY_us(10);                          /* Wait for some time for the channel to get selected */
 +
  util_BitSet(ADCSRA,ADSC);              /* Start the ADC conversion by setting ADSC bit */
 +
 
 +
  while(util_IsBitCleared(ADCSRA,ADIF)); /* Wait till the conversion is over */
 +
                                          /* ADIF will be set once ADC conversion is complete */
 +
    return(ADCW);                        /* Return the 10-bit result */
 +
}
 +
</syntaxhighlight>
  
 
=Hook up=
 
=Hook up=

Revision as of 18:13, 25 March 2016

The Atmega32 comes with a 10-bit, 8 channel inbuilt Ananlog to Digital Convertor (ADC). We will set it up and read temperature from LM35 and light with a simple (light dependent Resistor)LDR. So let's get started!

Basics

The ACSRA register allows us to enable the adc and set the sampling rate. ADC Control and Status Register A (ACSRA):

7 6 5 4 3 2 1 0
ADEN ADSC ADATE ADIF ADIE ADPS2 ADPS1 ADPS0
1 0 0 0 0 0 0 1

We will first initialize the ADC with the following code

  1. Enable ADC by Setting ADEN bit of ADCSRA and set the sampling frequency to half of oscillator frequency (i.e 8 MHz)
  2. Select channel zero by default using the ADMUX register shown below.

ADC Multiplexer Select (ADMUX)

7 6 5 4 3 2 1 0
REFS1 REFS0 ADLAR MUX4 MUX3 MUX2 MUX1 MUX0
0 0 0 0 0 0 0 0


void ADC_Init()
 {
   ADCSRA=(1<<ADEN) | (1<<ADPS0); /* Enable ADC , sampling freq=osc_freq/2 */
   ADMUX=0x00;                    /* Result right justified, select channel zero */
 }

Let us write another function to select the required channel and read the analog data as below :

  1. Select the required channel passed as input to the function
  2. Start the ADC conversion by setting ADSC bit in ADCSRA
  3. Wait till the conversion is over by checking the ADIF bit in ADCSRA
  4. Read the ADCW register and return the result of conversion
uint16_t ADC_GetAdcValue(uint8_t v_adcChannel_u8)
 {
 
   ADMUX = v_adcChannel_u8;               /* Select the required channel */
   DELAY_us(10);                          /* Wait for some time for the channel to get selected */
   util_BitSet(ADCSRA,ADSC);              /* Start the ADC conversion by setting ADSC bit */
 
   while(util_IsBitCleared(ADCSRA,ADIF)); /* Wait till the conversion is over */
                                          /* ADIF will be set once ADC conversion is complete */
     return(ADCW);                        /* Return the 10-bit result */
 }

Hook up