Sunday, July 24, 2011

Analog to digital conersion........(ADC)

Today i am going to tell u about analog to digital converter....
First of all before knowing the ckt and programming we should know what is ADC ?.
When u require to read the sensors .The reading what u get from sensors is analog .But controller work on digital.So we convert the analog values to the digital using our controller.
                  There is inbuilt ADC converter ports in Microcontroller.For example Atmega 8 has PORTC as ADC port , while Atmega 16 has PORTA as ADC port.
The code is as follows .....

#define F_CPU 1000000

#include <avr/io.h>
#include <util/delay.h>
#include "lcd4.h"      // link for lcd4.h--http://harry-nita.blogspot.com/2011/07/above-is-ckt-diagram.html

void delayms(uint16_t millis) {
  while ( millis ) {
    _delay_ms(1);
    millis--;
  }
}
// This is initialisation of ADC
void adc_init(void){
  
   ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // Set ADC prescalar to 128 - 125KHz sample rate @ 16MHz

   ADMUX |= (1 << REFS0); // Set ADC reference to AVCC
   ADMUX |= (1 << ADLAR); // Left adjust ADC result to allow easy 8 bit reading

   // No MUX values needed to be changed to use ADC0

   //ADCSRA |= (1 << ADFR);  // Set ADC to Free-Running Mode
   ADCSRA |= (1 << ADEN);  // Enable ADC
   ADCSRA |= (1 << ADSC);  // Start A2D Conversions
}
unsigned char adc_read(unsigned char ch){

    ADMUX &= 0xF8; // clear bottom 3 bits
    ADMUX |= ch; // then set bottom 3 bits to channel n
    ADCSRA |= (1 << ADSC);  // Start A2D Conversions
    while(bit_is_set(ADCSRA,ADSC));
   
    return(ADCH);
}
void puthex( unsigned char data ){

  unsigned char ascii[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  SendData(ascii[(data >> 4)]);
  SendData(ascii[data & 0x0F]);
}

int main (void)
{
  char str[] = "ROGER FEDERER";
  unsigned char tmp,i = 0;
  adc_init();
  InitLCD();
  SendString(str, 1, 0);// plain string
  delayms(1000);
  SendCommand(0x80);
  while(1){
        tmp = adc_read(i);             // here it reads the i th port of ADC port.
      SendData(i + 0x30);           
        SendData(':');
      puthex(tmp);                       // this will display the HEX equivalent of analog value.
    SendData(' ');
   
    i++;
    if(i > 5){
          i = 0;
          //SendCommand(0x01);
          SendCommand(0x80); 
    }
    if(i == 3)
          SendCommand(0xC0);
    delayms(1000);
  }
}

2 comments:

  1. Where Can I get the LCD4.h Header File ?

    ReplyDelete
    Replies
    1. Please refer the below link you will get the lcd header file..

      http://harry-nita.blogspot.in/2011/07/above-is-ckt-diagram.html

      Delete