1. 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.
    Dismiss Notice

ADC not working | ATMEGA16A | C++ AVRStudio

Discussion in 'AVR' started by ikelectro, Oct 24, 2015.

  1. ikelectro

    ikelectro Member

    Joined:
    Aug 25, 2012
    Messages:
    388
    Likes:
    4
    Location:
    Kolkata, Proud to be an Indian.
    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++.
    Code (text):
    /*



    #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
       
       
        while(1)
        {
        ADCSRA |=1<<ADSC;
        while (( ADCSRA&(1<<ADIF))==0);
       
        int z = ADCL;
        _delay_ms(20);        //delay for 20mili seconds
       
        int x = z%10;
        PORTB = 12;
        _delay_ms(1000);
        PORTB = x;            //3rd digit
        _delay_ms(1000);
       
        int a=z/10;
         unsigned short int b=a%10;
        PORTB = 13;
        _delay_ms(1000);  
        PORTB = b;            //2nd digit
        _delay_ms(1000);
       
    unsigned short int c =a/10;
        PORTB =14;
        _delay_ms(1000);
        PORTB = c;            //1st digit
        _delay_ms(15000);
       
          }
          return 0;
    }
     
  2. ikelectro

    ikelectro Member

    Joined:
    Aug 25, 2012
    Messages:
    388
    Likes:
    4
    Location:
    Kolkata, Proud to be an Indian.
    any one????? please help me out.
     
  3. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,302
    Likes:
    914
    Location:
    Rochdale UK
    I would help, but I'm not that familiar with AVR... What isn't working .. Nothing on portb??? Wrong data on portb??

    Where are your fuse settings?
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland

    I think you may have the same problem as in this thread: http://www.electro-tech-online.com/...c-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;
     
  6. ikelectro

    ikelectro Member

    Joined:
    Aug 25, 2012
    Messages:
    388
    Likes:
    4
    Location:
    Kolkata, Proud to be an Indian.
    okay, I'll give it try after returning home. Thanks for your reply.
     

Share This Page