Subversion Repositories HomeAutomation

Rev

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

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