Advertisement
Guest User

Untitled

a guest
Aug 29th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.99 KB | None | 0 0
  1. #define F_CPU 1000000UL //1Mhz
  2. #include <avr/io.h>
  3. #include <util/delay.h>
  4. #include <avr/interrupt.h>
  5. #include <stdlib.h>
  6.  
  7. int main (void){
  8.     DDRB |= 1 << PINB0; // set LEDRed as output
  9.     DDRB |= 1 << PINB1; // set LEDYellow2 as output
  10.     DDRB |= 1 << PINB2; // set LEDYellow1 as output
  11.     DDRB |= 1 << PINB3; // set LEDGreen2 as output
  12.     DDRB |= 1 << PINB4; // set LEDGreen1 as output
  13.    
  14.     //ADC prescaller, needs to be from, 50KHz to 200KHz, 16 prescale
  15.     //1 000 000 Hz / 50 000 hz = 20 or 1 000 000 Hz / 200 000 Hz = 5
  16.     ADCSRA |= 1 << ADPS2;
  17.     //8-bit Results 0 - 255
  18.     ADMUX |= 1 << ADLAR;
  19.     //Set AREF = AVCC with external capacitor at AREF pin
  20.     ADMUX |= 1 << REFS0;
  21.     //Enable ADC interrupts
  22.     //ADCSRA |= 1 << ADIE;
  23.     //Turn on ADC
  24.     ADCSRA |= 1 << ADEN;
  25.     //Enable global interrupts
  26.     sei();
  27.     //Start conversion
  28.     ADCSRA |= 1 << ADSC;
  29.    
  30.     while(1){
  31.         if(ADCH < 100 && ADCH > 20){
  32.             PORTB |= 1 << PINB0;
  33.             _delay_ms(500);
  34.             ADCSRA |= 1 << ADSC;
  35.         }
  36.         PORTB &= ~ 1 << PINB0;
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement