implemented 500ns timer and set jumpstart time

Wed, 07 Dec 2011 16:50:51 +0100

author
Malte Bayer <mbayer@neo-soft.org>
date
Wed, 07 Dec 2011 16:50:51 +0100
changeset 42
018d4d63ad3b
parent 41
9857c18c5e64
child 43
4e5d389f7b83

implemented 500ns timer and set jumpstart time

blackbox/lowlevel.c file | annotate | diff | comparison | revisions
blackbox/main.c file | annotate | diff | comparison | revisions
blackbox/main.h file | annotate | diff | comparison | revisions
--- a/blackbox/lowlevel.c	Wed Dec 07 15:56:05 2011 +0100
+++ b/blackbox/lowlevel.c	Wed Dec 07 16:50:51 2011 +0100
@@ -87,6 +87,9 @@
     //RAIL_DETECT_PORT |= _BV(RAIL_DETECT); // enable internal pull-up
     DDR(RAIL_POWER_PORT) |= _BV(RAIL_POWER);
 
+    // setup debut output on i2c pins
+    DDR(PORTC) |= _BV(PC0) | _BV(PC1);
+
     // display init sequence on LEDs
     LED(1, 1); blinkdelay();
     LED(2, 1); blinkdelay();
@@ -118,7 +121,8 @@
 
     // setup data packet timer
     TCCR1B = (1<<CS11) | (1<<WGM12); //divide by 8, set compare match (1mhz clock)
-    OCR1A = TIMER1_7500NS;
+    OCR1A = TIMER1_500NS;
+    //OCR1A = TIMER1_7500NS;
     TIMSK |= 1<<OCIE1A; //enable timer1 interrupt
 
     RS232_puts_p(PSTR("FreeSlot Blackbox v"));
--- a/blackbox/main.c	Wed Dec 07 15:56:05 2011 +0100
+++ b/blackbox/main.c	Wed Dec 07 16:50:51 2011 +0100
@@ -30,6 +30,10 @@
 
 
 #define MAX_SLOTS       6
+volatile uint16_t sysclk_ms_low = 0;
+volatile uint16_t sysclk_ms_high = 0;
+volatile uint8_t sysclk_packettimer = 0;
+
 volatile uint8_t speedlimit[MAX_SLOTS];
 volatile uint8_t fuel[MAX_SLOTS];
 volatile uint8_t jumpstart_time[MAX_SLOTS];
@@ -134,8 +138,7 @@
 
 void jumpstart(uint8_t controller) {
     if (jumpstart_time[controller] == 0) {
-        jumpstart_time[controller] = 1;
-        // todo: later the timestamp should written instead of just "1"
+        jumpstart_time[controller] = sysclk_ms_low;
         RS232_putc('~');
         RS232_putc('0'-controller);
         RS232_putc('\n');
@@ -238,14 +241,21 @@
 }
 
 ISR ( TIMER1_COMPA_vect ) {
+    //PORTC ^= _BV(PC0); // DEBUG OUTPUT SYSTEM CLOCK
+
     // trigger packet transfer:
-    transmit_len = transmit_len_next;
+    if (sysclk_packettimer == 14) { // 15*500 = 7500 NS
+        transmit_len = transmit_len_next;
+        sysclk_packettimer = 0;
+    } else sysclk_packettimer++;
     // here is some more time to do something else...
 
     // reset both car counters to overflow
-    car0_old = TIMER1_7500NS;
-    car1_old = TIMER1_7500NS;
+    car0_old = TIMER1_500NS;
+    car1_old = TIMER1_500NS;
 
+    sysclk_ms_low++; // increment 500ns timer
+    if (sysclk_ms_low == 0) sysclk_ms_high++; // increment upper 16bits
 }
 
 ISR ( TIMER2_COMP_vect ) {
@@ -360,7 +370,7 @@
 void reset_vars(void) {
     uint8_t i;
     for (i=0; i<MAX_SLOTS; i++) {
-        speedlimit[i] = 15;
+        if (i<4) speedlimit[i] = 15; else speedlimit[i] = 0;
         fuel[i] = 100;
         jumpstart_time[i] = 0;
     }
@@ -381,6 +391,8 @@
         case 2: LED(4, 1); break;
         case 1: LED(5, 1); break;
         case 0: { // RACE START!
+            sysclk_ms_low = 1;
+            sysclk_ms_high = 0;
             LEDS_OFF();
             LED(3, 1);
             mode = 3;
@@ -459,6 +471,8 @@
                     RS232_puts_p(prepare);
                 } else if (mode == 1) {
                     // Initiate race countdown
+                    sysclk_ms_low = 1;
+                    sysclk_ms_high = 0;
                     countdown = 5;
                     countdown_loops = COUNTDOWN_DELAY;
                     mode = 2;
--- a/blackbox/main.h	Wed Dec 07 15:56:05 2011 +0100
+++ b/blackbox/main.h	Wed Dec 07 16:50:51 2011 +0100
@@ -83,7 +83,8 @@
 #define TIMER1_DIVISOR  8
 #define VALUE1_7500NS   0.75e-2
 #define TIMER1_7500NS   (uint16_t)(VALUE1_7500NS * F_CPU/TIMER1_DIVISOR)
-
+#define VALUE1_500NS    0.5e-3
+#define TIMER1_500NS    (uint16_t)((VALUE1_500NS * F_CPU/TIMER1_DIVISOR) + 2) // MEASURE THIS EXACTLY WITH SCOPE
 
 #define DDR(x) (*(&x - 1))      // address of data direction register of port x
 #define PIN(x) (*(&x - 2))      // address of input register of port x

mercurial