Microcontroller › AVR › problem facing to use ADC conversion in ATMEGA16(xtal 12mhz)
- This topic has 4 replies, 3 voices, and was last updated 12 years, 7 months ago by
AJISH ALFRED.
-
AuthorPosts
-
March 1, 2013 at 5:17 am #2147
Nirav Tejani
ParticipantHello 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);}
}
June 10, 2013 at 8:10 am #9929Shrikant
Participantam 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;
June 10, 2013 at 1:17 pm #9936AJISH ALFRED
ParticipantHi Nirav,
Make sure that you’ve connected the reference pin of the ADC to 5V.
June 11, 2013 at 5:48 am #9940Nirav Tejani
ParticipantThanks 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.
June 11, 2013 at 1:19 pm #9947AJISH ALFRED
ParticipantYou are welcome. Alll the best for your future projects
-
AuthorPosts
- You must be logged in to reply to this topic.