- This topic has 4 replies, 3 voices, and was last updated 11 years, 4 months ago by .
Viewing 5 posts - 1 through 5 (of 5 total)
Viewing 5 posts - 1 through 5 (of 5 total)
- You must be logged in to reply to this topic.
|
Microcontroller › AVR › problem facing to use ADC conversion in ATMEGA16(xtal 12mhz)
Hello Friends,
I am totally confused by this problem.I am using ADC1 channel to give analog input with stable voltage input.My problem is that I am getting approximately same converted value for different voltage inputs.And yah i am using single conversion mode..Pls check my code…And let me know my problem,,,,,
CODE:
#include<avr/io.h>
#include<stdlib.h>
#define F_CPU 12000000UL
#include<util/delay.h>
//#include”lcd.c”
#define INVERTER_SW PA0
#define POWER_PIN PD0
// ADC initialisation function //
void ADC_init()
{
ADMUX=(1<<REFS1)|(REFS0);
ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);//for prescaler 128
}
//ADC read value function //
uint16_t ADC_read(unsigned char ch)
{
// uint16_t data;
ch=ch&0b00000111; //ch. must be between 0 to 7
ADMUX|=ch; //selecting channel
ADCSRA|=(1<<ADSC); //to start conversion
while(!(ADCSRA && (1<<ADIF)));
//waitnig to complete of conversion
ADCSRA|=(1<<ADIF); //to indicate of end of conversion
/* data=ADCL;
data=data + (ADCH << ;*/
// return (data);
return(ADC);
}
/* <<<<<<<<<<<<<<<<<<<<<<<<<< MAIN >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
int main()
{
DDRD=0x00;
DDRA|=(1<<INVERTER_SW)|(1<<PA3)|(1<<PA2);
// DDRB|=(1<<PB0);
// DDRA=0x7F;
PORTD=0x00;
PORTA=0x00;
// DDRA=0xE0;
// DDRC=0Xff;
ADC_init();
uint16_t value;
// init_LCD();
while(1)
{
//LCD_cmd(0x01);
value=ADC_read(1);
//LCD_cmd(0x80);
//int_lcd(value);
_delay_ms(3000);
if(value<400)
{
PORTA|=(1<<PA3);
}
if(value >630)
{
PORTA|=(1<<INVERTER_SW);
}
if(value>0620)
{
PORTA|=(1<<PA2);
}
_delay_ms(2000);
PORTA&=~(1<<PA3);
PORTA&=~(1<<INVERTER_SW);
PORTA&=~(1<<PA2);
}
}
am not quite sure but just check your adc read function.. i feel that you need to clear the channel used previously..
so instead of ch=ch&0b00001111 try using the following
ADMUX&=0XF0; // basically 11110000 .. this is to clear the previous channels
ADMUX|=ch;
Hi Nirav,
Make sure that you’ve connected the reference pin of the ADC to 5V.
Thanks for your suggetion
Now I am getting proper outputs .The problem is in my hardware.I was not applying proper ground to Adc pin thats the problem.
again thanks for your response.
You are welcome. Alll the best for your future projects