2008-03-14 Martin Thomas <mthomas@rhrk.uni-kl.de>
[avr_bc100.git] / BaseTinyFirmware / GCC / 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 <avr/io.h>\r
30 #include <avr/interrupt.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 volatile 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 ISR(TIMER0_COMPA_vect)\r
58 {\r
59         unsigned char i;\r
60 \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
64                         timeval[i]--;\r
65                 }\r
66         }\r
67 }\r
68 \r
69 \r
70 /*! \brief Checks if a specified timer has expired\r
71  *\r
72  * \param timer Specifies timer\r
73  *\r
74  * \retval TRUE Timer still going.\r
75  * \retval FALSE Timer has expired.\r
76  */ \r
77 unsigned char Time_Left(unsigned char timer)\r
78 {\r
79         unsigned long tval;\r
80         unsigned char sreg_saved;\r
81 \r
82         sreg_saved = SREG;\r
83         cli();\r
84         tval = timeval[timer];\r
85         SREG = sreg_saved;\r
86 \r
87         if( tval > 0 ) {\r
88                 return(TRUE);\r
89         } else {\r
90                 return(FALSE);\r
91         }\r
92 }\r
93 \r
94 \r
95 /*! \brief Sets the specified timer\r
96  *\r
97  * \param timer Specifies timer\r
98  * \param min Minutes for timer to count down\r
99  * \param sec Seconds for timer to count down\r
100  * \param ms Milliseconds for timer to count down\r
101  */\r
102 void Time_Set(unsigned char timer, unsigned int min, unsigned char sec,\r
103                                   unsigned char ms)\r
104 {\r
105 //      timeval[i] = 4 * (1000*(sec + 60*min) + ms);   // about 4000 ticks per second\r
106 //      timeval[i] = 240000 * (unsigned long)min;\r
107 //      timeval[i] += 4000 * (unsigned long)sec;\r
108 //      timeval[i] += 4 * (unsigned long)ms;\r
109 \r
110         unsigned long tval;\r
111         unsigned char sreg_saved;\r
112         \r
113 \r
114         tval  = 60000 * (unsigned long)min;\r
115         tval += 1000 * (unsigned long)sec;\r
116         tval += 1 * (unsigned long)ms;\r
117 \r
118         sreg_saved = SREG;\r
119         cli();\r
120         timeval[timer] = tval;\r
121         SREG = sreg_saved;\r
122 }\r
123 \r
124 \r
125 /*! \brief Stops timers\r
126  *\r
127  * Sets timer0's clock source to none.\r
128  */\r
129 void Time_Stop(void)\r
130 {\r
131         TCCR0B = 0;\r
132 }\r
133 \r
134 \r
135 /*! \brief Starts timers\r
136  *\r
137  * Sets timer0's clock source to system clock divided by 64.\r
138  */\r
139 void Time_Start(void)\r
140 {\r
141         TCCR0B = (0<<CS02)|(1<<CS01)|(1<<CS00);         // CLKT0 = CLK/64 = 125 kHz.\r
142 }\r
143 \r
144 \r
145 /*! \brief Initializes timers\r
146  *\r
147  * Resets all the timer values to 0, then sets up timer 0 for a compare match\r
148  * every millisecond.\r
149  */\r
150 void Time_Init(void)\r
151 {\r
152         unsigned char i;\r
153         unsigned char sreg_saved;\r
154         \r
155         sreg_saved = SREG;\r
156         cli();\r
157         for (i = 0; i<<TIMERS; i++)     {\r
158                 timeval[i] = 0;\r
159         }\r
160         SREG = sreg_saved;\r
161 \r
162         //    OCR0A = 0;  // Doesn't matter, will run in normal mode.\r
163         \r
164         OCR0A = 125;  // Will give a compare match every ms.\r
165         \r
166         OCR0B = 0;  // Doesn't matter, will run in normal mode.\r
167 \r
168                 //    TCCR0A = 0;  // Normal 8-bit mode, no input capture.\r
169 \r
170         TCCR0A = (1<<WGM00);  // 8-bit CTC mode.\r
171         \r
172         //    TCCR0B = (0<<CS02)|(1<<CS01)|(0<<CS00);  // CLKT0 = CLK/8 = 1 MHz.\r
173         \r
174         TCCR0B = (0<<CS02)|(1<<CS01)|(1<<CS00);         // CLKT0 = CLK/64 = 125 kHz.\r
175 \r
176         //    TIMSK |= (1<<TOIE0);      // Overflow interrupt enabled.\r
177 \r
178         TIMSK |= (1<<OCIE0A);  // Timer 0, Compare match A interrupt enabled.\r
179 }\r