- This topic has 1 reply, 2 voices, and was last updated 11 years, 9 months ago by .
Viewing 2 posts - 1 through 2 (of 2 total)
Viewing 2 posts - 1 through 2 (of 2 total)
- You must be logged in to reply to this topic.
|
Microcontroller › AVR › Please help me with code for using in built adc of atmega32. Its urgent.
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);
//
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);
}
Please try it on real hardware and update the result.