ADCinAVR

Embed Size (px)

Citation preview

  • 7/29/2019 ADCinAVR

    1/10

    N

    Dev

    Kri

    loped

    hna N

    rasha

    May

    y:

    nd Gu

    t Agra

    r Agar

    ta

    al

    al

  • 7/29/2019 ADCinAVR

    2/10

    ~ 1 ~

    WhatisanADC?An ADC, or Analog to Digital Converter, allows one to convert an analog voltage to a digital

    value that can be used by a microcontroller. There are many sources of analog signals that

    one might like to measure. There are analog sensors available that measure temperature,

    light intensity, distance, position, and force, just to name a few.

    IntroductionTheAVRADCThe AVR ADC allows the AVR microcontroller to convert analog voltages to digital values

    with few to no external parts. The ATmega8 features a 10-bit successive approximation

    ADC.ATmega8 has 7 channel ADC at PortC. The ADC has a separate analog supply voltagepin, AVCC. AVCC must not differ more than 0.3V from VCC.. The voltage reference

    may be externally decoupled at the AREF pin. AVCC is used as the voltage reference. The

    ADC can also be set to run continuously (the free-running mode) or to do only one

    conversion.ADC Conversion Result After the conversion is complete (ADIF is high), the conversion

    result can be found in the ADC Result Registers (ADCL, ADCH).

    Where VIN is the voltage on the selected input pin and VREF the selected voltage reference

    HowtoconfigureADCinATmega8?ThefollowingRegistersareusedforimplementationofADCinATmega8

    ADC Mul tiplexer Selec tion

    Register ADMUX

    Bit 7:6 REFS1:0: Reference Selection Bi tsThese bits select the voltage reference for the ADC, as shown below

  • 7/29/2019 ADCinAVR

    3/10

    ~ 2 ~

    Bit 5 ADLAR: ADC Left Adjust Result

    The ADLAR bit affects the presentation of the ADC conversion result in the ADC Data

    Register. Write one to ADLAR to left adjust the result. Otherwise, the result is right adjusted.

    ADLAR = 0

    ADLAR = 1

    When an ADC conversion is complete, the result is found in ADCH and ADCL When ADCL is

    read, the ADC Data Register is not updated until ADCH is read. Consequently, if the result isleft adjusted and no more than 8-bit precision is required, it is sufficient to read ADCH.

    Otherwise, ADCL must be read first, then ADCH.Bits 3:0 MUX3:0: Analog Channel Selection Bits

    The value of these bits selects which analog inputs are connected to the ADC.

    ADC Control and Status

    Register A ADCSRA

  • 7/29/2019 ADCinAVR

    4/10

    ~ 3 ~

    Bit 7 ADEN: ADC EnableWriting this bit to one enables the ADC. By writing it to zero, the ADC is turned off

    Bit 6 ADSC: ADC Start ConversionIn Single Conversion mode, write this bit to one to start each conversion. In Free Running mode, write thisbit to one to start the first conversion.

    Bit 5 ADFR: ADC Free Running SelectWhen this bit is set (one) the ADC operates in Free Running mode. In this mode, the ADC samples andupdates the Data Registers continuously. Clearing this bit (zero) will Terminate Free Running mode.

    Bit 4 ADIF: ADC Interrupt FlagThis bit is set when an ADC conversion completes and the Data Registers are updated. The ADCConversion Complete Interrupt is executed if the ADIE bit and the I-bit in SREG are set. ADIF is clearedby hardware when executing the corresponding interrupt Handling Vector. Alternatively, ADIF is clearedby writing a logical one to the flag.

    Bit 3 ADIE: ADC Interrupt EnableWhen this bit is written to one and the I-bit in SREG is set, the ADC Conversion CompleteInterrupt is activated.

    Bits 2:0 ADPS2:0: ADC Prescaler Select Bi ts

    Accordingtothedatasheet,thisprescalarneedstobesetsothattheADCinputfrequencyisbetween50KHzand200KHz.TheADCclockisderivedfromthesystemclockwithhelpofADPS2:0These bitsdetermine the division factor between the XTAL frequency and the input clock to the ADC.

    Lets take a simple example of switching on a LED when input voltage at ADC pin is above

    2.5 volt

    Code:Set threshold valueConfigure output LED pinConfigure ADC HardwareEnable ADCStart A2D Conversions

  • 7/29/2019 ADCinAVR

    5/10

    ~ 4 ~

    WHILE Forever

    IF ADC Value Higher then threshold, Turn on LEDELSE Turn on LED

    END WHILETo simplify this example, we will set up the ADC to continuously measure the voltage

    on ADC5. We will then poll the value in an endless loop and change the LED status

    as we need to. The skeleton code for our example would then be

    Code:#include int main (void){

    uint8_t thresholdValue =128;

    DDRB|=(1

  • 7/29/2019 ADCinAVR

    6/10

    ~ 5 ~

    To set the channel passed through the multiplexer to the ADC, the MUX bits in the ADMUX

    register need to be set accordingly. Since we are using ADC5 here

    Code:ADMUX&=0xF0;ADMUX|=5;

    In order to put the ADC into free-running mode, set the aptly-named ADFR bit in the

    ADCSRA register:

    Code:ADCSRA |=(1

  • 7/29/2019 ADCinAVR

    7/10

    ~ 6 ~

    Code:#include int main (void){uint8_t thresholdValue =128;

    DDRB |=(1

  • 7/29/2019 ADCinAVR

    8/10

    ~ 7 ~

    Code:#include int main (void){uint8_t thresholdValue =128;

    DDRB |=(1

  • 7/29/2019 ADCinAVR

    9/10

    ~ 8 ~

    pieces of hardware within the microcontroller can signal that a certain task has been

    completed. Normal program execution can be interrupted when one of these signals (the

    interrupt) is asserted. Depending on the interrupt signal, different user-defined programs,

    called interrupt service routines or

    ISRs, can be run. After the ISR completes, normal program execution resumes.

    The AVR ADC has an interrupt associated with it that is asserted when an A2D conversion

    completes. There are several changes that need to make to the first example to utilize this

    interrupt. First, lets Write the ISR that will be run when the interrupt is asserted.

    The first step in using interrupts in our application is to add the standard library header

    avr/interrupt.h. This file defines functions and macros needed to utilize interrupts on the

    AVR. The following line should be added below the io.h define in our original program.

    Code:#include

    Next, we'll define the ISR itself. To do this, we need the name of the interrupt we are connectingto the ISR. Referring to the datasheet, we find the name of the interrupt we want to use ADC,which is asserted when an A2C conversion completes. Here is the proper format for an ISR usingthe ADC interrupt:Code:ISR(ADC_vect){

    // Code to be executed when ISR fires}

    Now, we place the IF statement originally in the infinite loop inside the ISR, so it will only be

    run when the ADC interrupt indicates a conversion has been completed:Code:ISR(ADC_vect){if(ADCH >thresholdValue)

    {PORTB |=(1

  • 7/29/2019 ADCinAVR

    10/10

    ~ 9 ~

    Code:ADCSRA |=(1