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 / reading adc from atmega8

reading adc from atmega8

|

Microcontroller › AVR › reading adc from atmega8

  • This topic has 2 replies, 2 voices, and was last updated 10 years, 2 months ago by Arjun Singh.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • April 4, 2015 at 7:03 am #3603
    gavi prakash
    Participant

    Hi to all,

    I am reading adc values using atmega8 controller, the problem is it is always reading 1023 if im changing valus through pot. the code is lik below

     

     #include <avr/io.h>

    #include <util/delay.h>
     #include "lcd.h"
      
    void InitADC()
    {
    ADMUX=(1<<REFS0);                         // For Aref=AVcc;
    ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); //Rrescalar div factor =128
    }
     
    uint16_t ReadADC(uint8_t ch)
    {
       //Select ADC Channel ch must be 0-7
       ch=ch&0b00000111;
       ADMUX|=ch;
       
     ADCSRA |= (1<<ADSC);
     
        // wait for conversion to complete
        // ADSC becomes '0' again
        // till then, run loop continuously
        while(ADCSRA & (1<<ADSC));
     
        return (ADC);
    }
     
    void Wait()
    {
       uint8_t i;
       for(i=0;i<20;i++)
          _delay_loop_2(0);
    }
     
    void main()
    {
       uint16_t adc_result;
       char int_buffer[10];
    DDRB=0xFF;
    DDRC=0x00;
    DDRD=0xFF;
       //Initialize ADC
       InitADC();
    init_lcd();
    while(1)
       {
          adc_result=ReadADC(0);           // Read Analog value from channel-0
           itoa(adc_result, int_buffer, 10);
     
    lcd_str(int_buffer);
    lcd_cmd(0x01);
    lcd_cmd(0x03);
          Wait();
       }
    }
    ….
     
    Please help me i hav connect pot to adc(0) channel if any varying pot then also it is taking high(1023) value only .
     
    April 4, 2015 at 4:21 pm #12761
    gavi prakash
    Participant

    please anyone tell me the solution

    April 11, 2015 at 4:36 am #12782
    Arjun Singh
    Participant

    Hello friend

    As i come to know there is problem in your "uint16_t ReadADC(uint8_t ch)" function

     " while(ADCSRA & (1<<ADSC));" this statement is wrong. As adsc is to start conversion.

    You need to write-

    while(!(ADCSRA & (1<<ADIF))); // waiting for ADIF, conversion complete
    ADCSRA|=(1<<ADIF); // clearing of ADIF, it is done by writing 1 to it
     
    After this modification your code will properly run.
     
    Any doubt, feel free to ask :)
  • 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

  • using a RTC in SF basic June 18, 2025
  • What is correct names for GOOD user friendly circuit drawing program? June 18, 2025
  • Is AI making embedded software developers more productive? June 18, 2025
  • Why can't I breadboard this oscillator? June 18, 2025
  • Parts required for a personal project June 17, 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