Microcontroller › AVR › problem occurs in getting value from input adc channel in atmega 16
- This topic has 2 replies, 3 voices, and was last updated 12 years ago by
sushant.
-
AuthorPosts
-
February 28, 2013 at 1:40 pm #2144
Nirav Tejani
ParticipantHello,
I am using atmega 16 controller(12MHZ crystal) for ADC convertation.I have used ADC1 channel to get value with INTERNAL REFERENCE VOLTAGE 2.56.I am giving steady input to this channel.Now My Problem is that whenever i changed input voltage to ADC1 channel i m getting a same value for every voltage level..
My code of ADC is below:
#include<avr/io.h>
#include<stdlib.h>
#define F_CPU 12000000UL
#include<util/delay.h>
//#include”lcd.c”
void flag_check();
unsigned char sensor_flag=0;
#define INVERTER_SW PA0
#define POWER_PIN PD0// ADC initialisation function //
void ADC_init()
{
ADMUX=(1<<REFS1)|(REFS0);
//for internal ref.voltage 2.56 and for channel 7
ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);//for prescaler 128
}//ADC read value function //
uint16_t ADC_read(unsigned char ch)
{
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
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);}
}
PLs help me to solve it…….
Thanks,
Nirav Tejani…
March 6, 2013 at 4:42 pm #9271AJISH ALFRED
ParticipantHi Nerav,
Refer this link,
engineersgarage.com/embedded/avr-microcontroller-projects/adc-circuit
March 31, 2013 at 4:20 am #9399sushant
Participantuse folloing library file for ADC communication,
#include<avr/io.h>#include<adc_lib.h>#define ADC_VREF_TYPE 0x00// Read the AD conversion resultunsigned int read_adc(unsigned char adc_input){ADMUX=adc_input|ADC_VREF_TYPE;// Start the AD conversionADCSRA|=0x40;// Wait for the AD conversion to completewhile ((ADCSRA & 0x10)==0);ADCSRA|=0x10;return ADCW;}// header file//#include<avr/io.h>unsigned int read_adc(unsigned char);void ADCinit();void ADCinit(){ADMUX=ADC_VREF_TYPE;ADCSRA=0x85;} -
AuthorPosts
- You must be logged in to reply to this topic.