Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * SimplePotPlay.c
- *
- * Created: 1/1/2013 3:57:11 PM
- * Author: grist.carrigafoyl
- Testing reading analog pins.
- Using the reading to alter the flashing pattern on an LED.
- Only 8bit for now.
- */
- #define F_CPU 8000000UL // 8Mhz
- #define timer_pot MUX0
- #define photoresistor MUX1
- #include <avr/io.h>
- #include <util/delay.h>
- // Prototypes
- int main(void);
- int read_adc(char pin);
- int main(void)
- {
- DDRB = _BV(PB1); // All input except PB1
- PORTB = _BV(PB0); // Turn on the internal pullup resistor
- int timer_val,ambient_light, i;
- // How long the light stays on for. Simple constant multiplied by the pot reading
- int delay_time = 2; // ms
- //wake_up();
- while(1) {
- timer_val = read_adc(timer_pot);
- PORTB |= _BV(PB1); // On
- for (i=0;i<timer_val;i++) {
- _delay_ms(delay_time);
- }
- ambient_light = read_adc(photoresistor);
- PORTB &= ~_BV(PB1); // Off
- for (i=0;i<ambient_light;i++) {
- _delay_ms(delay_time);
- }
- }
- return 0;
- }
- int read_adc(char pin)
- { // Read the analog value from the ADC input pin.
- // Speculative code based on http://avrbasiccode.wikispaces.com/
- // When getting a 10bit value make sure you read the ADCL register before the ADCH register
- // collected_value = (ADCH << 8)|ADCL (representation only, have to get ADCL first as per above)
- int ret_val;
- // ADC settings. Check with datasheet that this is what you want
- ADCSRA |= _BV(ADEN) | // Enable ADC
- (_BV(ADPS1)) | // set prescaler to 128 (clock/128)
- (_BV(ADPS0)) |
- (_BV(ADPS2));
- // Assumes ADCSRA enabled and prescalars are set already
- //ADMUX |= _BV(ADLAR) | // AD result store in (more significant bit in ADCH)
- ADMUX |= _BV(pin); // Chose which AD input to read
- ADCSRA |= _BV(ADSC); // start a single conversion
- while (ADCSRA & _BV(ADSC)){} // wait until conversion is done
- // Throw away the first reading.
- ADCSRA |= _BV(ADSC); // start a single conversion
- while (ADCSRA & _BV(ADSC)){} // wait until conversion is done
- ret_val = ADCL; // only the high byte for now. I will want ADCL as well for 10bit
- ADCSRA &= ~(_BV(ADEN)); // Turn the ADC off
- return((ADCH << 8) | ret_val);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement