Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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

Status
Not open for further replies.

techspark

Newbie level 2
Newbie level 2
Joined
Dec 29, 2012
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,306
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:


#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);
}
 

Did you use the proper microcontroller in Proteus or winavr?

Please make sure whether you are setting proper values for the registers such as DDRB, DDRA, ADMUX etc.
 

You are not giving details on the exact problem, making it hard to focus on something specific in order to spot the mistake.
After a quick view, the problem may lie on the fact that you are reading on a 16 bit variable ('"value" as it is named inside the code) from the adc, but you are driving this value to PORTB which has 8 pins. The ADC result is left adjusted (ADLAR=1), which means that only the lower 8 bits of the ADC reg will be passed to port pins. Those bits contain only the lower two bits of the of the conversion, page 217.

http://www.atmel.com/Images/doc2503.pdf

I don't know if this solves it, maybe you should provide more info on the problem.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top