uc: enable support for sampling both ADC0 and ADC1

This commit is contained in:
Bart Van Der Meerssche 2010-02-12 19:57:07 +00:00
parent 9f0852e5bb
commit 3dead16060
2 changed files with 48 additions and 34 deletions

View File

@ -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 EEMEM EEPROM_measurements[4] = {{SENSOR0, START}, {SENSOR1, START}, {SENSOR2, START}, {SENSOR3, START}};
volatile struct sensor measurements[4]; volatile struct sensor measurements[4];
uint8_t muxn = 0;
// interrupt service routine for INT0 // interrupt service routine for INT0
ISR(INT0_vect) { ISR(INT0_vect) {
measurements[1].value++;
aux[1].pulse = true;
}
// interrupt service routine for INT1
ISR(INT1_vect) {
measurements[2].value++; measurements[2].value++;
aux[2].pulse = true; aux[2].pulse = true;
} }
// interrupt service routine for PCI2 (PCINT20) // interrupt service routine for INT1
ISR(PCINT2_vect) { ISR(INT1_vect) {
if (aux[3].toggle == false) {
aux[3].toggle = true;
}
else {
measurements[3].value++; measurements[3].value++;
aux[3].pulse = true; aux[3].pulse = true;
aux[3].toggle = false; }
// interrupt service routine for PCI2 (PCINT20)
/**
ISR(PCINT2_vect) {
if (aux[4].toggle == false) {
aux[4].toggle = true;
}
else {
measurements[4].value++;
aux[4].pulse = true;
aux[4].toggle = false;
} }
} }
**/
// interrupt service routine for ADC // interrupt service routine for ADC
ISR(TIMER2_OVF_vect) { ISR(TIMER2_OVF_vect) {
// read ADC result // read ADC result
// add to nano(Wh) counter // 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) { if (aux[muxn].nano > 1000000000) {
measurements[0].value++; measurements[muxn].value++;
aux[0].pulse = true; aux[muxn].pulse = true;
aux[0].nano -= 1000000000; aux[muxn].nano -= 1000000000;
aux[0].debug = ADC; 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 // start a new ADC conversion
ADCSRA |= (1<<ADSC); ADCSRA |= (1<<ADSC);
}
} }
// interrupt service routine for analog comparator // interrupt service routine for analog comparator
@ -189,8 +204,8 @@ void setup()
ACSR |= (1<<ACBG) | (1<<ACIE) | (1<<ACIS1) | (1<<ACIS0); ACSR |= (1<<ACBG) | (1<<ACIE) | (1<<ACIS1) | (1<<ACIS0);
// Timer2 normal operation // Timer2 normal operation
// Timer2 clock prescaler set to 8 => fTOV2 = 1000kHz / 256 / 8 = 488.28Hz (DS p.158) // Timer2 clock prescaler set to 1 => fTOV2 = 1000kHz / 256 / 1 = 3906.24Hz (DS p.158)
TCCR2B |= (1<<CS21); TCCR2B |= (1<<CS20);
TIMSK2 |= (1<<TOIE2); TIMSK2 |= (1<<TOIE2);
// disable digital input cicuitry on ADCx pins to reduce leakage current // disable digital input cicuitry on ADCx pins to reduce leakage current
@ -198,11 +213,8 @@ void setup()
// select VBG as reference for ADC // select VBG as reference for ADC
ADMUX |= (1<<REFS1) | (1<<REFS0); ADMUX |= (1<<REFS1) | (1<<REFS0);
// ADCn selected via main.h
ADMUX |= MUXN;
// ADC prescaler set to 8 => 1000kHz / 8 = 125kHz (DS p.258) // ADC prescaler set to 8 => 1000kHz / 8 = 125kHz (DS p.258)
ADCSRA |= (1<<ADPS1) | (1<<ADPS0); ADCSRA |= (1<<ADPS1) | (1<<ADPS0);
// enable ADC and start a first ADC conversion // enable ADC and start a first ADC conversion
ADCSRA |= (1<<ADEN) | (1<<ADSC); ADCSRA |= (1<<ADEN) | (1<<ADSC);
@ -243,10 +255,12 @@ void loop()
// check whether we have to send out a pls to the deamon // check whether we have to send out a pls to the deamon
for (i=0; i<4; i++) { for (i=0; i<4; i++) {
if (aux[i].pulse == true) { if (aux[i].pulse == true) {
if (i == 0) { if (i < 2) {
//debugging //debugging
printString("msg ADC sample value: "); printString("msg ADC");
printIntegerInBase((unsigned long)aux[0].debug, 10); printInteger((long)i);
printString(" sample value: ");
printIntegerInBase((unsigned long)aux[i].debug, 10);
printString("\n"); printString("\n");
} }
send((const struct sensor *)&measurements[i]); send((const struct sensor *)&measurements[i]);

View File

@ -43,32 +43,32 @@
#ifndef METERCONST #ifndef METERCONST
#if TYPE == 2201 // 220V - 1-phase @ 488.28Hz sampling rate #if TYPE == 2201 // 220V - 1-phase @ 488.28Hz sampling rate
#define METERCONST 6783 #define METERCONST 6783
#define MUXN 0 // #define MUXN 0
#warning "220V - 1-phase selected. METERCONST set to 6783" #warning "220V - 1-phase selected. METERCONST set to 6783"
#elif TYPE == 2203 // 220V - 3-phase @ 488.28Hz sampling rate #elif TYPE == 2203 // 220V - 3-phase @ 488.28Hz sampling rate
#define METERCONST 6721 #define METERCONST 6721
#define MUXN 1 // #define MUXN 1
#warning "220V - 3-phase selected. METERCONST set to 6721" #warning "220V - 3-phase selected. METERCONST set to 6721"
#elif TYPE == 2301 // 230V - 1-phase @ 488.28Hz sampling rate #elif TYPE == 2301 // 230V - 1-phase @ 488.28Hz sampling rate
#define METERCONST 7091 #define METERCONST 7091
#define MUXN 0 // #define MUXN 0
#warning "230V - 1-phase selected. METERCONST set to 7091" #warning "230V - 1-phase selected. METERCONST set to 7091"
#elif TYPE == 2303 // 230V - 3-phase @ 488.28Hz sampling rate #elif TYPE == 2303 // 230V - 3-phase @ 488.28Hz sampling rate
#define METERCONST 7026 #define METERCONST 7026
#define MUXN 1 // #define MUXN 1
#warning "230V - 3-phase selected. METERCONST set to 7026" #warning "230V - 3-phase selected. METERCONST set to 7026"
#elif TYPE == 2401 // 240V - 1-phase @ 488.28Hz sampling rate #elif TYPE == 2401 // 240V - 1-phase @ 488.28Hz sampling rate
#define METERCONST 7399 #define METERCONST 7399
#define MUXN 0 // #define MUXN 0
#warning "240V - 1-phase selected. METERCONST set to 7399" #warning "240V - 1-phase selected. METERCONST set to 7399"
#elif TYPE == 2403 // 240V - 3-phase @ 488.28Hz sampling rate #elif TYPE == 2403 // 240V - 3-phase @ 488.28Hz sampling rate
#define METERCONST 7331 #define METERCONST 7331
#define MUXN 1 // #define MUXN 1
#warning "240V - 3-phase selected. METERCONST set to 7331" #warning "240V - 3-phase selected. METERCONST set to 7331"
#endif #endif
#endif #endif