Revision 437
Cleaned up analog.c code. Recompiled library. Mostly commenting and
documentation fixes. No changes made to actual code.
time.c | ||
---|---|---|
34 | 34 |
**/ |
35 | 35 |
|
36 | 36 |
/* |
37 |
time.c |
|
38 |
anything that requires a delay |
|
39 |
mostly delay_ms |
|
37 |
time.c
|
|
38 |
anything that requires a delay
|
|
39 |
mostly delay_ms
|
|
40 | 40 |
|
41 |
author: Robotics Club, Colony Project |
|
41 |
author: Robotics Club, Colony Project
|
|
42 | 42 |
|
43 |
Change Log: |
|
44 |
2.5.07 - Kevin
|
|
45 |
Aaron fixed the orb/servo code and made them use timer3 but compare registers B and C. He hard set the prescaler
|
|
46 |
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.
|
|
47 |
You now count how many 16ths of a second you want until you trigger your actual interrupt. Works. Changed defines
|
|
48 |
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
|
|
49 |
etc. Read that section in the time.h file for more information. Tested and works, though the clock drifts more than
|
|
50 |
it used to
|
|
51 |
1.30.07 - Kevin
|
|
52 |
Modified the clock to run on timer3 on the Dragonfly. Works with decent accuracy. Using a prescaler of 256
|
|
53 |
the timer counts up to a precomputer value which will trigger an interrupt and reset the timer. Multiples of
|
|
54 |
256 change it by that multiple. Refer to the time.h file for all possible prescalers.
|
|
55 |
The interrupt will call a specified function _rtc_func every pulse.
|
|
56 |
All of it has been tested and it works.
|
|
43 |
Change Log:
|
|
44 |
2.5.07 - Kevin
|
|
45 |
Aaron fixed the orb/servo code and made them use timer3 but compare registers B and C. He hard set the prescaler
|
|
46 |
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.
|
|
47 |
You now count how many 16ths of a second you want until you trigger your actual interrupt. Works. Changed defines
|
|
48 |
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
|
|
49 |
etc. Read that section in the time.h file for more information. Tested and works, though the clock drifts more than
|
|
50 |
it used to
|
|
51 |
1.30.07 - Kevin
|
|
52 |
Modified the clock to run on timer3 on the Dragonfly. Works with decent accuracy. Using a prescaler of 256
|
|
53 |
the timer counts up to a precomputer value which will trigger an interrupt and reset the timer. Multiples of
|
|
54 |
256 change it by that multiple. Refer to the time.h file for all possible prescalers.
|
|
55 |
The interrupt will call a specified function _rtc_func every pulse.
|
|
56 |
All of it has been tested and it works.
|
|
57 | 57 |
|
58 | 58 |
*/ |
59 | 59 |
#include <avr/interrupt.h> |
... | ... | |
81 | 81 |
* |
82 | 82 |
* @param ms the number of milliseconds to delay for |
83 | 83 |
**/ |
84 |
void delay_ms(int ms) |
|
85 |
{ |
|
86 |
for(; ms > 15; ms-=15) |
|
87 |
_delay_ms(15); |
|
88 |
_delay_ms(ms); |
|
84 |
void delay_ms(int ms) { |
|
85 |
for(; ms > 15; ms-=15) |
|
86 |
_delay_ms(15); |
|
87 |
_delay_ms(ms); |
|
89 | 88 |
} |
90 | 89 |
|
91 |
|
|
92 | 90 |
/* Prescales defined in time.h. SECOND will give you 1 second. |
93 | 91 |
More scales are defined in the time.h file. |
94 | 92 |
rtc_func is the address to a function that you want called every clock tick. */ |
... | ... | |
106 | 104 |
**/ |
107 | 105 |
void rtc_init(int prescale_opt, void (*rtc_func)(void)) { |
108 | 106 |
|
109 |
//Clear timer register for Timer 3
|
|
110 |
TCNT3 = 0;
|
|
107 |
//Clear timer register for Timer 3
|
|
108 |
TCNT3 = 0;
|
|
111 | 109 |
|
112 |
/* This sets the Waveform Generation Module to CTC (Clear Timer on Compare) Mode (100)
|
|
113 |
See page135 in Atmega128 Docs for more modes and explanations */
|
|
114 |
TCCR3B |= _BV(WGM32);
|
|
110 |
/* This sets the Waveform Generation Module to CTC (Clear Timer on Compare) Mode (100)
|
|
111 |
See page135 in Atmega128 Docs for more modes and explanations */ |
|
112 |
TCCR3B |= _BV(WGM32);
|
|
115 | 113 |
|
116 |
/* This sets the prescaler for the system clock (8MHz) ie: divides the clock by some number.
|
|
117 |
Currently set to a prescaler of 8 because that is what the orb and servos use (they are on this timer as well)
|
|
118 |
See page137 in Atemga128 Docs for all the available prescalers */
|
|
119 |
TCCR3B |= _BV(CS31);
|
|
114 |
/* This sets the prescaler for the system clock (8MHz) ie: divides the clock by some number.
|
|
115 |
Currently set to a prescaler of 8 because that is what the orb and servos use (they are on this timer as well) |
|
116 |
See page137 in Atemga128 Docs for all the available prescalers */ |
|
117 |
TCCR3B |= _BV(CS31);
|
|
120 | 118 |
|
121 |
/* Sets the two regsiters that we compare against. So the timer counts up to this number and
|
|
122 |
then resets back to 0 and calls the compare match interrupt.
|
|
123 |
8x10^6 / 8 = 1/16 Second. All values are based off of this number. Do not change it unless you
|
|
124 |
are l337*/
|
|
119 |
/* Sets the two regsiters that we compare against. So the timer counts up to this number and
|
|
120 |
then resets back to 0 and calls the compare match interrupt. |
|
121 |
8x10^6 / 8 = 1/16 Second. All values are based off of this number. Do not change it unless you |
|
122 |
are l337*/ |
|
125 | 123 |
|
126 |
OCR3A = 0xF424;
|
|
124 |
OCR3A = 0xF424;
|
|
127 | 125 |
|
128 |
/* Enable Output Compare A Interrupt. When OCR3A is met by the timer TCNT3 this interrupt will be
|
|
129 |
triggerd. (See page140 in Atmega128 Docs for more information */
|
|
130 |
ETIMSK |= _BV(OCIE3A);
|
|
126 |
/* Enable Output Compare A Interrupt. When OCR3A is met by the timer TCNT3 this interrupt will be
|
|
127 |
triggerd. (See page140 in Atmega128 Docs for more information */ |
|
128 |
ETIMSK |= _BV(OCIE3A);
|
|
131 | 129 |
|
132 |
/* Store the pointer to the function to be used in the interrupt */
|
|
133 |
_rtc_f = rtc_func;
|
|
130 |
/* Store the pointer to the function to be used in the interrupt */
|
|
131 |
_rtc_f = rtc_func;
|
|
134 | 132 |
|
135 |
/* Store how many 1/16ths of a second you want to let by before triggering an interrupt */
|
|
136 |
_rtc_scale = prescale_opt;
|
|
133 |
/* Store how many 1/16ths of a second you want to let by before triggering an interrupt */
|
|
134 |
_rtc_scale = prescale_opt;
|
|
137 | 135 |
} |
138 | 136 |
|
139 | 137 |
/** |
... | ... | |
144 | 142 |
* |
145 | 143 |
* @see rtc_init, rtc_reset |
146 | 144 |
**/ |
147 |
int rtc_get(void){ |
|
148 |
return _rtc_val;
|
|
145 |
int rtc_get(void) {
|
|
146 |
return _rtc_val;
|
|
149 | 147 |
} |
150 | 148 |
|
151 | 149 |
/** |
... | ... | |
153 | 151 |
* |
154 | 152 |
* @see rtc_init, rtc_get |
155 | 153 |
**/ |
156 |
void rtc_reset(void){ |
|
157 |
_rtc_val = 0;
|
|
154 |
void rtc_reset(void) {
|
|
155 |
_rtc_val = 0;
|
|
158 | 156 |
} |
159 | 157 |
|
160 | 158 |
/** @} **/ //end defgroup |
... | ... | |
164 | 162 |
and when it reaches the amount of time you want, execute the code. */ |
165 | 163 |
SIGNAL(TIMER3_COMPA_vect) { |
166 | 164 |
|
167 |
if (_rtc_pulse == _rtc_scale) {
|
|
168 |
//Increment the real time clock counter
|
|
169 |
_rtc_val++;
|
|
165 |
if (_rtc_pulse == _rtc_scale) {
|
|
166 |
//Increment the real time clock counter
|
|
167 |
_rtc_val++;
|
|
170 | 168 |
|
171 |
//Calls the function tied to the real time clock if defined
|
|
172 |
if(_rtc_f != 0)
|
|
173 |
_rtc_f();
|
|
169 |
//Calls the function tied to the real time clock if defined
|
|
170 |
if(_rtc_f != 0)
|
|
171 |
_rtc_f();
|
|
174 | 172 |
|
175 |
//Resets the pulse until the next scale is matched
|
|
176 |
_rtc_pulse = 0;
|
|
177 |
}
|
|
173 |
//Resets the pulse until the next scale is matched
|
|
174 |
_rtc_pulse = 0;
|
|
175 |
}
|
|
178 | 176 |
|
179 |
//Updates the amount of pulses seen since the last scale match
|
|
180 |
_rtc_pulse++;
|
|
177 |
//Updates the amount of pulses seen since the last scale match
|
|
178 |
_rtc_pulse++;
|
|
181 | 179 |
|
182 | 180 |
} |
183 |
|
Also available in: Unified diff