Initial import
[avr_bc100.git] / BaseTinyFirmware / IAR / time.c
1 /* This file has been prepared for Doxygen automatic documentation generation.*/\r
2 /*! \file *********************************************************************\r
3  *\r
4  * \brief\r
5  *      Functions for timing\r
6  *\r
7  *      Contains functions to initialize, set, poll and stop timers.\r
8  *\r
9  * \par Application note:\r
10  *      AVR458: Charging Li-Ion Batteries with BC100 \n\r
11  *      AVR463: Charging NiMH Batteries with BC100\r
12  *\r
13  * \par Documentation\r
14  *      For comprehensive code documentation, supported compilers, compiler \r
15  *      settings and supported devices see readme.html\r
16  *\r
17  * \author\r
18  *      Atmel Corporation: http://www.atmel.com \n\r
19  *      Support email: avr@atmel.com\r
20  *\r
21  * \r
22  * $Name$\r
23  * $Revision: 2299 $\r
24  * $RCSfile$\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
28 \r
29 #include <ioavr.h>\r
30 #include <inavr.h>\r
31 \r
32 #include "enums.h"\r
33 \r
34 #include "main.h"\r
35 #include "time.h"\r
36 \r
37 \r
38 //******************************************************************************\r
39 // Variables\r
40 //******************************************************************************\r
41 unsigned long timeval[TIMERS];  //!< Contains the values for each timer.\r
42 \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
45 \r
46 \r
47 //******************************************************************************\r
48 // Functions\r
49 //******************************************************************************\r
50 /*! \brief Interrupt service routine for timer 0 overflow\r
51  *\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
56  */\r
57 #pragma vector = TIM0_COMPA_vect\r
58 __interrupt void TICK_ISR(void)\r
59 {\r
60         unsigned char i;\r
61 \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
65                         timeval[i]--;\r
66                 }\r
67         }\r
68 }\r
69 \r
70 \r
71 /*! \brief Checks if a specified timer has expired\r
72  *\r
73  * \param timer Specifies timer\r
74  *\r
75  * \retval TRUE Timer still going.\r
76  * \retval FALSE Timer has expired.\r
77  */ \r
78 unsigned char Time_Left(unsigned char timer)\r
79 {\r
80         if(timeval[timer] > 0) {\r
81                 return(TRUE);\r
82         } else {\r
83                 return(FALSE);\r
84         }\r
85 }\r
86 \r
87 \r
88 /*! \brief Sets the specified timer\r
89  *\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
94  */\r
95 void Time_Set(unsigned char timer, unsigned int min, unsigned char sec,\r
96                                   unsigned char ms)\r
97 {\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
102 \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
106 }\r
107 \r
108 \r
109 /*! \brief Stops timers\r
110  *\r
111  * Sets timer0's clock source to none.\r
112  */\r
113 void Time_Stop(void)\r
114 {\r
115         TCCR0B = 0;\r
116 }\r
117 \r
118 \r
119 /*! \brief Starts timers\r
120  *\r
121  * Sets timer0's clock source to system clock divided by 64.\r
122  */\r
123 void Time_Start(void)\r
124 {\r
125         TCCR0B = (0<<CS02)|(1<<CS01)|(1<<CS00);         // CLKT0 = CLK/64 = 125 kHz.\r
126 }\r
127 \r
128 \r
129 /*! \brief Initializes timers\r
130  *\r
131  * Resets all the timer values to 0, then sets up timer 0 for a compare match\r
132  * every millisecond.\r
133  */\r
134 void Time_Init(void)\r
135 {\r
136         unsigned char i;\r
137         \r
138         for (i = 0; i<<TIMERS; i++)     {\r
139                 timeval[i] = 0;\r
140         }\r
141 \r
142         //    OCR0A = 0;  // Doesn't matter, will run in normal mode.\r
143         \r
144         OCR0A = 125;  // Will give a compare match every ms.\r
145         \r
146         OCR0B = 0;  // Doesn't matter, will run in normal mode.\r
147 \r
148                 //    TCCR0A = 0;  // Normal 8-bit mode, no input capture.\r
149 \r
150         TCCR0A = (1<<WGM00);  // 8-bit CTC mode.\r
151         \r
152         //    TCCR0B = (0<<CS02)|(1<<CS01)|(0<<CS00);  // CLKT0 = CLK/8 = 1 MHz.\r
153         \r
154         TCCR0B = (0<<CS02)|(1<<CS01)|(1<<CS00);         // CLKT0 = CLK/64 = 125 kHz.\r
155 \r
156         //    TIMSK |= (1<<TOIE0);      // Overflow interrupt enabled.\r
157 \r
158         TIMSK |= (1<<OCIE0A);  // Timer 0, Compare match A interrupt enabled.\r
159 \r
160         // Enable interrupts, just in case they weren't already.\r
161         __enable_interrupt();       \r
162 }\r