| Remote Access Control |

# timer.c

Go to the documentation of this file.

    00001 // This file has been prepared for Doxygen automatic documentation generation.
    00048 #include "timer.h"
    00049 #include "common.h"
    00050 #include "config.h"
    00051
    00052
    00053
    00055 #define TIMER_PRESCALER_VALUE ((1<<CS12) | (0<<CS11) | (1<<CS10))
    00057 #define SHORT_TIMEOUT_VALUE 100
    00058
    00059
    00060
    00061 volatile bool shortTimeout;
    00062 volatile bool longTimeout;
    00063
    00065 static byte longTimeoutHighCounter;
    00066
    00067
    00068
    00070 #pragma vector = TIMER1_COMPA_vect
    00071 __interrupt void shortTimeoutHandler(void)
    00072 {
    00073         // Set timeout flag.
    00074         shortTimeout = true;
    00075         // Disable this interrupt.
    00076         TIMSK1 &= ~(1<<OCIE1A);
    00077 }
    00078
    00079
    00080
    00082 #pragma vector = TIMER1_COMPB_vect
    00083 __interrupt void longTimeoutHandler(void)
    00084 {
    00085         // Count down.
    00086         if( longTimeoutHighCounter > 0 ) {
    00087                 if( --longTimeoutHighCounter == 0 ) {
    00088                         OCR1B = TCNT1 + LONG_TIMEOUT_LO_VALUE;
    00089                 }
    00090         } else {
    00091                 // Set timeout flag.
    00092                 longTimeout = true;
    00093                 // Disable this interrupt.
    00094                 TIMSK1 &= ~(1<<OCIE1B);
    00095         }
    00096 }
    00097
    00098
    00099
    00100 void startShortTimeout(void)
    00101 {
    00102         // Clear timeout flag.
    00103         shortTimeout = false;
    00104         // Initialize compare value relative
    00105         // to current timer value.
    00106         OCR1A = TCNT1 + SHORT_TIMEOUT_VALUE;
    00107         // Clear pending interrupt flag.
    00108         TIFR1 |= (1<<OCF1A);
    00109         // Enable interrupt.
    00110         TIMSK1 |= (1<<OCIE1A);
    00111         // Start timer/counter if not started yet.
    00112         TCCR1B = TIMER_PRESCALER_VALUE;
    00113 }
    00114
    00115
    00116
    00117 void startLongTimeout(void)
    00118 {
    00119         // Clear timeout flag.
    00120         longTimeout = false;
    00121         // Initialize countdown value.
    00122         longTimeoutHighCounter = LONG_TIMEOUT_HI_VALUE;
    00123         // Initialize compare value to one less than
    00124         // current counter value, so that we do not get
    00125         // an interrupt immediately.
    00126         OCR1B = TCNT1 - 1;
    00127         // Clear pending interrupt flag.
    00128         TIFR1 |= (1<<OCF1B);
    00129         // Enable interrupt.
    00130         TIMSK1 |= (1<<OCIE1B);
    00131         // Start timer/counter if not started yet.
    00132         TCCR1B = TIMER_PRESCALER_VALUE;
    00133 }
    00134
    00135
    00136
    00137 void stopTimer(void)
    00138 {
    00139         TCCR1B = 0x00;
    00140         shortTimeout = false;
    00141         longTimeout = false;
    00142 }
    00143

@DOC_TITLE@