techspark
Newbie level 2
- Joined
- Dec 29, 2012
- Messages
- 2
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- 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);
}
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);
}