#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.h>
+#include "clock.h"
#include "modbus.h"
-#define TIMEOUT 0x2FF
+#define ECHO_TIMEOUT (CLOCK_HZ/10) // 10 ms
+#define MEASUREMENT_WAIT (CLOCK_HZ/3) // three triggers per second
#define N_TRIGGERS 3
#define N_SENSORS 12
pull_trigger(trig);
- starttime = TCNT1;
+ starttime = get_clock();
while (to_start || to_measure) {
uint8_t bits = 0;
- uint16_t now = TCNT1;
+ uint16_t now = get_clock();
- if (now-starttime >= TIMEOUT)
+ if (now-starttime >= ECHO_TIMEOUT)
break;
bits = get_pin(trig);
distances[trig*N_TRIG_SENSORS + i] = 0;
}
-static void do_measurements()
-{
- uint8_t trig;
-
- for (trig = 0; trig < N_TRIGGERS; trig++) {
- do_measurement(trig);
- _delay_ms(200);
- }
-}
-
static void led_set(uint8_t led, uint8_t state)
{
if (led == 0) {
if (state) {
- PORTD |= _BV(PD4);
- // PORTC |= _BV(PC5);
+ PORTC |= _BV(PC5);
led_bitmap |= 1;
} else {
- PORTD &= ~_BV(PD4);
- // PORTC &= ~_BV(PC5);
+ PORTC &= ~_BV(PC5);
led_bitmap &= ~1;
}
} else {
DDRD |= _BV(PD7); // Trig D
DDRB |= _BV(PB4) | _BV(PB5); // Trig B, LED 2
DDRC |= _BV(PC4) | _BV(PC5); // Trig C, LED 1
- // temporary LED
- DDRD |= _BV(PD4);
// set up the timer
TCCR1A = 0;
for (trig = 0; trig < N_TRIGGERS; trig++) {
uint16_t now;
do_measurement(trig);
- now = TCNT1;
- while (TCNT1-now < 200)
+ now = get_clock();
+ while (get_clock()-now < MEASUREMENT_WAIT)
modbus_poll();
}