Thursday 9 June 2016

ADC with AVR



ADC: Analog to digital Converter:




 In ATMEGA16/32, PORTA contains the ADC pins. Some other features of the ADC are as follows:
  • 8 channel implies that there are 8 ADC pins are multiplexed together. You can easily see that these pins are located across PORTA (PA0…PA7).
  • 10 bit resolution implies that there are 2^10 = 1024 steps (as described below).

  • ADC Registers – ADMUX, ADCSRA, ADCH, ADCL. 

1) ADMUX Register:












Program on ADC potentiometer:

First Method:


#include<avr/io.h>
#include<util/delay.h>
#include<stdio.h>
unsigned long int temp;
char res[4];
void datta(unsigned char a)
{
PORTD=a;
PORTB=0x05;
_delay_ms(30);
PORTB=0x01;
}
void command(unsigned char s)
{
PORTD=s;
PORTB=0x04;
_delay_ms(30);
PORTB=0x00;
}
void init()
{
command(0x38);
_delay_ms(100);
command(0x01);
_delay_ms(100);
command(0x0E);
_delay_ms(100);
command(0x80);
_delay_ms(100);
}
void stringg(unsigned char *p)
{
while(*p!='\0')
{
datta(*p);
p++;
}
}
void main()
{
unsigned int x;
ADMUX=0x40;
DDRD=0xFF;
DDRB=0xFF;
init();
while(1)
{
ADCSRA=((1<<ADEN)|(1<<ADSC)|(1<<ADIF));
while((ADCSRA&(1<<ADIF))==0);
temp=ADC;
sprintf(res,"%d",temp/10);
stringg(res);
_delay_ms(1000);
command(0x01);
_delay_ms(100);
}
}


Simulation for  Program:



Second Method:


#include<avr/io.h>
#include<util/delay.h>


void datta(unsigned char a)
{
PORTD=a;
PORTB=0x05;
_delay_ms(30);
PORTB=0x01;
}
void command(unsigned char s)
{
PORTD=s;
PORTB=0x04;
_delay_ms(30);
PORTB=0x00;
}
void init()
{
command(0x38);
_delay_ms(100);
command(0x01);
_delay_ms(100);
command(0x0E);
_delay_ms(100);
command(0x80);
_delay_ms(100);
}
void stringg(unsigned char *p)
{
while(*p!='\0')
{
datta(*p);
p++;
}
}
void num(unsigned int i)
{
int j=1,k,l;
k=i;
while(k>=9)
{
j=j*10;
k=k/10;
}
while(j>=1)
{
l=i/j;
i=i%j;
j=j/10;
datta(l+48);
}
}


void main()
{
unsigned int x;
ADMUX=0x40;
DDRD=0xFF;
DDRB=0xFF;
init();
while(1)
{
ADCSRA=((1<<ADEN)|(1<<ADSC)|(1<<ADIF));
while((ADCSRA&(1<<ADIF))==0);
x=ADC;
num(x);
_delay_ms(1000);
command(0x01);
_delay_ms(100);
}
}

Simulation for  Program:








Program on ADC (temperature Sensor):

First Method:


#include<avr/io.h>
#include<util/delay.h>
#include<stdio.h>
unsigned long int temp;
char res[4];
void datta(unsigned char a)
{
PORTD=a;
PORTB=0x05;
_delay_ms(30);
PORTB=0x01;
}
void command(unsigned char s)
{
PORTD=s;
PORTB=0x04;
_delay_ms(30);
PORTB=0x00;
}
void init()
{
command(0x38);
_delay_ms(100);
command(0x01);
_delay_ms(100);
command(0x0E);
_delay_ms(100);
command(0x80);
_delay_ms(100);
}
void stringg(unsigned char *p)
{
while(*p!='\0')
{
datta(*p);
p++;
}
}
void main()
{
unsigned int x;
ADMUX=0x40;
DDRD=0xFF;
DDRB=0xFF;
init();
while(1)
{
ADCSRA=((1<<ADEN)|(1<<ADSC)|(1<<ADIF));
while((ADCSRA&(1<<ADIF))==0);
temp=ADC;
sprintf(res,"%d",temp/2);
stringg(res);
_delay_ms(1000);
command(0x01);
_delay_ms(100);
}

}


Simulation for  Program:




Second Method:


#include<avr/io.h>
#include<util/delay.h>
#include"lcd_header.h"
void datta(unsigned char a)
{
PORTD=a;
PORTB=0x05;
_delay_ms(30);
PORTB=0x01;
}
void command(unsigned char s)
{
PORTD=s;
PORTB=0x04;
_delay_ms(30);
PORTB=0x00;
}
void init()
{
command(0x38);
_delay_ms(100);
command(0x01);
_delay_ms(100);
command(0x0E);
_delay_ms(100);
command(0x80);
_delay_ms(100);
}
void stringg(unsigned char *p)
{
while(*p!='\0')
{
datta(*p);
p++;
}
}
void num(unsigned int i)
{
int j=1,k,l;
k=i;
while(k>=9)
{
j=j*10;
k=k/10;
}
while(j>=1)
{
l=i/j;
i=i%j;
j=j/10;
datta(l+48);
}
}
void main()
{
unsigned int x;
ADMUX=0x40;
DDRD=0xFF;
DDRB=0xFF;
init();
while(1)
{
ADCSRA=0xE7;
while((ADCSRA&(1<<ADIF))==0);
x=ADC/2;
num(x);
_delay_ms(1000);
command(0x01);
_delay_ms(100);
}
}


Simulation for  Program:



0 comments:

Post a Comment

if you have any doubt please let me know