Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

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