- This topic has 2 replies, 3 voices, and was last updated 8 years, 1 month ago by .
Viewing 3 posts - 1 through 3 (of 3 total)
Viewing 3 posts - 1 through 3 (of 3 total)
- You must be logged in to reply to this topic.
|
Microcontroller › AVR › problem with adc conversion code in atmega 16
My adc conversion code is as follows…I am giving the analog i/p at pin 39 of atmega16….the adc is used in 10 bit resolution and lcd is in 4 bit mode…I have tested the code on the lcd…It shows me 1023 as the o/p….but the value doesnt change even if i change the analog i/p to pin 39.I think there might be some problems with my adc initialization….Can u please help me?
//ICC-AVR application builder : 7/22/2011 8:16:48 PM
// Target : M16
// Crystal: 16.000Mhz
#include <iom16v.h>
#include <macros.h>
#include <lcd.h>
void port_init(void)
{
PORTA = 0x00;
DDRA = 0xFD;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0xFF;
PORTD = 0x00;
DDRD = 0x00;
}
//ADC initialize
// Conversion time: 52uS
void adc_init(void)
{
ADCSR = 0x00; //disable adc
ADMUX = 0x21; //select adc input 1
ACSR = 0x80;
ADCSR = 0x87;
ADCSRA|=(0x40);
}
unsigned int read_adc(unsigned char ADC_channel_no)
{
unsigned int j=0,val;
unsigned char i;
ADCH=0x00;
i=ADC_channel_no&0x0F;
ADMUX=i|0x21;
ADCSR|=0x40;
for(j=1;j<1023;j++);
val=(ADCH*4)+(ADCL/64);
return(val);
}
void DELAY1(unsigned int dela)
{
int i,k;
for(k=0;k<dela;k++)
{
for(i=0;i<50000;i++)
{}
}
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
adc_init();
// MCUCR = 0x00;
// GICR = 0x00;
// TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//
void main(void)
{
unsigned int i;
init_devices();
LCD_INIT();
while(1)
{
i=read_adc(1);
LCD_WRITE(“vtg=”,4);
LCD_NO(i);
DELAY1(5000);
}
}
Hi Shivam
The code thing seems to be fine. One reason could be that you might have not given a proper voltage to Aref pin (pin 32). It is used to provide a reference voltage for A to D conversions, generally we connect this pin to +5 V.
This might work
connect AVCC pin and AREF pins of ATMega16 micro controller to +5 V
try to check if analog voltage at pin 39 actually varies or not. what kind of input you have given to pin 39?