Subversion Repositories HomeAutomation

Rev

Rev 127 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 127 Rev 149
Line 2... Line 2...
2
 * Timebase software module. Utilizes timer0 to provide a 32bit timebase with
2
 * Timebase software module. Utilizes timer0 to provide a 32bit timebase with
3
 * millisecond-granularity to the application.
3
 * millisecond-granularity to the application.
4
 *
-
 
5
 * @target  ATmega8
-
 
6
 *
4
 *
7
 * @author  Martin Thomas
5
 * @author  Martin Thomas
8
 * @author  Jimmy Myhrman
6
 * @author  Jimmy Myhrman
9
 *
7
 *
10
 * @date    2006-11-19
8
 * @date    2006-11-19
11
 */
9
 */
12
 
10
 
13
/*-----------------------------------------------------------------------------
11
/*-----------------------------------------------------------------------------
14
 * Includes
12
 * Includes
15
 *---------------------------------------------------------------------------*/
13
 *---------------------------------------------------------------------------*/
16
#include <inttypes.h>
14
#include <inttypes.h>
17
#include <avr/io.h>
15
#include <avr/io.h>
18
#include <avr/interrupt.h>
16
#include <avr/interrupt.h>
19
 
17
 
20
#include <timebase.h>
18
#include <timebase.h>
21
 
19
 
22
 
20
 
23
/*-----------------------------------------------------------------------------
21
/*-----------------------------------------------------------------------------
24
 * Globals
22
 * Globals
25
 *---------------------------------------------------------------------------*/
23
 *---------------------------------------------------------------------------*/
26
volatile uint32_t gMilliSecTick;
24
volatile uint32_t gMilliSecTick;
27
 
25
 
28
 
26
 
29
/*-----------------------------------------------------------------------------
27
/*-----------------------------------------------------------------------------
30
 * Interrupt Service Routines
28
 * Interrupt Service Routines
31
 *---------------------------------------------------------------------------*/
29
 *---------------------------------------------------------------------------*/
32
ISR(SIG_OVERFLOW0) {
30
ISR(SIG_OVERFLOW0) {
33
    TCNT0 = TIMEBASE_RELOAD;
31
    TCNT0 = TIMEBASE_RELOAD;
34
    gMilliSecTick++;
32
    gMilliSecTick++;
35
}
33
}
36
 
34
 
37
 
35
 
38
/*-----------------------------------------------------------------------------
36
/*-----------------------------------------------------------------------------
39
 * Public Functions
37
 * Public Functions
40
 *---------------------------------------------------------------------------*/
38
 *---------------------------------------------------------------------------*/
-
 
39
 
41
/**
40
/**
42
 * Initializes the timebase. The hardware timer interrupt source will be
41
 * Initializes the timebase. The hardware timer interrupt source will be
43
 * enabled, but global interrupts need to be enabled by the application.
42
 * enabled, but global interrupts need to be enabled by the application.
44
 */
43
 */
45
void Timebase_Init() {
44
void Timebase_Init() {
-
 
45
   
-
 
46
    #if defined(__AVR_ATmega8__)
46
    TCCR0 = (1<<CS01) | (1<<CS00); // prescaler: 64
47
    TCCR0 = (1<<CS01) | (1<<CS00); // prescaler: 64
47
    TCNT0 = TIMEBASE_RELOAD; // set initial reload-value
48
    TCNT0 = TIMEBASE_RELOAD; // set initial reload-value
48
    TIFR  |= (1<<TOV0);  // clear overflow int.
49
    TIFR  |= (1<<TOV0);  // clear overflow int.
49
    TIMSK |= (1<<TOIE0); // enable overflow-interrupt
50
    TIMSK |= (1<<TOIE0); // enable overflow-interrupt
-
 
51
    #endif
-
 
52
   
-
 
53
    #if defined(__AVR_ATmega88__)
-
 
54
    TCCR0A = (1<<CS01) | (1<<CS00); // prescaler: 64
-
 
55
    TCNT0 = TIMEBASE_RELOAD; // set initial reload-value
-
 
56
    TIFR0  |= (1<<TOV0);  // clear overflow int.
-
 
57
    TIMSK0 |= (1<<TOIE0); // enable overflow-interrupt
-
 
58
    #endif
50
}
59
}
-
 
60
 
51
 
61
 
52
/**
62
/**
53
 * Reads the current time.
63
 * Reads the current time.
54
 *
64
 *
55
 * @return
65
 * @return
56
 *      The current time in milliseconds (32bit value).
66
 *      The current time in milliseconds (32bit value).
Line 62... Line 72...
62
    cli();
72
    cli();
63
    res = gMilliSecTick;
73
    res = gMilliSecTick;
64
    SREG=sreg;
74
    SREG=sreg;
65
    return res;
75
    return res;
66
}
76
}
-
 
77
 
67
 
78
 
68
/**
79
/**
69
 * Checks how much time has passed since a given timestamp.
80
 * Checks how much time has passed since a given timestamp.
70
 *
81
 *
71
 * @param t0
82
 * @param t0