trennfix/sw: Was not as beautiful as expected. Now it's better!
[eisenbahn.git] / trennfix / sw / config / trennfix_0.4.h
index 964853e2ec2f3ff80dbec5ed1d5a126382ee3a3a..b0c78157b65925bd4a105e93a68f50cf2be3223c 100644 (file)
@@ -6,15 +6,14 @@
 #define PIN_LED   _PIN(PORTB, PORTB2)
 #define PIN_SENSE _PIN(PORTB, PORTB4)
 #define PIN_DRIVE _PIN(PORTB, PORTB3)
-//#define PIN_DRIVE PIN_LED
 #define PIN_BTN   _PIN(PORTB, PB1)
-
+#define PIN_TRIGGER _PIN(PORTB, PB0)
 #define BTN_PRESSED (!PINVAL(PIN_BTN))
 
-#define START_TIMER0 ({TCNT0 = 0; TCCR0B = 2;})
-#define START_TIMER1 ({TCNT1 = 1; TCCR1 = 7; GTCCR |= 2;})
 #define STOP_TIMER1 ({TCCR1 = 0; TCNT1 = 1;})
 
+#define PWM_CYCLE 21
+
 static inline void setup_hw(void)
 {
        /* Turn off the 1/8 clock prescaler - now running at 16MHz*/
@@ -25,6 +24,7 @@ static inline void setup_hw(void)
        INPUT_PIN(PIN_BTN);
        OUTPUT_PIN(PIN_DRIVE);
        OUTPUT_PIN(PIN_LED);
+       OUTPUT_PIN(PIN_TRIGGER);
        setpin(PIN_BTN, 1);   /* Need pullup              */
        
        GIMSK |= _BV(PCIE);     /* Enable pin change interrupt for sense port */
@@ -35,14 +35,16 @@ static inline void setup_hw(void)
 
        /* Setup timer 0, used for mm_switch */
         TCCR0A = 0; /* Normal mode */
-       TCCR0B = 0; /* Timer off */
+       TCCR0B = 2; /* Prescaler 8 */
        TIMSK |= _BV(OCIE0A);   /* Get a match interrupt */
+       OCR0A = 100; TCCR0B = 2;
 
 #ifdef WITH_PWM        
        /* Timer 1 as PWM */
-       OCR1C = 14;
-       OCR1B = 14;
-       TCCR1 = 0x6;
+       OCR1C = PWM_CYCLE;
+       OCR1B = PWM_CYCLE;
+       TCCR1 = 0x5;
+
        GTCCR |= _BV(PWM1B) | _BV(COM1B0);
 #endif
 
@@ -54,25 +56,36 @@ static inline void setup_hw(void)
  * Can be altered to use the LED.
  *
  */
-static void __attribute__((unused)) trigger(void)
+static inline void __attribute__((unused)) trigger(void)
+{
+       setpin(PIN_TRIGGER, 1);
+       setpin(PIN_TRIGGER, 0);
+}
+
+
+static inline void __attribute__((unused)) trigger_on(void)
+{
+       setpin(PIN_TRIGGER, 1);
+}
+
+static inline void __attribute__((unused)) trigger_off(void)
 {
-       setpin(PIN_DRIVE, 1);
-       _delay_us(4);
-       setpin(PIN_DRIVE, 0);
-       _delay_us(4);
+       setpin(PIN_TRIGGER, 0);
 }
 
 /*
  * Configuration for mm receiver code.
  */
-
 #define MM_SENSE (!PINVAL(PIN_SENSE))
-#define MM_TSTART ({TCNT0 = 0; GTCCR |= 1; OCR0A = 115; TCCR0B = 2;})
 #define MM_TIMER_INT_VECT TIMER0_COMPA_vect
 
-/* Costs 38 bytes program memory */
-#define MM_FILTER_REPEATED
-#define USE_EEPROM_UPDATE
+#define MM_TSTART {                                                    \
+               GTCCR  |= _BV(TSM) | _BV(PSR0);                         \
+               TCNT0   = 0;                                            \
+               TIFR   |= _BV(OCF0A);                                   \
+               GTCCR  &= ~_BV(TSM);                                    \
+       }
+
 
 #ifdef USE_REGISTER_VARS
 uint8_t register drive_on asm("r8");