Initial import
[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 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         if(timeval[timer] > 0) {\r
80                 return(TRUE);\r
81         } else {\r
82                 return(FALSE);\r
83         }\r
84 }\r
85 \r
86 \r
87 /*! \brief Sets the specified timer\r
88  *\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
93  */\r
94 void Time_Set(unsigned char timer, unsigned int min, unsigned char sec,\r
95                                   unsigned char ms)\r
96 {\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
101 \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
105 }\r
106 \r
107 \r
108 /*! \brief Stops timers\r
109  *\r
110  * Sets timer0's clock source to none.\r
111  */\r
112 void Time_Stop(void)\r
113 {\r
114         TCCR0B = 0;\r
115 }\r
116 \r
117 \r
118 /*! \brief Starts timers\r
119  *\r
120  * Sets timer0's clock source to system clock divided by 64.\r
121  */\r
122 void Time_Start(void)\r
123 {\r
124         TCCR0B = (0<<CS02)|(1<<CS01)|(1<<CS00);         // CLKT0 = CLK/64 = 125 kHz.\r
125 }\r
126 \r
127 \r
128 /*! \brief Initializes timers\r
129  *\r
130  * Resets all the timer values to 0, then sets up timer 0 for a compare match\r
131  * every millisecond.\r
132  */\r
133 void Time_Init(void)\r
134 {\r
135         unsigned char i;\r
136         \r
137         for (i = 0; i<<TIMERS; i++)     {\r
138                 timeval[i] = 0;\r
139         }\r
140 \r
141         //    OCR0A = 0;  // Doesn't matter, will run in normal mode.\r
142         \r
143         OCR0A = 125;  // Will give a compare match every ms.\r
144         \r
145         OCR0B = 0;  // Doesn't matter, will run in normal mode.\r
146 \r
147                 //    TCCR0A = 0;  // Normal 8-bit mode, no input capture.\r
148 \r
149         TCCR0A = (1<<WGM00);  // 8-bit CTC mode.\r
150         \r
151         //    TCCR0B = (0<<CS02)|(1<<CS01)|(0<<CS00);  // CLKT0 = CLK/8 = 1 MHz.\r
152         \r
153         TCCR0B = (0<<CS02)|(1<<CS01)|(1<<CS00);         // CLKT0 = CLK/64 = 125 kHz.\r
154 \r
155         //    TIMSK |= (1<<TOIE0);      // Overflow interrupt enabled.\r
156 \r
157         TIMSK |= (1<<OCIE0A);  // Timer 0, Compare match A interrupt enabled.\r
158 \r
159         // Enable interrupts, just in case they weren't already.\r
160         sei();       \r
161 }\r