void setup() {
// put your setup code here, to run once:
//for clearing the bit use & and for setting use |(or)
ADMUX = (1<<REFS0);//referance is zero
ADMUX &=(~(1<<ADLAR));//left adjust
ADCSRA |=(1<<ADEN)| (1<<ADPS1) | (1<<ADPS0);// adc enable auto trigger prescalar
ADCSRA &=(~(1<<ADPS2)) & (~(1<<ADATE)) ;
ADCSRA |=(1<<ADSC);//star conversion
ADMUX &=(~(1<<MUX3))&(~(1<<MUX2))&(~(1<<MUX1))&(~(1<<MUX0));//pin no.
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
long x;
x=ADCL;
Serial.println(x);
}
this is my code i am learning register level coding spent whole sunday but couldnot get desiered output
I am readin a0 pin using this code it gives me 0 whereas if i program it normal it gives me different out and this output is satisfactory can u help me in decoding above code
ADCSRA = ((1<<ADPS2) | (1<<ADATE)) ; // this sets the two bits and zeros everything else
ADCSRA |= ((1<<ADPS2) | (1<<ADATE)) ; // this sets the individual bits
ADCSRA &= ~((1<<ADPS2) | (1<<ADATE)) ; // this clears the individual bits
Without checking whether all the other bits are correct or not, you have NOT set the ADC into "free running" mode, so you'd need to do a "start conversion" and "wait for conversion to complete" each time you want to read the results.
ADCSRA &=(~(1<<ADPS2)) & (~(1<<ADATE)) ;
I think that that works out to be technically correct, but I'd much rather see it written as: