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 with adc conversion code in atmega 16

problem with adc conversion code in atmega 16

|

Microcontroller › AVR › problem with adc conversion code in atmega 16

  • This topic has 2 replies, 3 voices, and was last updated 9 years, 8 months ago by Ashutosh Bhatt.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • August 21, 2011 at 9:23 am #1256
    Shivam
    Participant

    My adc conversion code is as follows…I am giving the analog i/p at pin 39 of atmega16….the adc is used in 10 bit resolution and lcd is in 4 bit mode…I have tested the code on the lcd…It shows me 1023 as the o/p….but the value doesnt change even if i change the analog i/p to pin 39.I think there might be some problems with my adc initialization….Can u please help me?

     

     

     

     

    //ICC-AVR application builder : 7/22/2011 8:16:48 PM
    // Target : M16
    // Crystal: 16.000Mhz

    #include <iom16v.h>
    #include <macros.h>
    #include <lcd.h>

    void port_init(void)
    {
     PORTA = 0x00;
     DDRA  = 0xFD;
     PORTB = 0x00;
     DDRB  = 0x00;
     PORTC = 0x00; //m103 output only
     DDRC  = 0xFF;
     PORTD = 0x00;
     DDRD  = 0x00;
    }

    //ADC initialize
    // Conversion time: 52uS
    void adc_init(void)
    {
     ADCSR = 0x00; //disable adc
     ADMUX = 0x21; //select adc input 1
     ACSR  = 0x80;
     ADCSR = 0x87;
     ADCSRA|=(0x40);
    }

    unsigned int read_adc(unsigned char ADC_channel_no)
    {
      unsigned int j=0,val;
    unsigned char i;
      ADCH=0x00;
      i=ADC_channel_no&0x0F;
      ADMUX=i|0x21;
      ADCSR|=0x40;
      for(j=1;j<1023;j++);
       val=(ADCH*4)+(ADCL/64);
        return(val);
    }

    void DELAY1(unsigned int dela)
    {
        
        int i,k;
        for(k=0;k<dela;k++)
        {

            for(i=0;i<50000;i++)
            {}
            
                
        }

    }
     
     

     

    //call this routine to initialize all peripherals
    void init_devices(void)
    {
     //stop errant interrupts until set up
     CLI(); //disable all interrupts
     port_init();
     adc_init();

    // MCUCR = 0x00;
    // GICR  = 0x00;
    // TIMSK = 0x00; //timer interrupt sources
     SEI(); //re-enable interrupts
     //all peripherals are now initialized
    }

    //
    void main(void)
    {
     unsigned int i;
     init_devices();
     LCD_INIT();

      while(1)
      {       
               i=read_adc(1);
               LCD_WRITE(“vtg=”,4);
               LCD_NO(i);
               DELAY1(5000);
      }    
    }

    December 14, 2016 at 4:39 pm #14272
    GANEEV SINGH
    Participant

    Hi Shivam

    The code thing seems to be fine. One reason could be that you might have not given a proper voltage to Aref pin (pin 32). It is used to provide a reference voltage for A to D conversions, generally we connect this pin to +5 V. 

    This might work :)

    December 22, 2016 at 7:15 pm #14290
    Ashutosh Bhatt
    Participant

    connect AVCC pin and AREF pins of ATMega16 micro controller to +5 V

    try to check if analog voltage at pin 39 actually varies or not. what kind of input you have given to pin 39?

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

RSS feed: Recent Posts Recent Posts

  • Cheap ways to link 5 Keithley units and 4 BNC units? September 8, 2026
  • Marantz SR4500 – CS49400 DSP reverse-engineering / custom active crossover September 8, 2026
  • Sony KV-29VL40 - 4 Blink error - IC001 Pin 17 high September 8, 2026
  • Issue with undervoltage battery protection circuit September 7, 2026
  • Yamaha PX3 amp dry joints? September 7, 2026

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2026 Arrowfly 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 Arrowfly
Privacy Policy | Advertising

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