1 |
/*********************************************************************
|
2 |
*
|
3 |
* Tick Manager for PIC18
|
4 |
*
|
5 |
*********************************************************************
|
6 |
* FileName: Tick.c
|
7 |
* Dependencies: stackTSK.h
|
8 |
* Tick.h
|
9 |
* Processor: PIC18
|
10 |
* Complier: MCC18 v1.00.50 or higher
|
11 |
* HITECH PICC-18 V8.10PL1 or higher
|
12 |
* Company: Microchip Technology, Inc.
|
13 |
*
|
14 |
* Software License Agreement
|
15 |
*
|
16 |
* The software supplied herewith by Microchip Technology Incorporated
|
17 |
* (the “Company”) for its PICmicro® Microcontroller is intended and
|
18 |
* supplied to you, the Company’s customer, for use solely and
|
19 |
* exclusively on Microchip PICmicro Microcontroller products. The
|
20 |
* software is owned by the Company and/or its supplier, and is
|
21 |
* protected under applicable copyright laws. All rights are reserved.
|
22 |
* Any use in violation of the foregoing restrictions may subject the
|
23 |
* user to criminal sanctions under applicable laws, as well as to
|
24 |
* civil liability for the breach of the terms and conditions of this
|
25 |
* license.
|
26 |
*
|
27 |
* THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
|
28 |
* WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
|
29 |
* TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
30 |
* PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
|
31 |
* IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
|
32 |
* CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
|
33 |
*
|
34 |
* HiTech PICC18 Compiler Options excluding device selection:
|
35 |
* -FAKELOCAL -G -O -Zg -E -C
|
36 |
*
|
37 |
*
|
38 |
*
|
39 |
*
|
40 |
* Author Date Comment
|
41 |
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
42 |
* Nilesh Rajbharti 6/28/01 Original (Rev 1.0)
|
43 |
* Nilesh Rajbharti 2/9/02 Cleanup
|
44 |
* Nilesh Rajbharti 5/22/02 Rev 2.0 (See version.log for detail)
|
45 |
********************************************************************/
|
46 |
|
47 |
#define TICK_INCLUDE
|
48 |
|
49 |
#include "stacktsk.h"
|
50 |
#include "tick.h"
|
51 |
|
52 |
#define TICK_TEMP_VALUE_1 \
|
53 |
((CLOCK_FREQ / 4) / (TICKS_PER_SECOND * TICK_PRESCALE_VALUE))
|
54 |
|
55 |
#if TICK_TEMP_VALUE_1 > 60000
|
56 |
#error TICK_PER_SECOND value cannot be programmed with current CLOCK_FREQ
|
57 |
#error Either lower TICK_PER_SECOND or manually configure the Timer
|
58 |
#endif
|
59 |
|
60 |
#define TICK_TEMP_VALUE (65535 - TICK_TEMP_VALUE_1)
|
61 |
|
62 |
#define TICK_COUNTER_HIGH ((TICK_TEMP_VALUE >> 8) & 0xff)
|
63 |
#define TICK_COUNTER_LOW (TICK_TEMP_VALUE & 0xff)
|
64 |
|
65 |
#if (TICK_PRESCALE_VALUE == 2)
|
66 |
#define TIMER_PRESCALE (0)
|
67 |
#elif ( TICK_PRESCALE_VALUE == 4 )
|
68 |
#define TIMER_PRESCALE (1)
|
69 |
#elif ( TICK_PRESCALE_VALUE == 8 )
|
70 |
#define TIMER_PRESCALE (2)
|
71 |
#elif ( TICK_PRESCALE_VALUE == 16 )
|
72 |
#define TIMER_PRESCALE (3)
|
73 |
#elif ( TICK_PRESCALE_VALUE == 32 )
|
74 |
#define TIMER_PRESCALE (4)
|
75 |
#elif ( TICK_PRESCALE_VALUE == 64 )
|
76 |
#define TIMER_PRESCALE (5)
|
77 |
#elif ( TICK_PRESCALE_VALUE == 128 )
|
78 |
#define TIMER_PRESCALE (6)
|
79 |
#elif ( TICK_PRESCALE_VALUE == 256 )
|
80 |
#define TIMER_PRESCALE (7)
|
81 |
#else
|
82 |
#error Invalid TICK_PRESCALE_VALUE specified.
|
83 |
#endif
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
TICK TickCount = 0;
|
89 |
|
90 |
|
91 |
/*********************************************************************
|
92 |
* Function: void TickInit(void)
|
93 |
*
|
94 |
* PreCondition: None
|
95 |
*
|
96 |
* Input: None
|
97 |
*
|
98 |
* Output: Tick manager is initialized.
|
99 |
*
|
100 |
* Side Effects: None
|
101 |
*
|
102 |
* Overview: Initializes Timer0 as a tick counter.
|
103 |
*
|
104 |
* Note: None
|
105 |
********************************************************************/
|
106 |
void TickInit(void)
|
107 |
{
|
108 |
// Start the timer.
|
109 |
TMR0L = TICK_COUNTER_LOW;
|
110 |
TMR0H = TICK_COUNTER_HIGH;
|
111 |
|
112 |
// 16-BIT, internal timer, PSA set to 1:256
|
113 |
T0CON = 0b00000000 | TIMER_PRESCALE;
|
114 |
|
115 |
|
116 |
// Start the timer.
|
117 |
T0CON_TMR0ON = 1;
|
118 |
|
119 |
INTCON_TMR0IF = 1;
|
120 |
INTCON_TMR0IE = 1;
|
121 |
|
122 |
}
|
123 |
|
124 |
|
125 |
/*********************************************************************
|
126 |
* Function: TICK TickGet(void)
|
127 |
*
|
128 |
* PreCondition: None
|
129 |
*
|
130 |
* Input: None
|
131 |
*
|
132 |
* Output: Current second value is given
|
133 |
*
|
134 |
* Side Effects: None
|
135 |
*
|
136 |
* Overview: None
|
137 |
*
|
138 |
* Note: None
|
139 |
********************************************************************/
|
140 |
TICK TickGet(void)
|
141 |
{
|
142 |
return TickCount;
|
143 |
}
|
144 |
|
145 |
|
146 |
/*********************************************************************
|
147 |
* Function: void TickUpdate(void)
|
148 |
*
|
149 |
* PreCondition: None
|
150 |
*
|
151 |
* Input: None
|
152 |
*
|
153 |
* Output: None
|
154 |
*
|
155 |
* Side Effects: None
|
156 |
*
|
157 |
* Overview: Internal Tick and Seconds count are updated.
|
158 |
*
|
159 |
* Note: None
|
160 |
********************************************************************/
|
161 |
void TickUpdate(void)
|
162 |
{
|
163 |
if ( INTCON_TMR0IF )
|
164 |
{
|
165 |
TMR0H = TICK_COUNTER_HIGH;
|
166 |
TMR0L = TICK_COUNTER_LOW;
|
167 |
|
168 |
TickCount++;
|
169 |
|
170 |
// Remove this if not needed.
|
171 |
// LATA4 ^= 1;
|
172 |
|
173 |
INTCON_TMR0IF = 0;
|
174 |
}
|
175 |
}
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|