spacepaste

  1.  
  2. #include <avr/io.h> //Defines pins, ports, etc.
  3. #include <util/delay.h>
  4. static inline void initADC0(void) {
  5. ADMUX |= (1 << REFS0); //reference voltage on AVCC
  6. //ADCSRA |= (1 << ADPS1) | (1 << ADPS0); //ADC clock prescaler /8
  7. ADCSRA |= (1 << ADPS0) | (1 << ADPS1) | (1 << ADPS2); //ADC clock prescaler /64
  8. ADCSRA |= (1 << ADEN); //enables the ADC
  9. }
  10. int main(void) {
  11. uint16_t potentiometerValue;
  12. uint16_t threshold_level;
  13. threshold_level= 0b10000000;
  14. DDRB |= (1 << PB0); //Data Direction Register B: writing a 1 to the bit enables output
  15. initADC0();
  16. while (1) {
  17. ADCSRA |= (1 << ADSC); //start ADC conversion
  18. loop_until_bit_is_clear(ADCSRA, ADSC); //wait until ADC conversion is done
  19. potentiometerValue= ADC; //read ADC value in
  20. if (potentiometerValue > threshold_level) {
  21. PORTB= 0b00000001; //turn on LED attached to port PB0
  22. }
  23. else {
  24. PORTB= 0b00000000; //turn off LED attached to port PB0
  25. }
  26. return (0); //this line is never actually reached
  27. }
  28. }
  29.