1 /* This file has been prepared for Doxygen automatic documentation generation.*/
\r
2 /*! \file *********************************************************************
\r
5 * Functions for timing
\r
7 * Contains functions to initialize, set, poll and stop timers.
\r
9 * \par Application note:
\r
10 * AVR458: Charging Li-Ion Batteries with BC100 \n
\r
11 * AVR463: Charging NiMH Batteries with BC100
\r
13 * \par Documentation
\r
14 * For comprehensive code documentation, supported compilers, compiler
\r
15 * settings and supported devices see readme.html
\r
18 * Atmel Corporation: http://www.atmel.com \n
\r
19 * Support email: avr@atmel.com
\r
25 * $URL: http://svn.norway.atmel.com/AppsAVR8/avr458_Charging_Li-Ion_Batteries_with_BC100/tag/20070904_release_1.0/code/IAR/time.c $
\r
26 * $Date: 2007-08-23 12:55:51 +0200 (to, 23 aug 2007) $\n
\r
27 ******************************************************************************/
\r
30 #include <avr/interrupt.h>
\r
38 //******************************************************************************
\r
40 //******************************************************************************
\r
41 unsigned long timeval[TIMERS]; //!< Contains the values for each timer.
\r
43 // timer runs at 1 MHz and overflow will occur every 255 / 1 Mz ~= 0.25 ms
\r
44 //#pragma vector = TIM0_OVF_vect
\r
47 //******************************************************************************
\r
49 //******************************************************************************
\r
50 /*! \brief Interrupt service routine for timer 0 overflow
\r
52 * Timer 0 runs at 125 kHz and compare match will occur every millisecond
\r
53 * (125 / 125 kHz = 1.0 ms), which will result in a call to this function.
\r
54 * When called, this function will decrement the time left for each timer,
\r
55 * unless they are already at zero.
\r
57 ISR(TIMER0_COMPA_vect)
\r
61 // 1 ms has passed, decrement all non-zero timers.
\r
62 for (i = 0; i < TIMERS; i++) {
\r
63 if(timeval[i] > 0) {
\r
70 /*! \brief Checks if a specified timer has expired
\r
72 * \param timer Specifies timer
\r
74 * \retval TRUE Timer still going.
\r
75 * \retval FALSE Timer has expired.
\r
77 unsigned char Time_Left(unsigned char timer)
\r
79 if(timeval[timer] > 0) {
\r
87 /*! \brief Sets the specified timer
\r
89 * \param timer Specifies timer
\r
90 * \param min Minutes for timer to count down
\r
91 * \param sec Seconds for timer to count down
\r
92 * \param ms Milliseconds for timer to count down
\r
94 void Time_Set(unsigned char timer, unsigned int min, unsigned char sec,
\r
97 // timeval[i] = 4 * (1000*(sec + 60*min) + ms); // about 4000 ticks per second
\r
98 // timeval[i] = 240000 * (unsigned long)min;
\r
99 // timeval[i] += 4000 * (unsigned long)sec;
\r
100 // timeval[i] += 4 * (unsigned long)ms;
\r
102 timeval[timer] = 60000 * (unsigned long)min;
\r
103 timeval[timer] += 1000 * (unsigned long)sec;
\r
104 timeval[timer] += 1 * (unsigned long)ms;
\r
108 /*! \brief Stops timers
\r
110 * Sets timer0's clock source to none.
\r
112 void Time_Stop(void)
\r
118 /*! \brief Starts timers
\r
120 * Sets timer0's clock source to system clock divided by 64.
\r
122 void Time_Start(void)
\r
124 TCCR0B = (0<<CS02)|(1<<CS01)|(1<<CS00); // CLKT0 = CLK/64 = 125 kHz.
\r
128 /*! \brief Initializes timers
\r
130 * Resets all the timer values to 0, then sets up timer 0 for a compare match
\r
131 * every millisecond.
\r
133 void Time_Init(void)
\r
137 for (i = 0; i<<TIMERS; i++) {
\r
141 // OCR0A = 0; // Doesn't matter, will run in normal mode.
\r
143 OCR0A = 125; // Will give a compare match every ms.
\r
145 OCR0B = 0; // Doesn't matter, will run in normal mode.
\r
147 // TCCR0A = 0; // Normal 8-bit mode, no input capture.
\r
149 TCCR0A = (1<<WGM00); // 8-bit CTC mode.
\r
151 // TCCR0B = (0<<CS02)|(1<<CS01)|(0<<CS00); // CLKT0 = CLK/8 = 1 MHz.
\r
153 TCCR0B = (0<<CS02)|(1<<CS01)|(1<<CS00); // CLKT0 = CLK/64 = 125 kHz.
\r
155 // TIMSK |= (1<<TOIE0); // Overflow interrupt enabled.
\r
157 TIMSK |= (1<<OCIE0A); // Timer 0, Compare match A interrupt enabled.
\r
159 // Enable interrupts, just in case they weren't already.
\r