southpaw4471092
Junior Member level 1
- Joined
- Apr 6, 2012
- Messages
- 15
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,392
Hi,
im having a problem in executing the in-built adc function practically.Please help me rectify it.
i will elaborate on my doubts.Initially how should the AVCC ,AVref pin connected?
Also i have another problem with the execution of IF statement, evn when condition becomes false, it executes the statement inside if condition.
the following is my code.
im having a problem in executing the in-built adc function practically.Please help me rectify it.
i will elaborate on my doubts.Initially how should the AVCC ,AVref pin connected?
Also i have another problem with the execution of IF statement, evn when condition becomes false, it executes the statement inside if condition.
the following is my code.
Code:
void adc_init()
{
ADMUX|=(1<<REFS0)|(1<<REFS1);
ADCSRA|=(1<<ADEN)|(1<<ADPS1)|(1<<ADPS0)|(1<<ADPS2);
}
uint16_t adc_read(uint8_t ch)
{
// select the corresponding channel 0~7
// ANDing with '7' will always keep the value
// of 'ch' between 0 and 7
ch &= 0b00000111; // AND operation with 7
ADMUX = (ADMUX & 0xF8)|ch; // clears the bottom 3 bits before ORing
// start single conversion
// write '1' to ADSC
ADCSRA |= (1<<ADSC);
// wait for conversion to complete
// ADSC becomes '0' again
// till then, run loop continuously
while(ADCSRA & (1<<ADSC));
return (ADCW);
}
void main()
{
DDRB|=(1<<PB0);
adc_init();
while(1)
{
if(adc_read(0) > 400)
{
PORTB|=(1<<PB0);
}
}
}
Last edited by a moderator: