avr: add a ms timestamp to a pls message for pulse inputs

This commit is contained in:
Bart Van Der Meerssche 2010-09-04 19:44:54 +02:00
parent 8d1ccbc8fc
commit 431fefa5f2
2 changed files with 33 additions and 5 deletions

View File

@ -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 EEMEM EEPROM_measurements[4] = {{SENSOR0, START}, {SENSOR1, START}, {SENSOR2, START}, {SENSOR3, START}};
volatile struct sensor measurements[4]; volatile struct sensor measurements[4];
volatile struct time_struct time = {false, 0};
volatile uint8_t muxn = 0; volatile uint8_t muxn = 0;
volatile uint16_t timer = 0; volatile uint16_t timer = 0;
@ -50,12 +52,14 @@ volatile uint16_t timer = 0;
ISR(INT0_vect) { ISR(INT0_vect) {
measurements[2].value++; measurements[2].value++;
aux[2].pulse = true; aux[2].pulse = true;
aux[2].time = time.ms;
} }
// interrupt service routine for INT1 // interrupt service routine for INT1
ISR(INT1_vect) { ISR(INT1_vect) {
measurements[3].value++; measurements[3].value++;
aux[3].pulse = true; aux[3].pulse = true;
aux[3].time = time.ms;
} }
// interrupt service routine for PCI2 (PCINT20) // interrupt service routine for PCI2 (PCINT20)
@ -67,6 +71,7 @@ ISR(PCINT2_vect) {
else { else {
measurements[4].value++; measurements[4].value++;
aux[4].pulse = true; aux[4].pulse = true;
aux[4].time = time.ms;
aux[4].toggle = false; aux[4].toggle = false;
} }
} }
@ -104,6 +109,11 @@ ISR(TIMER2_COMPA_vect) {
if (!(muxn &= 0x1)) timer++; if (!(muxn &= 0x1)) timer++;
if (timer > SECOND) timer = 0; 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 &= 0xF8;
ADMUX |= muxn; ADMUX |= muxn;
// start a new ADC conversion // 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) void send(uint8_t msg_type, const struct sensor *measurement, const struct state *aux)
{ {
uint8_t i = 46; uint8_t i;
char message[49];
uint32_t value = 0; uint32_t value = 0;
uint32_t ms = 0;
int32_t rest; int32_t rest;
uint8_t pulse_count; uint8_t pulse_count;
char message[60];
switch (msg_type) { switch (msg_type) {
case PULSE: case PULSE:
// blink the green LED // blink the green LED
@ -276,6 +287,7 @@ void send(uint8_t msg_type, const struct sensor *measurement, const struct state
cli(); cli();
value = measurement->value; value = measurement->value;
ms = aux->time;
sei(); sei();
strcpy(message, "pls "); 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[4], measurement->id);
strcpy(&message[36], ":0000000000\n"); strcpy(&message[36], ":0000000000\n");
i = 46;
do { // generate digits in reverse order do { // generate digits in reverse order
message[i--] = '0' + value % 10; // get next digit message[i--] = '0' + value % 10; // get next digit
} while ((value /= 10) > 0); // delete it } 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); printString(message);
} }

View File

@ -88,6 +88,11 @@ asm volatile ( \
) )
// datastructures // datastructures
struct time_struct {
boolean skip;
uint32_t ms;
};
struct state { struct state {
boolean pulse; boolean pulse;
boolean toggle; boolean toggle;
@ -99,6 +104,8 @@ struct state {
uint32_t nano_end; uint32_t nano_end;
uint8_t pulse_count; uint8_t pulse_count;
uint8_t pulse_count_final; uint8_t pulse_count_final;
uint32_t time;
}; };
struct sensor { struct sensor {