Advertisement
grist

Untitled

Jan 2nd, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.17 KB | None | 0 0
  1. /*
  2.  * SimplePotPlay.c
  3.  *
  4.  * Created: 1/1/2013 3:57:11 PM
  5.  *  Author: grist.carrigafoyl
  6.  
  7.  Testing reading analog pins.
  8.  
  9.  Using the reading to alter the flashing pattern on an LED.
  10.  Only 8bit for now.
  11.  
  12.  */
  13.  
  14. #define F_CPU 8000000UL  //  8Mhz
  15. #define timer_pot MUX0
  16. #define photoresistor MUX1
  17.  
  18. #include <avr/io.h>
  19. #include <util/delay.h>
  20.  
  21.  
  22. // Prototypes
  23. int main(void);
  24. int read_adc(char pin);
  25.  
  26. int main(void)
  27. {
  28.        
  29.     DDRB = _BV(PB1); // All input except PB1
  30.     PORTB = _BV(PB0);  // Turn on the internal pullup resistor
  31.     int timer_val,ambient_light, i;
  32.    
  33.     // How long the light stays on for. Simple constant multiplied by the pot reading
  34.     int delay_time = 2; // ms
  35.     //wake_up();
  36.     while(1) {
  37.        
  38.         timer_val = read_adc(timer_pot);
  39.         PORTB |= _BV(PB1);  // On
  40.         for (i=0;i<timer_val;i++) {
  41.             _delay_ms(delay_time);
  42.         }
  43.        
  44.         ambient_light = read_adc(photoresistor);
  45.         PORTB &= ~_BV(PB1); // Off
  46.         for (i=0;i<ambient_light;i++) {
  47.             _delay_ms(delay_time); 
  48.         }
  49.     }
  50.     return 0;
  51. }
  52.  
  53.  
  54. int read_adc(char pin)
  55. {  // Read the analog value from the ADC input pin.
  56.     // Speculative code based on    http://avrbasiccode.wikispaces.com/
  57.     // When getting a 10bit value make sure you read the ADCL register before the ADCH register
  58.     // collected_value = (ADCH << 8)|ADCL (representation only, have to get ADCL first as per above)
  59.     int ret_val;
  60.    
  61.     // ADC settings. Check with datasheet that this is what you want
  62.         ADCSRA |= _BV(ADEN) | // Enable ADC
  63.         (_BV(ADPS1)) | // set prescaler to 128 (clock/128)
  64.         (_BV(ADPS0)) |
  65.         (_BV(ADPS2));
  66.    
  67.     // Assumes ADCSRA enabled and prescalars are set already
  68.     //ADMUX |= _BV(ADLAR) | // AD result store in (more significant bit in ADCH)
  69.     ADMUX |= _BV(pin); // Chose which AD input to read
  70.        
  71.     ADCSRA |= _BV(ADSC); // start a single conversion
  72.     while (ADCSRA & _BV(ADSC)){} // wait until conversion is done
  73.     // Throw away the first reading.   
  74.     ADCSRA |= _BV(ADSC); // start a single conversion
  75.     while (ADCSRA & _BV(ADSC)){} // wait until conversion is done
  76.    
  77.     ret_val = ADCL; // only the high byte for now. I will want ADCL as well for 10bit
  78.    
  79.     ADCSRA &= ~(_BV(ADEN)); // Turn the ADC off
  80.     return((ADCH << 8) | ret_val);
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement