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

Thread Starter

techspark

Joined Dec 24, 2012
11
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:

Rich (BB code):
#include<avr/io.h>
#include<util/delay.h>
 
void ADC_init(void);
unsigned int ADC_read(unsigned char);
 
// ------------------------------------------------
int main(void)
{
    unsigned int value;
    DDRB=0xFF;
    DDRA=0x00;
    ADC_init();    // Initialization of ADC
    // ch=0;
    while(1)
    {
        value=ADC_read(0);
        PORTB=value;
        _delay_us(500);
    } 
}
//------------------------------------------------
 
void ADC_init(void)        // Initialization of ADC
{
    ADMUX=(1<<REFS0)|(1 << ADLAR);    // AVcc with external capacitor at AREF
    ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);    
    SFIOR=(1<<PUD);                    // Enable ADC and set Prescaler division factor as 128 & also disabling the pull ups
}
 
unsigned int ADC_read(unsigned char ch)
{
    ch= ch & 0b00000111;        // channel must be b/w 0 to 7
    ADMUX |= ch;                // selecting channel
    ADCSRA|=(1<<ADSC)| (1 << ADEN);            // start conversion
    while(!(ADCSRA & (1<<ADIF)));    // waiting for ADIF, conversion complete
    ADCSRA|=(1<<ADIF);            // clearing of ADIF, it is done by writing 1 to it
 
    return (ADC);
}
 
Last edited by a moderator:

402DF855

Joined Feb 9, 2013
86
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.
 

tshuck

Joined Oct 18, 2012
3,527
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:[...]
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....
 

Thread Starter

techspark

Joined Dec 24, 2012
11
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
 

thatoneguy

Joined Feb 19, 2009
6,349
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?
 
Top