Subversion Repositories HomeAutomation

Rev

Rev 24 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 24 Rev 25
-
 
1
/*********************************************************************
-
 
2
 *
-
 
3
 *                  Ticker service
-
 
4
 *
-
 
5
 *********************************************************************
-
 
6
 * FileName:        Tick.c
-
 
7
 *
-
 
8
 * Author               Date    Comment
-
 
9
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
 
10
 * Johan Böhlin     21-10-06    Original        (Rev 1.0)
-
 
11
 ********************************************************************/
-
 
12
 
1
#define TICK_INCLUDE
13
#define TICK_INCLUDE
2
 
14
 
3
#include "..\Include\Tick.h"
15
#include "..\Include\Tick.h"
4
#include "..\Include\Compiler.h"
16
#include "..\Include\Compiler.h"
5
 
17
 
6
#define TICK_TEMP_VALUE_1 ((INSTR_FREQ) / (TICKS_PER_SECOND * TICK_PRESCALE_VALUE))
18
#define TICK_TEMP_VALUE_1 ((INSTR_FREQ) / (TICKS_PER_SECOND * TICK_PRESCALE_VALUE))
7
 
19
 
8
#if TICK_TEMP_VALUE_1 > 60000
20
#if TICK_TEMP_VALUE_1 > 60000
9
#error TICK_PER_SECOND value cannot be programmed with current CLOCK_FREQ
21
#error TICK_PER_SECOND value cannot be programmed with current CLOCK_FREQ
10
#error Either lower TICK_PER_SECOND or manually configure the Timer
22
#error Either lower TICK_PER_SECOND or manually configure the Timer
11
#endif
23
#endif
12
 
24
 
13
#define TICK_TEMP_VALUE         (65535 - TICK_TEMP_VALUE_1)
25
#define TICK_TEMP_VALUE         (65535 - TICK_TEMP_VALUE_1)
14
 
26
 
15
#define TICK_COUNTER_HIGH       ((TICK_TEMP_VALUE >> 8) & 0xff)
27
#define TICK_COUNTER_HIGH       ((TICK_TEMP_VALUE >> 8) & 0xff)
16
#define TICK_COUNTER_LOW        (TICK_TEMP_VALUE & 0xff)
28
#define TICK_COUNTER_LOW        (TICK_TEMP_VALUE & 0xff)
17
 
29
 
18
#if (TICK_PRESCALE_VALUE == 2)
30
#if (TICK_PRESCALE_VALUE == 2)
19
    #define TIMER_PRESCALE  (0)
31
    #define TIMER_PRESCALE  (0)
20
#elif ( TICK_PRESCALE_VALUE == 4 )
32
#elif ( TICK_PRESCALE_VALUE == 4 )
21
    #define TIMER_PRESCALE  (1)
33
    #define TIMER_PRESCALE  (1)
22
#elif ( TICK_PRESCALE_VALUE == 8 )
34
#elif ( TICK_PRESCALE_VALUE == 8 )
23
    #define TIMER_PRESCALE  (2)
35
    #define TIMER_PRESCALE  (2)
24
#elif ( TICK_PRESCALE_VALUE == 16 )
36
#elif ( TICK_PRESCALE_VALUE == 16 )
25
    #define TIMER_PRESCALE  (3)
37
    #define TIMER_PRESCALE  (3)
26
#elif ( TICK_PRESCALE_VALUE == 32 )
38
#elif ( TICK_PRESCALE_VALUE == 32 )
27
    #define TIMER_PRESCALE  (4)
39
    #define TIMER_PRESCALE  (4)
28
#elif ( TICK_PRESCALE_VALUE == 64 )
40
#elif ( TICK_PRESCALE_VALUE == 64 )
29
    #define TIMER_PRESCALE  (5)
41
    #define TIMER_PRESCALE  (5)
30
#elif ( TICK_PRESCALE_VALUE == 128 )
42
#elif ( TICK_PRESCALE_VALUE == 128 )
31
    #define TIMER_PRESCALE  (6)
43
    #define TIMER_PRESCALE  (6)
32
#elif ( TICK_PRESCALE_VALUE == 256 )
44
#elif ( TICK_PRESCALE_VALUE == 256 )
33
    #define TIMER_PRESCALE  (7)
45
    #define TIMER_PRESCALE  (7)
34
#else
46
#else
35
    #error Invalid TICK_PRESCALE_VALUE specified.
47
    #error Invalid TICK_PRESCALE_VALUE specified.
36
#endif
48
#endif
37
 
49
 
38
 
50
 
39
TICK TickCount = 0; // 10ms/unit
51
TICK TickCount = 0; // 10ms/unit
40
 
52
 
41
 
53
 
42
 
54
 
43
/*
55
/*
44
*   Function: TickInit
56
*   Function: TickInit
45
*
57
*
46
*   Input:  none
58
*   Input:  none
47
*   Output: Tick manager is initialized.
59
*   Output: Tick manager is initialized.
48
*   Pre-conditions: TickInit
60
*   Pre-conditions: TickInit
49
*   Affects: none
61
*   Affects: none
50
*   Depends: none
62
*   Depends: none
51
*/
63
*/
52
#define PERIOD (INSTR_FREQ/256/1000)*TICK_PERIOD_MS
64
#define PERIOD (INSTR_FREQ/256/1000)*TICK_PERIOD_MS
53
void tickInit(void)
65
void tickInit(void)
54
{
66
{
55
    // Start the timer.
67
    // Start the timer.
56
    TMR0L = TICK_COUNTER_LOW;
68
    TMR0L = TICK_COUNTER_LOW;
57
    TMR0H = TICK_COUNTER_HIGH;
69
    TMR0H = TICK_COUNTER_HIGH;
58
 
70
 
59
    // 16-BIT, internal timer, PSA set to 1:256
71
    // 16-BIT, internal timer, PSA set to 1:256
60
    T0CON = 0b00000000 | TIMER_PRESCALE;
72
    T0CON = 0b00000000 | TIMER_PRESCALE;
61
 
73
 
62
    // Start the timer.
74
    // Start the timer.
63
    T0CONbits.TMR0ON = 1;
75
    T0CONbits.TMR0ON = 1;
64
 
76
 
65
    INTCONbits.TMR0IF = 1;
77
    INTCONbits.TMR0IF = 1;
66
    INTCONbits.TMR0IE = 1;
78
    INTCONbits.TMR0IE = 1;
67
}
79
}
68
 
80
 
69
/*
81
/*
70
*   Function: TickGet
82
*   Function: TickGet
71
*
83
*
72
*   Input:  none
84
*   Input:  none
73
*   Output: Current tick value is given, 1 tick represents approximately 10ms
85
*   Output: Current tick value is given, 1 tick represents approximately 10ms
74
*   Pre-conditions: TickInit
86
*   Pre-conditions: TickInit
75
*   Affects: none
87
*   Affects: none
76
*   Depends: none
88
*   Depends: none
77
*/
89
*/
78
TICK tickGet(void)
90
TICK tickGet(void)
79
{
91
{
80
    return TickCount;
92
    return TickCount;
81
}
93
}
82
 
94
 
83
 
95
 
84
/*
96
/*
85
*   Function: tickUpdate
97
*   Function: tickUpdate
86
*
98
*
87
*   Input:  none
99
*   Input:  none
88
*   Output: none
100
*   Output: none
89
*   Pre-conditions: TickInit
101
*   Pre-conditions: TickInit
90
*   Affects: none
102
*   Affects: none
91
*   Depends: none
103
*   Depends: none
92
*/
104
*/
93
void tickUpdate(void)
105
void tickUpdate(void)
94
{
106
{
95
    if(INTCONbits.TMR0IF)
107
    if(INTCONbits.TMR0IF)
96
    {
108
    {
97
        TMR0H = TICK_COUNTER_HIGH;
109
        TMR0H = TICK_COUNTER_HIGH;
98
        TMR0L = TICK_COUNTER_LOW;
110
        TMR0L = TICK_COUNTER_LOW;
99
 
111
 
100
        TickCount++;
112
        TickCount++;
101
 
113
 
102
        INTCONbits.TMR0IF = 0;
114
        INTCONbits.TMR0IF = 0;
103
    }
115
    }
104
}
116
}
105
 
117