]> www.fi.muni.cz Git - openparking.git/commitdiff
Clock handling factored out to a separate file.
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 22 May 2015 20:17:31 +0000 (22:17 +0200)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 22 May 2015 20:19:23 +0000 (22:19 +0200)
firmware/Makefile
firmware/clock.c [new file with mode: 0755]
firmware/clock.h [new file with mode: 0644]
firmware/firmware.c
firmware/modbus.c

index 9cfa5279c7355de6bb0f0238d1d470d965f5e9ef..e0bd5027e6c2e757e36750adc7027806b20e4b3f 100644 (file)
@@ -1,5 +1,5 @@
 PROGRAM=firmware
-SRC=firmware.c modbus.c
+SRC=firmware.c modbus.c clock.c
 OBJ=$(SRC:.c=.o)
 
 MCU=atmega328p
diff --git a/firmware/clock.c b/firmware/clock.c
new file mode 100755 (executable)
index 0000000..b618222
--- /dev/null
@@ -0,0 +1,9 @@
+#include <avr/io.h>
+#include "clock.h"
+
+void init_clock()
+{
+       // use the 16-bit Timer/Counter1 with 1024x prescaling
+       TCCR1B = _BV(CS12)|_BV(CS10); // CLK/1024
+}
+
diff --git a/firmware/clock.h b/firmware/clock.h
new file mode 100644 (file)
index 0000000..ed6c1df
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef CLOCK_H__
+#define CLOCK_H__ 1
+
+typedef uint16_t clock_t;
+#define get_clock() (TCNT1)
+#define CLOCK_HZ       (F_CPU/1024)
+extern void init_clock();
+
+#endif /* !CLOCK_H__ */
index 2019402b56c834bdeabf27899f311abe92615924..d70705d5993c25a2ce5829200e12d38a2349c6c3 100644 (file)
@@ -2,9 +2,11 @@
 #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
@@ -48,13 +50,13 @@ static void do_measurement(unsigned char trig)
 
        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);
@@ -180,8 +182,8 @@ int main()
                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();
                }
 
index a49bd877e1c778357e5f3f39f3f1b5ece6defbec..0c238c2a9b00d655522bcafee847753a2b80ba2d 100644 (file)
@@ -11,6 +11,7 @@
 #include <util/atomic.h>
 #include <util/delay.h>
 
+#include "clock.h"
 #include "modbus.h"
 
 #define BUFSIZE 128    // must be a power of two
@@ -44,13 +45,11 @@ static uint8_t mb_unit_id;
 #define UBRR_VAL        ((F_CPU + 8UL * UART_BAUD) / (16UL*UART_BAUD) - 1)
 #define wait_one_byte()        _delay_us(10*1000000/UART_BAUD)
 
-#define get_clock()    (TCNT1)
-#define CLOCK_SPEED    (F_CPU/1024)
 /*
  * According to Wikipedia, it is indeed 28 bits = 3.5 bytes without
  * start- and stopbits.
  */
-#define TIMEOUT                (28*CLOCK_SPEED/UART_BAUD)
+#define REQ_TIMEOUT            (28*CLOCK_HZ/UART_BAUD)
 
 uint16_t hold_regs[MB_N_HOLD_REGS];
 
@@ -196,7 +195,7 @@ void modbus_poll()
                if (rx_bytes == 0) // nothing received yet
                        return;
 
-               if (get_clock() - last_rx < TIMEOUT) // still receiving
+               if (get_clock() - last_rx < REQ_TIMEOUT) // still receiving
                        return;
 
                if (rx_bytes < 4) { // too short