Project

General

Profile

Statistics
| Revision:

root / branches / encoders / code / lib / src / libdragonfly / time.c @ 193

History | View | Annotate | Download (6.06 KB)

1
/*
2
time.c
3
anything that requires a delay
4
mostly delay_ms
5

6
author: Robotics Club, Colony Project
7

8
Change Log:
9
        2.5.07 - Kevin
10
                Aaron fixed the orb/servo code and made them use timer3 but compare registers B and C. He hard set the prescaler
11
                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.
12
                You now count how many 16ths of a second you want until you trigger your actual interrupt. Works. Changed defines
13
                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
14
                etc. Read that section in the time.h file for more information. Tested and works, though the clock drifts more than
15
                it used to
16
        1.30.07 - Kevin
17
                Modified the clock to run on timer3 on the Dragonfly. Works with decent accuracy. Using a prescaler of 256
18
        the timer counts up to a precomputer value which will trigger an interrupt and reset the timer. Multiples of
19
        256 change it by that multiple. Refer to the time.h file for all possible prescalers.
20
                The interrupt will call a specified function _rtc_func every pulse.
21
                All of it has been tested and it works.        
22
                
23
        10.29.07 - Andrew
24
                Modify to accept multiple functions with different time intervals
25
        
26

27
*/
28
#include <avr/interrupt.h>
29
#include <util/delay.h>
30
#include <time.h>
31
#include <serial.h>
32

    
33

    
34

    
35
static volatile int _rtc_val[MAX_FUNCTIONS];
36
static volatile int _rtc_pulse[MAX_FUNCTIONS];
37
static volatile int _rtc_scale[MAX_FUNCTIONS];
38
static int function_count = 0; //Number of functions
39
static void (*_rtc_f[MAX_FUNCTIONS])(void);
40

    
41
/**
42
 * @defgroup time Time
43
 * @brief Time functions
44
 * 
45
 * Functions dealing with time.
46
 * 
47
 * @{
48
 **/
49

    
50
/**
51
 * Delays for the specified number of milliseconds.
52
 * The accuracy of this function is unknown.
53
 *
54
 * @param ms the number of milliseconds to delay for
55
 **/
56
void delay_ms(int ms) 
57
{
58
        for(; ms > 15; ms-=15)
59
                _delay_ms(15);
60
        _delay_ms(ms);
61
}
62

    
63

    
64
/*         Prescales defined in time.h. SECOND will give you 1 second.
65
        More scales are defined in the time.h file.
66
        rtc_func is the address to a function that you want called every clock tick. */
67
/**
68
 * Initializes the real time clock. Prescales are defined in time.h.
69
 * For example, SECOND will give 1 second. The specified function is
70
 * called every clock tick. For the real time clock to activate,
71
 * interrupts must be enabled. (through sei() )
72
 *
73
 * @param prescale_opt the period with which the timer is triggered
74
 * @param rtc_func the function called when the timer is triggered
75
 *
76
 * @see rtc_get, rtc_reset
77
 **/
78
 
79
 int rtc_multi_init(int* prescale_opt,  void (*rtc_func[])(void), int argc) {
80
        
81
        //Check that the number of functions doesn't exceed alloted space
82
        if(argc > MAX_FUNCTIONS)
83
                return -1;
84
        
85
        function_count = argc;
86
        //Clear timer register for Timer 3
87
        TCNT3 = 0;
88
        
89
        /*         This sets the Waveform Generation Module to CTC (Clear Timer on Compare) Mode (100)
90
                See page135 in Atmega128 Docs for more modes and explanations */
91
        TCCR3B |= _BV(WGM32);
92
        
93
        /*         This sets the prescaler for the system clock (8MHz) ie: divides the clock by some number.
94
                Currently set to a prescaler of 8 because that is what the orb and servos use (they are on this timer as well)
95
                See page137 in Atemga128 Docs for all the available prescalers */
96
        TCCR3B |= _BV(CS31);
97
        
98
        /*         Sets the two regsiters that we compare against. So the timer counts up to this number and
99
                then resets back to 0 and calls the compare match interrupt.
100
                8x10^6 / 8 = 1/16 Second. All values are based off of this number. Do not change it unless you
101
                are l337*/
102
                
103
        OCR3A = 0xF424;        
104

    
105
        /*         Enable Output Compare A Interrupt. When OCR3A is met by the timer TCNT3 this interrupt will be
106
                triggerd. (See page140 in Atmega128 Docs for more information */
107
        ETIMSK |= _BV(OCIE3A);
108
        
109

    
110
        /* Set number of functions*/
111
        function_count = argc;
112
        
113
        /*        Store how many 1/16ths of a second you want to let by before triggering an interrupt */
114
        
115
        for (int i = 0; i <argc; i++) {
116
                _rtc_pulse[i] = 0;
117
                _rtc_val[i] = 0;
118
                _rtc_f[i] = rtc_func[i];
119
                /*        Store how many 1/16ths of a second you want to let by before triggering an interrupt */
120
                _rtc_scale[i] = prescale_opt[i];
121
        }
122
        return argc-1;
123
}
124

    
125
int add_function(int prescale_opt, void *rtc_func (void))
126
{
127
        if(function_count+1>=MAX_FUNCTIONS)
128
                return -1;
129
        function_count++;
130
        _rtc_f[function_count] = rtc_func;
131
        _rtc_pulse[function_count] = 0;
132
        _rtc_scale[function_count]  = prescale_opt;
133
        return 1;
134
}
135

    
136
int rtc_init(int prescale_opt, void *rtc_func(void)){
137
        int temp[] = {prescale_opt};
138
        void (*temp2[MAX_FUNCTIONS])(void) = {rtc_func};
139
        return rtc_multi_init(temp, temp2, 1);
140
}
141

    
142
/**
143
 * Returns the time elapsed in seconds since the last call to
144
 * rtc_init or rtc_reset.
145
 *
146
 * @return the number of seconds since the last call to rtc_init or rtc_reset
147
 *
148
 * @see rtc_init, rtc_reset
149
 **/
150
 
151
int rtc_get(void){
152
        return _rtc_val[0];
153
}
154

    
155
int rtc_get_pos(int pos){
156
        return _rtc_val[pos];
157
}
158

    
159
/**
160
 * Resets the real time clock counter to 0.
161
 *
162
 * @see rtc_init, rtc_get
163
 **/
164
 
165
void rtc_reset(void){
166
        _rtc_val[0] = 0;
167
}
168

    
169
void rtc_reset_all(void)
170
{
171
        for(int i =0; i <MAX_FUNCTIONS; i++)
172
                _rtc_val[i] = 0;
173
}
174

    
175
/** @} **/ //end defgroup
176

    
177
/*        Called every pulse. Function in _rtc_f is called every _rtc_scale and also the counter is updated.
178
        Bascially, since the pulse is hard set at 1/16s  you want to count how many 16ths of a second have passed
179
        and when it reaches the amount of time you want, execute the code. */
180

    
181
SIGNAL(TIMER3_COMPA_vect) {
182
        for(int functionNumber = 0; functionNumber < function_count; functionNumber++)
183
        {
184
                if (_rtc_pulse[functionNumber] ==  _rtc_scale[functionNumber]) {
185
                        //Increment the real time clock counter
186
                        _rtc_val[functionNumber]++;
187
                        
188
                        //Calls the function tied to the real time clock if defined
189
                        _rtc_f[functionNumber]();
190
                        
191
                        //Resets the pulse until the next scale is matched
192
                        _rtc_pulse[functionNumber] = 0;
193
                }        
194
                _rtc_pulse[functionNumber]++;
195
        }
196
        //Updates the amount of pulses seen since the last scale match
197
        
198
}