SHOW:
|
|
- or go back to the newest paste.
| 1 | /* | |
| 2 | * SimplePotPlay.c | |
| 3 | * | |
| 4 | * Created: 1/1/2013 3:57:11 PM | |
| 5 | * Author: grist.carrigafoyl | |
| 6 | ||
| 7 | - | Testing reading analog pins. |
| 7 | + | Testing reading analog pins. This one works properly. |
| 8 | ||
| 9 | - | Using the reading to alter the flashing pattern on an LED. |
| 9 | + | Using the readings to alter the flashing pattern on an LED. |
| 10 | - | Only 8bit for now. |
| 10 | + | Using 10 bit resolution. |
| 11 | ||
| 12 | */ | |
| 13 | ||
| 14 | - | #define F_CPU 8000000UL // 8Mhz |
| 14 | + | #define F_CPU 8000000UL // 8Mhz |
| 15 | - | #define timer_pot MUX0 |
| 15 | + | #define admux_bits 0b00000000 // All default (VCC ref, ADLAR unset) |
| 16 | - | #define photoresistor MUX1 |
| 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 | - | DDRB = _BV(PB1); // All input except PB1 |
| 29 | + | |
| 30 | - | PORTB = _BV(PB0); // Turn on the internal pullup resistor |
| 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 | - | // How long the light stays on for. Simple constant multiplied by the pot reading |
| 33 | + | |
| 34 | // ADC settings. Check with datasheet that this is what you want | |
| 35 | - | //wake_up(); |
| 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 | - | ambient_light = read_adc(photoresistor); |
| 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 | - | { // Read the analog value from the ADC input pin.
|
| 55 | + | |
| 56 | - | // Speculative code based on http://avrbasiccode.wikispaces.com/ |
| 56 | + | |
| 57 | - | // When getting a 10bit value make sure you read the ADCL register before the ADCH register |
| 57 | + | |
| 58 | - | // collected_value = (ADCH << 8)|ADCL (representation only, have to get ADCL first as per above) |
| 58 | + | |
| 59 | ||
| 60 | } | |
| 61 | return 0; | |
| 62 | - | ADCSRA |= _BV(ADEN) | // Enable ADC |
| 62 | + | |
| 63 | - | (_BV(ADPS1)) | // set prescaler to 128 (clock/128) |
| 63 | + | |
| 64 | - | (_BV(ADPS0)) | |
| 64 | + | |
| 65 | - | (_BV(ADPS2)); |
| 65 | + | |
| 66 | { // Read the analog value from the ADC input pin.
| |
| 67 | // Code based on samples from http://avrbasiccode.wikispaces.com/ | |
| 68 | - | //ADMUX |= _BV(ADLAR) | // AD result store in (more significant bit in ADCH) |
| 68 | + | |
| 69 | - | ADMUX |= _BV(pin); // Chose which AD input to read |
| 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 | - | // Throw away the first reading. |
| 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 | - | ret_val = ADCL; // only the high byte for now. I will want ADCL as well for 10bit |
| 77 | + | |
| 78 | ADCSRA |= _BV(ADSC); // start a single conversion | |
| 79 | - | ADCSRA &= ~(_BV(ADEN)); // Turn the ADC off |
| 79 | + | |
| 80 | ||
| 81 | ret_val = ADCL; // Get ADCL first for 10bit and combine with ADCH | |
| 82 | return((ADCH << 8) | ret_val); | |
| 83 | } |