• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

ADC not working | ATMEGA16A | C++ AVRStudio


Here is the code below. the code does not working with my hardware but with the same hardware ASM code is working. The result always same in C++.

#define F_CPU 4000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <string.h>

int main(void)
{   DDRB = 0xFF;            //PORTB AS OUT PUT
    DDRA =0;                //PORTA AS INPUT OF ADC
    ADCSRA = 0x87;        //initiazing ADC   
    ADMUX = 0xC0;
    _delay_ms(2000);    //delay for 2seconds
    ADCSRA |=1<<ADSC;
    while (( ADCSRA&(1<<ADIF))==0);
    int z = ADCL;
    _delay_ms(20);        //delay for 20mili seconds
    int x = z%10;
    PORTB = 12;
    PORTB = x;            //3rd digit
    int a=z/10;
     unsigned short int b=a%10;
    PORTB = 13;
    PORTB = b;            //2nd digit
unsigned short int c =a/10;
    PORTB =14;
    PORTB = c;            //1st digit
      return 0;


any one????? please help me out.


Well-Known Member
Most Helpful Member


I think you may have the same problem as in this thread: http://www.electro-tech-online.com/threads/result-is-not-written-to-adc-register-because-it-has-been-locked.141734/

Short answer is that you also need to read the "high byte" register ADCH after reading the low byte ADCL. Even if you don't use the high-byte, you need to read it.
Just read it to a dummy variable after:
int z = ADCL;
int adc_high = ADCH;
okay, I'll give it try after returning home. Thanks for your reply.

Latest threads

EE World Online Articles