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
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 #pragma vector = TIM0_COMPA_vect
\r
58 __interrupt void TICK_ISR(void)
\r
62 // 1 ms has passed, decrement all non-zero timers.
\r
63 for (i = 0; i < TIMERS; i++) {
\r
64 if(timeval[i] > 0) {
\r
71 /*! \brief Checks if a specified timer has expired
\r
73 * \param timer Specifies timer
\r
75 * \retval TRUE Timer still going.
\r
76 * \retval FALSE Timer has expired.
\r
78 unsigned char Time_Left(unsigned char timer)
\r
80 if(timeval[timer] > 0) {
\r
88 /*! \brief Sets the specified timer
\r
90 * \param timer Specifies timer
\r
91 * \param min Minutes for timer to count down
\r
92 * \param sec Seconds for timer to count down
\r
93 * \param ms Milliseconds for timer to count down
\r
95 void Time_Set(unsigned char timer, unsigned int min, unsigned char sec,
\r
98 // timeval[i] = 4 * (1000*(sec + 60*min) + ms); // about 4000 ticks per second
\r
99 // timeval[i] = 240000 * (unsigned long)min;
\r
100 // timeval[i] += 4000 * (unsigned long)sec;
\r
101 // timeval[i] += 4 * (unsigned long)ms;
\r
103 timeval[timer] = 60000 * (unsigned long)min;
\r
104 timeval[timer] += 1000 * (unsigned long)sec;
\r
105 timeval[timer] += 1 * (unsigned long)ms;
\r
109 /*! \brief Stops timers
\r
111 * Sets timer0's clock source to none.
\r
113 void Time_Stop(void)
\r
119 /*! \brief Starts timers
\r
121 * Sets timer0's clock source to system clock divided by 64.
\r
123 void Time_Start(void)
\r
125 TCCR0B = (0<<CS02)|(1<<CS01)|(1<<CS00); // CLKT0 = CLK/64 = 125 kHz.
\r
129 /*! \brief Initializes timers
\r
131 * Resets all the timer values to 0, then sets up timer 0 for a compare match
\r
132 * every millisecond.
\r
134 void Time_Init(void)
\r
138 for (i = 0; i<<TIMERS; i++) {
\r
142 // OCR0A = 0; // Doesn't matter, will run in normal mode.
\r
144 OCR0A = 125; // Will give a compare match every ms.
\r
146 OCR0B = 0; // Doesn't matter, will run in normal mode.
\r
148 // TCCR0A = 0; // Normal 8-bit mode, no input capture.
\r
150 TCCR0A = (1<<WGM00); // 8-bit CTC mode.
\r
152 // TCCR0B = (0<<CS02)|(1<<CS01)|(0<<CS00); // CLKT0 = CLK/8 = 1 MHz.
\r
154 TCCR0B = (0<<CS02)|(1<<CS01)|(1<<CS00); // CLKT0 = CLK/64 = 125 kHz.
\r
156 // TIMSK |= (1<<TOIE0); // Overflow interrupt enabled.
\r
158 TIMSK |= (1<<OCIE0A); // Timer 0, Compare match A interrupt enabled.
\r
160 // Enable interrupts, just in case they weren't already.
\r
161 __enable_interrupt();
\r