Subversion Repositories HomeAutomation

Rev

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

Rev 86 Rev 353
Line 2... Line 2...
2
 *
2
 *
3
 *                  Ticker module service
3
 *                  Ticker module service
4
 *
4
 *
5
 *********************************************************************
5
 *********************************************************************
6
 * FileName:        $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canBase/Source/Tick.c $
6
 * FileName:        $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canBase/Source/Tick.c $
7
 * Last changed:    $LastChangedDate: 2006-11-14 10:55:38 +0100 (Tue, 14 Nov 2006) $
7
 * Last changed:    $LastChangedDate: 2007-03-03 19:07:14 +0100 (Sat, 03 Mar 2007) $
8
 * By:              $LastChangedBy: johboh $
8
 * By:              $LastChangedBy: johboh $
9
 * Revision:        $Revision: 86 $
9
 * Revision:        $Revision: 353 $
10
 ********************************************************************/
10
 ********************************************************************/
11
 
11
 
12
#define TICK_INCLUDE
12
#define TICK_INCLUDE
13
 
13
 
14
#include <Tick.h>
14
#include <Tick.h>
15
#include <Compiler.h>
-
 
16
 
15
 
17
#define TICK_TEMP_VALUE_1 ((INSTR_FREQ) / (TICKS_PER_SECOND * TICK_PRESCALE_VALUE))
-
 
18
 
-
 
19
#if TICK_TEMP_VALUE_1 > 60000
-
 
20
#error TICK_PER_SECOND value cannot be programmed with current CLOCK_FREQ
-
 
21
#error Either lower TICK_PER_SECOND or manually configure the Timer
-
 
22
#endif
-
 
23
 
-
 
24
#define TICK_TEMP_VALUE         (65535 - TICK_TEMP_VALUE_1)
-
 
25
 
-
 
26
#define TICK_COUNTER_HIGH       ((TICK_TEMP_VALUE >> 8) & 0xff)
-
 
27
#define TICK_COUNTER_LOW        (TICK_TEMP_VALUE & 0xff)
-
 
28
 
-
 
29
#if (TICK_PRESCALE_VALUE == 2)
-
 
30
    #define TIMER_PRESCALE  (0)
-
 
31
#elif ( TICK_PRESCALE_VALUE == 4 )
-
 
32
    #define TIMER_PRESCALE  (1)
-
 
33
#elif ( TICK_PRESCALE_VALUE == 8 )
-
 
34
    #define TIMER_PRESCALE  (2)
-
 
35
#elif ( TICK_PRESCALE_VALUE == 16 )
-
 
36
    #define TIMER_PRESCALE  (3)
-
 
37
#elif ( TICK_PRESCALE_VALUE == 32 )
-
 
38
    #define TIMER_PRESCALE  (4)
-
 
39
#elif ( TICK_PRESCALE_VALUE == 64 )
-
 
40
    #define TIMER_PRESCALE  (5)
-
 
41
#elif ( TICK_PRESCALE_VALUE == 128 )
-
 
42
    #define TIMER_PRESCALE  (6)
-
 
43
#elif ( TICK_PRESCALE_VALUE == 256 )
-
 
44
    #define TIMER_PRESCALE  (7)
-
 
45
#else
-
 
46
    #error Invalid TICK_PRESCALE_VALUE specified.
-
 
47
#endif
-
 
48
 
-
 
49
 
-
 
50
TICK TickCount = 0; // 10ms/unit
-
 
51
 
16
 
-
 
17
static TICK TickCount = 0;  // 10ms/unit
52
 
18
 
53
 
19
 
54
/*
20
/*
55
*   Function: TickInit
21
*   Function: TickInit
56
*
22
*
Line 58... Line 24...
58
*   Output: Tick manager is initialized.
24
*   Output: Tick manager is initialized.
59
*   Pre-conditions: TickInit
25
*   Pre-conditions: TickInit
60
*   Affects: none
26
*   Affects: none
61
*   Depends: none
27
*   Depends: none
62
*/
28
*/
63
#define PERIOD (INSTR_FREQ/256/1000)*TICK_PERIOD_MS
-
 
-
 
29
 
64
void tickInit(void)
30
void tickInit(void)
65
{
31
{
66
    // Start the timer.
32
    T0CON = 0b10000000 | TICK_PRESCALE;
67
    TMR0L = TICK_COUNTER_LOW;
33
    TMR0H = (BYTE)((TICK_COUNTER & 0xFF00)>>8);
68
    TMR0H = TICK_COUNTER_HIGH;
34
    TMR0L = (TICK_COUNTER & 0xFF);
69
 
35
 
70
    // 16-BIT, internal timer, PSA set to 1:256
-
 
71
    T0CON = 0b00000000 | TIMER_PRESCALE;
-
 
72
 
-
 
73
    // Start the timer.
-
 
74
    T0CONbits.TMR0ON = 1;
-
 
75
 
-
 
76
    INTCONbits.TMR0IF = 1;
36
    INTCONbits.TMR0IF = 0;
77
    INTCONbits.TMR0IE = 1;
37
    INTCONbits.TMR0IE = 1;
-
 
38
    INTCON2bits.TMR0IP = 1;
-
 
39
 
-
 
40
    TickCount = 0;
78
}
41
}
79
 
42
 
80
/*
43
/*
81
*   Function: TickGet
44
*   Function: TickGet
82
*
45
*
Line 101... Line 64...
101
*   Affects: none
64
*   Affects: none
102
*   Depends: none
65
*   Depends: none
103
*/
66
*/
104
void tickUpdate(void)
67
void tickUpdate(void)
105
{
68
{
106
    if(INTCONbits.TMR0IF)
69
    if(INTCONbits.TMR0IF==1 && INTCONbits.TMR0IE==1)
107
    {
70
    {
108
        TMR0H = TICK_COUNTER_HIGH;
71
        TMR0H = (BYTE)((TICK_COUNTER & 0xFF00)>>8);
109
        TMR0L = TICK_COUNTER_LOW;
72
        TMR0L = (TICK_COUNTER & 0xFF);
110
 
73
 
111
        TickCount++;
74
        TickCount++;
112
 
75
 
113
        INTCONbits.TMR0IF = 0;
76
        INTCONbits.TMR0IF = 0;
114
    }
77
    }