Project

General

Profile

Statistics
| Revision:

root / trunk / code / lib / src / libdragonfly / time.c @ 7

History | View | Annotate | Download (4.79 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
*/
24
#include <avr/interrupt.h>
25
#include <util/delay.h>
26
#include <time.h>
27
#include <serial.h>
28

    
29
static volatile int _rtc_val = 0;
30
static volatile int _rtc_pulse = 0;
31
static volatile int _rtc_scale = 32;        //Defaults to 1 Second per pulse
32
static void (*_rtc_f)(void) = 0;
33

    
34
/**
35
 * @defgroup time Time
36
 * @brief Time functions
37
 * 
38
 * Functions dealing with time.
39
 * 
40
 * @{
41
 **/
42

    
43
/**
44
 * Delays for the specified number of milliseconds.
45
 * The accuracy of this function is unknown.
46
 *
47
 * @param ms the number of milliseconds to delay for
48
 **/
49
void delay_ms(int ms) 
50
{
51
        for(; ms > 15; ms-=15)
52
                _delay_ms(15);
53
        _delay_ms(ms);
54
}
55

    
56

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

    
93
        /*         Enable Output Compare A Interrupt. When OCR3A is met by the timer TCNT3 this interrupt will be
94
                triggerd. (See page140 in Atmega128 Docs for more information */
95
        ETIMSK |= _BV(OCIE3A);
96
        
97
        /*        Store the pointer to the function to be used in the interrupt */
98
        _rtc_f = rtc_func;
99
        
100
        /*        Store how many 1/16ths of a second you want to let by before triggering an interrupt */
101
        _rtc_scale = prescale_opt;
102
}
103

    
104
/**
105
 * Returns the time elapsed in seconds since the last call to
106
 * rtc_init or rtc_reset.
107
 *
108
 * @return the number of seconds since the last call to rtc_init or rtc_reset
109
 *
110
 * @see rtc_init, rtc_reset
111
 **/
112
int rtc_get(void){
113
        return _rtc_val;
114
}
115

    
116
/**
117
 * Resets the real time clock counter to 0.
118
 *
119
 * @see rtc_init, rtc_get
120
 **/
121
void rtc_reset(void){
122
        _rtc_val = 0;
123
}
124

    
125
/** @} **/ //end defgroup
126

    
127
/*        Called every pulse. Function in _rtc_f is called every _rtc_scale and also the counter is updated.
128
        Bascially, since the pulse is hard set at 1/16s  you want to count how many 16ths of a second have passed
129
        and when it reaches the amount of time you want, execute the code. */
130
SIGNAL(TIMER3_COMPA_vect) {
131

    
132
        if (_rtc_pulse ==  _rtc_scale) {
133
                //Increment the real time clock counter
134
                _rtc_val++;
135
                
136
                //Calls the function tied to the real time clock if defined
137
                if(_rtc_f != 0)
138
                        _rtc_f();
139
                
140
                //Resets the pulse until the next scale is matched
141
                _rtc_pulse = 0;
142
        }        
143
        
144
        //Updates the amount of pulses seen since the last scale match
145
        _rtc_pulse++;
146
        
147
}
148