From 3dead16060cacf01329ae0b5e02223a45a2e21f5 Mon Sep 17 00:00:00 2001 From: Bart Van Der Meerssche Date: Fri, 12 Feb 2010 19:57:07 +0000 Subject: [PATCH] uc: enable support for sampling both ADC0 and ADC1 --- uc/main.c | 68 +++++++++++++++++++++++++++++++++---------------------- uc/main.h | 14 ++++++------ 2 files changed, 48 insertions(+), 34 deletions(-) diff --git a/uc/main.c b/uc/main.c index 1ca7c7e..7b8b86b 100644 --- a/uc/main.c +++ b/uc/main.c @@ -41,45 +41,60 @@ volatile struct state aux[4] = {{false, false, START, 0}, {false, false, START, volatile struct sensor EEMEM EEPROM_measurements[4] = {{SENSOR0, START}, {SENSOR1, START}, {SENSOR2, START}, {SENSOR3, START}}; volatile struct sensor measurements[4]; +uint8_t muxn = 0; // interrupt service routine for INT0 ISR(INT0_vect) { - measurements[1].value++; - aux[1].pulse = true; -} - -// interrupt service routine for INT1 -ISR(INT1_vect) { measurements[2].value++; aux[2].pulse = true; } +// interrupt service routine for INT1 +ISR(INT1_vect) { + measurements[3].value++; + aux[3].pulse = true; +} + // interrupt service routine for PCI2 (PCINT20) +/** ISR(PCINT2_vect) { - if (aux[3].toggle == false) { - aux[3].toggle = true; + if (aux[4].toggle == false) { + aux[4].toggle = true; } else { - measurements[3].value++; - aux[3].pulse = true; - aux[3].toggle = false; + measurements[4].value++; + aux[4].pulse = true; + aux[4].toggle = false; } } +**/ // interrupt service routine for ADC ISR(TIMER2_OVF_vect) { // read ADC result // add to nano(Wh) counter - MacU16X16to32(aux[0].nano, METERCONST, ADC); + if (muxn < 2) { + MacU16X16to32(aux[muxn].nano, METERCONST, ADC); - if (aux[0].nano > 1000000000) { - measurements[0].value++; - aux[0].pulse = true; - aux[0].nano -= 1000000000; - aux[0].debug = ADC; + if (aux[muxn].nano > 1000000000) { + measurements[muxn].value++; + aux[muxn].pulse = true; + aux[muxn].nano -= 1000000000; + aux[muxn].debug = ADC; + } + } + + // rotate amongst the available ADC input channels (0 to 7) + muxn = ++muxn & 0x7; + + // but only use ADC0 and 1 + if (muxn < 2) { + ADMUX &= 0xF8; + ADMUX |= muxn; + + // start a new ADC conversion + ADCSRA |= (1< fTOV2 = 1000kHz / 256 / 8 = 488.28Hz (DS p.158) - TCCR2B |= (1< fTOV2 = 1000kHz / 256 / 1 = 3906.24Hz (DS p.158) + TCCR2B |= (1< 1000kHz / 8 = 125kHz (DS p.258) ADCSRA |= (1<