root / branches / autonomous_recharging / code / projects / libbayboard / time.c @ 769
History | View | Annotate | Download (5.16 KB)
1 |
/**
|
---|---|
2 |
* Copyright (c) 2007 Colony Project
|
3 |
*
|
4 |
* Permission is hereby granted, free of charge, to any person
|
5 |
* obtaining a copy of this software and associated documentation
|
6 |
* files (the "Software"), to deal in the Software without
|
7 |
* restriction, including without limitation the rights to use,
|
8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
* copies of the Software, and to permit persons to whom the
|
10 |
* Software is furnished to do so, subject to the following
|
11 |
* conditions:
|
12 |
*
|
13 |
* The above copyright notice and this permission notice shall be
|
14 |
* included in all copies or substantial portions of the Software.
|
15 |
*
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
23 |
* OTHER DEALINGS IN THE SOFTWARE.
|
24 |
**/
|
25 |
|
26 |
|
27 |
/**
|
28 |
* @file time.c
|
29 |
* @brief Timer code
|
30 |
*
|
31 |
* Implementation of functions for timers.
|
32 |
*
|
33 |
* @author Colony Project, CMU Robotics Club
|
34 |
**/
|
35 |
|
36 |
/*
|
37 |
time.c
|
38 |
anything that requires a delay
|
39 |
mostly delay_ms
|
40 |
|
41 |
author: Robotics Club, Colony Project
|
42 |
|
43 |
Change Log:
|
44 |
4.7.08 - Austin
|
45 |
Modified for bay boards
|
46 |
2.5.07 - Kevin
|
47 |
Aaron fixed the orb/servo code and made them use timer3 but compare registers B and C. He hard set the prescaler
|
48 |
to 8 so the RTC broke. Changed it so that we use a 8 prescaler which sets the compare match at 1/16th of a second.
|
49 |
You now count how many 16ths of a second you want until you trigger your actual interrupt. Works. Changed defines
|
50 |
for time so you can still call rtc_init with a scale but now it is defined in terms of actual time like second, quarter_second
|
51 |
etc. Read that section in the time.h file for more information. Tested and works, though the clock drifts more than
|
52 |
it used to
|
53 |
1.30.07 - Kevin
|
54 |
Modified the clock to run on timer3 on the Dragonfly. Works with decent accuracy. Using a prescaler of 256
|
55 |
the timer counts up to a precomputer value which will trigger an interrupt and reset the timer. Multiples of
|
56 |
256 change it by that multiple. Refer to the time.h file for all possible prescalers.
|
57 |
The interrupt will call a specified function _rtc_func every pulse.
|
58 |
All of it has been tested and it works.
|
59 |
|
60 |
*/
|
61 |
#include <avr/interrupt.h> |
62 |
#include <util/delay.h> |
63 |
#include <time.h> |
64 |
#include <serial.h> |
65 |
|
66 |
static volatile int _rtc_val = 0; |
67 |
static volatile int _rtc_pulse = 0; |
68 |
static volatile int _rtc_scale = 32; //Defaults to 1 Second per pulse |
69 |
static void (*_rtc_f)(void) = 0; |
70 |
|
71 |
/**
|
72 |
* @defgroup time Time
|
73 |
* @brief Time functions
|
74 |
*
|
75 |
* Functions dealing with time.
|
76 |
*
|
77 |
* @{
|
78 |
**/
|
79 |
|
80 |
/**
|
81 |
* Delays for the specified number of milliseconds.
|
82 |
* The accuracy of this function is unknown.
|
83 |
*
|
84 |
* @param ms the number of milliseconds to delay for
|
85 |
**/
|
86 |
void delay_ms(int ms) |
87 |
{ |
88 |
for(; ms > 15; ms-=15) |
89 |
_delay_ms(15);
|
90 |
_delay_ms(ms); |
91 |
} |
92 |
|
93 |
|
94 |
/* Prescales defined in time.h. SECOND will give you 1 second.
|
95 |
More scales are defined in the time.h file.
|
96 |
rtc_func is the address to a function that you want called every clock tick. */
|
97 |
/**
|
98 |
* Initializes the real time clock. Prescales are defined in time.h.
|
99 |
* For example, SECOND will give 1 second. The specified function is
|
100 |
* called every clock tick. For the real time clock to activate,
|
101 |
* interrupts must be enabled. (through sei() )
|
102 |
*
|
103 |
* @param prescale_opt the period with which the timer is triggered
|
104 |
* @param rtc_func the function called when the timer is triggered
|
105 |
*
|
106 |
* @see rtc_get, rtc_reset
|
107 |
*
|
108 |
**/
|
109 |
void rtc_init(int prescale_opt, void (*rtc_func)(void)) { |
110 |
|
111 |
|
112 |
/* Interrupt on match to ICR1 */
|
113 |
TIMSK1 |= _BV(ICIE1); |
114 |
|
115 |
/* Store the pointer to the function to be used in the interrupt */
|
116 |
_rtc_f = rtc_func; |
117 |
|
118 |
/* Store how many 1/16ths of a second you want to let by before triggering an interrupt */
|
119 |
_rtc_scale = prescale_opt; |
120 |
} |
121 |
|
122 |
/**
|
123 |
* Returns the time elapsed in seconds since the last call to
|
124 |
* rtc_init or rtc_reset.
|
125 |
*
|
126 |
* @return the number of seconds since the last call to rtc_init or rtc_reset
|
127 |
*
|
128 |
* @see rtc_init, rtc_reset
|
129 |
**/
|
130 |
int rtc_get(void){ |
131 |
return _rtc_val;
|
132 |
} |
133 |
|
134 |
/**
|
135 |
* Resets the real time clock counter to 0.
|
136 |
*
|
137 |
* @see rtc_init, rtc_get
|
138 |
**/
|
139 |
void rtc_reset(void){ |
140 |
_rtc_val = 0;
|
141 |
} |
142 |
|
143 |
/** @} **/ //end defgroup |
144 |
|
145 |
/* Called every pulse. Function in _rtc_f is called every _rtc_scale and also the counter is updated.
|
146 |
Bascially, since the pulse is hard set at 1/16s you want to count how many 16ths of a second have passed
|
147 |
and when it reaches the amount of time you want, execute the code. */
|
148 |
ISR(TIMER1_CAPT_vect) { |
149 |
|
150 |
if (_rtc_pulse == _rtc_scale) {
|
151 |
//Increment the real time clock counter
|
152 |
_rtc_val++; |
153 |
|
154 |
//Calls the function tied to the real time clock if defined
|
155 |
if(_rtc_f != 0) |
156 |
_rtc_f(); |
157 |
|
158 |
//Resets the pulse until the next scale is matched
|
159 |
_rtc_pulse = 0;
|
160 |
} |
161 |
|
162 |
//Updates the amount of pulses seen since the last scale match
|
163 |
_rtc_pulse++; |
164 |
|
165 |
} |
166 |
|