EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / problem occurs in getting value from input adc channel in atmega 16

problem occurs in getting value from input adc channel in atmega 16

|

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, 8 months ago by sushant.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • February 28, 2013 at 1:40 pm #2144
    Nirav Tejani
    Participant

    Hello,

             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 #9271
    AJISH ALFRED
    Participant

    Hi Nerav,

    Refer this link,

    engineersgarage.com/embedded/avr-microcontroller-projects/adc-circuit

    March 31, 2013 at 4:20 am #9399
    sushant
    Participant

    use folloing library file for ADC communication, 

     

     
    #include<avr/io.h>
    #include<adc_lib.h>
     
     
     
    #define ADC_VREF_TYPE 0x00
     
    // Read the AD conversion result
    unsigned int read_adc(unsigned char adc_input)
    {
    ADMUX=adc_input|ADC_VREF_TYPE;
    // Start the AD conversion
    ADCSRA|=0x40;
    // Wait for the AD conversion to complete
    while ((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;
    }
  • Author
    Posts
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • mechanism to shutdown feeding when sensor temperature rises December 13, 2025
  • Oshonsoft MSSP simulation question December 13, 2025
  • Droplet1 December 13, 2025
  • What is involved to convert a small town to fiber optic? December 13, 2025
  • Measuring controller current output with a meter December 13, 2025

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise