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 facing to use ADC conversion in ATMEGA16(xtal 12mhz)

problem facing to use ADC conversion in ATMEGA16(xtal 12mhz)

|

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.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • March 1, 2013 at 5:17 am #2147
    Nirav Tejani
    Participant

    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 << 8);*/
    // 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 #9929
    Shrikant
    Participant

    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;

     

    June 10, 2013 at 1:17 pm #9936
    AJISH ALFRED
    Participant

    Hi Nirav,

    Make sure that you’ve connected the reference pin of the ADC to 5V.

    June 11, 2013 at 5:48 am #9940
    Nirav Tejani
    Participant

    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.

     

    June 11, 2013 at 1:19 pm #9947
    AJISH ALFRED
    Participant

    You are welcome. Alll the best for your future projects

  • Author
    Posts
Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Wien bridge oscillator January 19, 2026
  • Silicone insulated wire January 19, 2026
  • Op amp shock January 19, 2026
  • Is the input voltage selectable ? January 19, 2026
  • Unable To Get Advertised Op-Amp Slew Rate January 19, 2026

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2026 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