.

INCLUDES RFID interfacing with avr GPS interfacing with avr RF Module interfacing with avr Stepper Motor With AVR

Saturday, December 17, 2011

How to interface serial ADC0831 with AVR microcontroller (ATmega16)

       ADC is an electronics device that converts the analog signals to digital number proportional to the magnitude of voltage. The ADC chips like ADC0804, ADC0809 etc., give 8-bit digital output. The controller device needs eight pins to receive the 8-bit data (For more details about ADC refer to Using Inbuilt ADC of AVR). Some applications need higher resolution ADCs, (10 or higher bits digital data output) for data accuracy.
       Using parallel ADCs is one option for such applications. However using parallel ADC will increase the size of the hardware as a 10-bit parallel ADC will have 10 output lines. Also you might have to use controller with higher number of pins. The other option is to use serial ADC, which needs smaller number of pins. Since the data is transmitted serially, the data transfer rate of the serial ADC is low as compared to parallel ADC. They can serve as a very good alternative in applications where speed of data transfer in not a critical point. This article explores interfacing of serial ADC0831 with ATmega16.

Circuit description:
The connection of ADC0831 with ATmega16 is shown in the circuit diagram. The output of variable resistor is connected to Vin(+) and Vin(-) pin is grounded. The Pins CS, CLK and DO of ADC are connected to microcontroller.

Programming steps:
1. Send a high to low pulse to CS pin to initialize conversion.
2. Monitor the status of D0 bit until it goes low.
3. Send a clock pulse.
4. Receive the data bits from DO pin of ADC 0831.
5. Store the data bits in a variable by using bitwise operation.
6. When the data byte is received from serial ADC, display it on LCD.



// Program to interface serial ADC 0831 with AVR microcontroller (ATMEGA 16)
#include<avr/io.h>
#include<util/delay.h>
#include<inttypes.h>

#define DO PD2
#define CLK PD1
#define CS PD0

#define lcdport PORTA
#define rs PB0
#define rw PB1
#define en PB2

void lcd_init(void);
void lcdcmd(unsigned char);
void lcddata(unsigned char);
void adc_conversion(unsigned char);
void twi_init();
unsigned char adc_read();

int main()
{
    unsigned char data[12]= "ADC OUTPUT:";

    int i=0;
    unsigned char bits=0;

    DDRA=0xFF;
    DDRB=0x07;
    DDRD=~_BV(DO);    //DO pin is input pin and rest of the pins are output pins
    DDRC=0xFF;
    PORTD=0x07;


    lcd_init();
    while(data[i]!='\0')
    {
        lcddata(data[i]);
        _delay_ms(5);
        i++;
    }
               
    while(1)
    {   
        PORTD|=(1<<CS);            // high-to-low pulse is provided to CS pin
        _delay_ms(1);
        PORTD&=~(1<<CS);
        _delay_ms(1);
                           
        while(PIND & _BV(DO)) // wait until 0 bit is received
        {
            PORTD|=(1<<CLK);
            _delay_ms(1);
            PORTD&=~(1<<CLK);
            _delay_ms(1);
        }
       
        PORTD|=(1<<CLK);        // a clock pusle is provided
        _delay_ms(1);
        PORTD&=~(1<<CLK);
        _delay_ms(1);   
                   
        for(i=0;i<8;i++)       
        {
            PORTD|=(1<<CLK);    // data receive when pulse is high
            _delay_ms(1);
            bits=bits<<1;        //"bits" is variable to store data.left shift operation is perform to make place for upcoming bit

            if(bit_is_set(PIND,DO))    // if 1 is received
            bits |=1;            // bits is increment by 1
           
            PORTD&=~(1<<CLK);        // pulse low
            _delay_ms(1);
        }
       
    adc_conversion(bits);        //
    }
           
   
}

/* this function is written to convert interger value to their corresponding ASCII value*/

void adc_conversion(unsigned char adc_out) 
{
    unsigned int adc_out1;
    int i=0;
    char position=0xC2;

    for(i=0;i<=2;i++)
    {
    adc_out1=adc_out%10;
    adc_out=adc_out/10;
    lcdcmd(position);
    lcddata(48+adc_out1);
    position--;
    }
}

void lcd_init()            // fuction for LCD initialization
{
    lcdcmd(0x38);
    lcdcmd(0x0C);
    lcdcmd(0x01);
    lcdcmd(0x06);
    lcdcmd(0x80);
}

void lcdcmd(unsigned char cmdout)   
{
    lcdport=cmdout;
    PORTB=(0<<rs)|(0<<rw)|(1<<en);
    _delay_ms(10);
    PORTB=(0<<rs)|(0<<rw)|(0<<en);
}


void lcddata(unsigned char dataout)
{
    lcdport=dataout;
    PORTB=(1<<rs)|(0<<rw)|(1<<en);
    _delay_ms(10);
    PORTB=(1<<rs)|(0<<rw)|(0<<en);
}

No comments:

Post a Comment