From 431fefa5f2fc79a6ac8890e6d8ca8d0ddedd06a4 Mon Sep 17 00:00:00 2001 From: Bart Van Der Meerssche Date: Sat, 4 Sep 2010 19:44:54 +0200 Subject: [PATCH] avr: add a ms timestamp to a pls message for pulse inputs --- mote/v1/avr/main.c | 27 ++++++++++++++++++++++++--- mote/v1/avr/main.h | 11 +++++++++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/mote/v1/avr/main.c b/mote/v1/avr/main.c index 8114edb..75609a8 100644 --- a/mote/v1/avr/main.c +++ b/mote/v1/avr/main.c @@ -43,6 +43,8 @@ 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]; +volatile struct time_struct time = {false, 0}; + volatile uint8_t muxn = 0; volatile uint16_t timer = 0; @@ -50,12 +52,14 @@ volatile uint16_t timer = 0; ISR(INT0_vect) { measurements[2].value++; aux[2].pulse = true; + aux[2].time = time.ms; } // interrupt service routine for INT1 ISR(INT1_vect) { measurements[3].value++; aux[3].pulse = true; + aux[3].time = time.ms; } // interrupt service routine for PCI2 (PCINT20) @@ -67,6 +71,7 @@ ISR(PCINT2_vect) { else { measurements[4].value++; aux[4].pulse = true; + aux[4].time = time.ms; aux[4].toggle = false; } } @@ -104,6 +109,11 @@ ISR(TIMER2_COMPA_vect) { if (!(muxn &= 0x1)) timer++; if (timer > SECOND) timer = 0; + // We have timer interrupts occcuring at a frequency of 1250Hz. + // In order to map this to 1000Hz (=ms) we have to skip every fifth interrupt. + if (!time.skip) time.ms++; + time.skip = (((time.ms & 0x3) == 3) && !time.skip) ? true : false; + ADMUX &= 0xF8; ADMUX |= muxn; // start a new ADC conversion @@ -260,13 +270,14 @@ void setup() void send(uint8_t msg_type, const struct sensor *measurement, const struct state *aux) { - uint8_t i = 46; - char message[49]; + uint8_t i; uint32_t value = 0; - + uint32_t ms = 0; int32_t rest; uint8_t pulse_count; + char message[60]; + switch (msg_type) { case PULSE: // blink the green LED @@ -276,6 +287,7 @@ void send(uint8_t msg_type, const struct sensor *measurement, const struct state cli(); value = measurement->value; + ms = aux->time; sei(); strcpy(message, "pls "); @@ -302,10 +314,19 @@ void send(uint8_t msg_type, const struct sensor *measurement, const struct state strcpy(&message[4], measurement->id); strcpy(&message[36], ":0000000000\n"); + i = 46; do { // generate digits in reverse order message[i--] = '0' + value % 10; // get next digit } while ((value /= 10) > 0); // delete it + if ((msg_type == PULSE) && ms) { + strcpy(&message[47], ":0000000000\n"); + i = 57; + do { // generate digits in reverse order + message[i--] = '0' + ms % 10; // get next digit + } while ((ms /= 10) > 0); // delete it + } + printString(message); } diff --git a/mote/v1/avr/main.h b/mote/v1/avr/main.h index 1596d88..b29dd31 100644 --- a/mote/v1/avr/main.h +++ b/mote/v1/avr/main.h @@ -88,9 +88,14 @@ asm volatile ( \ ) // datastructures +struct time_struct { + boolean skip; + uint32_t ms; +}; + struct state { - boolean pulse; - boolean toggle; + boolean pulse; + boolean toggle; uint32_t nano; uint16_t adc; @@ -99,6 +104,8 @@ struct state { uint32_t nano_end; uint8_t pulse_count; uint8_t pulse_count_final; + + uint32_t time; }; struct sensor {