- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

# time.c

Go to the documentation of this file.

    00001 /*This file is prepared for Doxygen automatic documentation generation.*/
    00013
    00014 /* Copyright (c) 2009 Atmel Corporation. All rights reserved.
    00015  *
    00016  * Redistribution and use in source and binary forms, with or without
    00017  * modification, are permitted provided that the following conditions are met:
    00018  *
    00019  * 1. Redistributions of source code must retain the above copyright notice,
    00020  * this list of conditions and the following disclaimer.
    00021  *
    00022  * 2. Redistributions in binary form must reproduce the above copyright notice,
    00023  * this list of conditions and the following disclaimer in the documentation
    00024  * and/or other materials provided with the distribution.
    00025  *
    00026  * 3. The name of Atmel may not be used to endorse or promote products derived
    00027  * from this software without specific prior written permission.
    00028  *
    00029  * 4. This software may only be redistributed and used in connection with an Atmel
    00030  * AVR product.
    00031  *
    00032  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
    00033  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    00034  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND
    00035  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
    00036  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    00037  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    00038  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    00039  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00040  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    00041  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00042  */
    00043
    00044 #include "config.h"
    00045 #include "time.h"
    00046
    00047
    00048 //******************************************************************************
    00049 // Variables
    00050 //******************************************************************************
    00051 unsigned long timeval[TIMERS];
    00052
    00053 // timer runs at 1 MHz and overflow will occur every 255 / 1 Mz ~= 0.25 ms
    00054 //#pragma vector = TIM0_OVF_vect
    00055
    00056
    00057 //******************************************************************************
    00058 // Functions
    00059 //******************************************************************************
    00067 #ifdef __GNUC__
    00068  ISR(TIMER0_COMPA_vect)
    00069 #else
    00070 #pragma vector = TIMER0_COMPA_vect
    00071 __interrupt void TICK_ISR(void)
    00072 #endif
    00073 {
    00074    unsigned char i;
    00075
    00076    // 1 ms has passed, decrement all non-zero timers.
    00077    for (i = 0; i < TIMERS; i++) {
    00078       if(timeval[i] > 0) {
    00079          timeval[i]--;
    00080       }
    00081    }
    00082 }
    00083
    00084
    00092 unsigned char Time_Left(unsigned char timer)
    00093 {
    00094    if(timeval[timer] > 0) {
    00095       return(TRUE);
    00096    } else {
    00097       return(FALSE);
    00098    }
    00099 }
    00100
    00101
    00109 void Time_Set(unsigned char timer, unsigned int min, unsigned char sec,
    00110               unsigned char ms)
    00111 {
    00112    timeval[timer] = 60000 * (unsigned long)min;
    00113    timeval[timer] += 1000 * (unsigned long)sec;
    00114    timeval[timer] += 1 * (unsigned long)ms;
    00115 }
    00116
    00117
    00122 void Time_Stop(void)
    00123 {
    00124    TCCR0B = 0;
    00125 }
    00126
    00127
    00132 void Time_Start(void)
    00133 {
    00134    TCNT0 = 0x00;//cgs added
    00135    TCCR0B = (0<<CS02)|(1<<CS01)|(1<<CS00);   // CLKT0 = CLK/64 = 125 kHz.
    00136 }
    00137
    00138
    00144 void Time_Init(void)
    00145 {
    00146    unsigned char i;
    00147
    00148    for (i = 0; i<<TIMERS; i++)   {
    00149       timeval[i] = 0;
    00150    }
    00151
    00152    OCR0A = 125;  // Will give a compare match every ms.
    00153
    00154    OCR0B = 0;  // Doesn't matter, will run in normal mode.
    00155
    00156    TCCR0A = (1<<WGM00);  // 8-bit CTC mode.
    00157
    00158    TCCR0B = (0<<CS02)|(1<<CS01)|(1<<CS00);   // CLKT0 = CLK/64 = 125 kHz.
    00159
    00160    TIMSK0 |= (1<<OCIE0A);  // Timer 0, Compare match A interrupt enabled.
    00161
    00162    // Enable interrupts, just in case they weren't already.
    00163 #ifdef __GNUC__
    00164    Enable_interrupt();
    00165 #else
    00166    __enable_interrupt();
    00167 #endif
    00168 }