Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.81 KB | None | 0 0
  1. void adc_init(void){
  2.   ADCSRA |= ((1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0));  //16Mhz/128 = 125Khz the ADC reference clock
  3.   ADMUX |= (1<<REFS0);              //Voltage reference from Avcc (5v)
  4.   ADCSRA |= (1<<ADEN);              //Turn on ADC
  5.   ADCSRA |= (1<<ADSC);              //Do an initial conversion because this one is the slowest and to ensure that everything is up and running
  6. }
  7.  
  8. volatile uint16_t read_adc(uint8_t channel){
  9.   ADCW = 0;
  10.   ADMUX &= 0xF0;                    //Clear the older channel that was read
  11.   ADMUX |= channel;                //Defines the new ADC channel to be read
  12.   ADCSRA |= (1<<ADSC);                //Starts a new conversion
  13.   while(ADCSRA & (1<<ADSC));            //Wait until the conversion is done
  14.   return ADCW;                    //Returns the ADC value of the chosen channel
  15. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement