Please help me with code for using in built adc of atmega32. Its urgent. Thanks alot

Discussion in 'Embedded Systems and Microcontrollers' started by techspark, Feb 9, 2013.

  1. techspark

    Thread Starter New Member

    Dec 24, 2012
    11
    0
    the program is not running properly I tried it in proteus and used winavr.
    the code takes the analog value from port a and gives the output to port b.
    code as follows:

    Code ( (Unknown Language)):
    1.  
    2. #include<avr/io.h>
    3. #include<util/delay.h>
    4.  
    5. void ADC_init(void);
    6. unsigned int ADC_read(unsigned char);
    7.  
    8. // ------------------------------------------------
    9. int main(void)
    10. {
    11.     unsigned int value;
    12.     DDRB=0xFF;
    13.     DDRA=0x00;
    14.     ADC_init();    // Initialization of ADC
    15.     // ch=0;
    16.     while(1)
    17.     {
    18.         value=ADC_read(0);
    19.         PORTB=value;
    20.         _delay_us(500);
    21.     }
    22. }
    23. //------------------------------------------------
    24.  
    25. void ADC_init(void)        // Initialization of ADC
    26. {
    27.     ADMUX=(1<<REFS0)|(1 << ADLAR);    // AVcc with external capacitor at AREF
    28.     ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);    
    29.     SFIOR=(1<<PUD);                    // Enable ADC and set Prescaler division factor as 128 & also disabling the pull ups
    30. }
    31.  
    32. unsigned int ADC_read(unsigned char ch)
    33. {
    34.     ch= ch & 0b00000111;        // channel must be b/w 0 to 7
    35.     ADMUX |= ch;                // selecting channel
    36.     ADCSRA|=(1<<ADSC)| (1 << ADEN);            // start conversion
    37.     while(!(ADCSRA & (1<<ADIF)));    // waiting for ADIF, conversion complete
    38.     ADCSRA|=(1<<ADIF);            // clearing of ADIF, it is done by writing 1 to it
    39.  
    40.     return (ADC);
    41. }
    42.  
     
    Last edited by a moderator: Feb 9, 2013
  2. 402DF855

    New Member

    Feb 9, 2013
    9
    1
    ADSC clears when the conversion is complete, while ADIF is cleared when the ISR is executed. Not sure if you have an ISR, also not sure if this is THE problem but ADSC seems more appropriate.
     
  3. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    What exactly is your circuit doing?

    Do you have AVCC connected?

    Did your implementation work when you simulated it?

    Perhaps, you should give us a schematic....
     
  4. techspark

    Thread Starter New Member

    Dec 24, 2012
    11
    0
    AVCC connected to VCC and the circuit gets stuck incase i try to run in proteus.
    i don't know what exactly the problem is.
    thanks
     
  5. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    do that...

    Also, what errors are you getting from your simulation?

    does it work if you return ADCL, instead of ADCH?
     
  6. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    Heads up: Anything with "URGENT" in the title doesn't get read for a day or two intentionally.

    Are you running this on an actual microcontroller, or only in simulation?
     
Loading...