Advertisement
grist

Untitled

Jan 2nd, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * SimplePotPlay.c
  3.  *
  4.  * Created: 1/1/2013 3:57:11 PM
  5.  *  Author: grist.carrigafoyl
  6.  
  7.  Testing reading analog pins. This one works properly.
  8.  
  9.  Using the readings to alter the flashing pattern on an LED.
  10.  Using 10 bit resolution.
  11.  
  12.  */
  13.  
  14. #define F_CPU           8000000UL   //  8Mhz
  15. #define admux_bits      0b00000000  // All default (VCC ref, ADLAR unset)
  16. #define timer_pot       0b00000001  // ADC1 PB2 (pin7)
  17. #define photoresistor   0b00000011  // ADC3 PB3 (pin2)
  18.  
  19. #include <avr/io.h>
  20. #include <util/delay.h>
  21.  
  22.  
  23. // Prototypes
  24. int main(void);
  25. int read_adc(char pin);
  26.  
  27. int main(void)
  28. {
  29.     int timer_val,ambient_light, i;
  30.        
  31.     DDRB = _BV(PB1); // All input except PB1 (will drive the SSR, is LED for now)
  32.     PORTB = _BV(PB0);  // Turn on the internal pullup resistor on the IR detector pin
  33.    
  34.     // ADC settings. Check with datasheet that this is what you want
  35.     ADCSRA |= _BV(ADEN) | // Enable ADC
  36.     (_BV(ADPS1)) | // set prescaler to 128 (clock/128)
  37.     (_BV(ADPS0)) | // 8MHz/128 = 62.5Khz (aiming for 50 - 200)
  38.     (_BV(ADPS2));
  39.    
  40.     // How long the light stays on or off for. Simple constant multiplied by the pot reading
  41.     int delay_time = 2; // ms
  42.  
  43.     while(1) {
  44.        
  45.         timer_val = read_adc(timer_pot);
  46.         PORTB |= _BV(PB1);  // On
  47.         // _delay_ms will only take a constant as its argument or the code won't compile. So
  48.         // I'm using a constant time with a for loop around it as a simple multiplier. It's
  49.         // not very accurate, but good enough for this purpose.
  50.         for (i=0;i<timer_val;i++) {
  51.             _delay_ms(delay_time);
  52.         }
  53.        
  54.         ambient_light = read_adc(photoresistor);
  55.         PORTB &= ~_BV(PB1); // Off
  56.         for (i=0;i<ambient_light;i++) {
  57.             _delay_ms(delay_time); 
  58.         }
  59.  
  60.     }
  61.     return 0;
  62. }
  63.  
  64.  
  65. int read_adc(char pin)
  66. { // Read the analog value from the ADC input pin.
  67.     // Code based on samples from http://avrbasiccode.wikispaces.com/
  68.     int ret_val;
  69.  
  70.     // Assumes ADCSRA enabled and prescalars are set already
  71.     // Set the input channel and other ADMUX bits
  72.     ADMUX = admux_bits | pin;
  73.    
  74.     // Discard the first reading as it could
  75.     ADCSRA |= _BV(ADSC); // start a single conversion
  76.     while (ADCSRA & _BV(ADSC)){} // wait until conversion is done
  77.        
  78.     ADCSRA |= _BV(ADSC); // start a single conversion
  79.     while (ADCSRA & _BV(ADSC)){} // wait until conversion is done
  80.    
  81.     ret_val = ADCL; // Get ADCL first for 10bit and combine with ADCH
  82.     return((ADCH << 8) | ret_val);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement