1 /** ###################################################################
\r
2 ** THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
\r
3 ** Filename : TickTimer.C
\r
4 ** Project : RTOSDemo
\r
5 ** Processor : MC9S12DP256BCPV
\r
6 ** Beantype : TimerInt
\r
7 ** Version : Bean 02.063, Driver 01.05, CPU db: 2.87.283
\r
8 ** Compiler : Metrowerks HC12 C Compiler
\r
9 ** Date/Time : 18/06/2005, 16:21
\r
11 ** This bean "TimerInt" implements a periodic interrupt.
\r
12 ** When the bean and its events are enabled, the "OnInterrupt"
\r
13 ** event is called periodically with the period that you specify.
\r
14 ** TimerInt supports also changing the period in runtime.
\r
15 ** The source of periodic interrupt can be timer compare or reload
\r
16 ** register or timer-overflow interrupt (of free running counter).
\r
18 ** Timer name : ECT (16-bit)
\r
19 ** Compare name : TC0
\r
20 ** Counter shared : No
\r
22 ** High-speed CPU mode
\r
23 ** Prescaler : divide-by-8
\r
24 ** Clock : 3124000 Hz
\r
25 ** Initial period/frequency
\r
26 ** Xtal ticks : 16000
\r
27 ** microseconds : 1000
\r
29 ** seconds (real) : 0.0010000
\r
33 ** Runtime setting : period/frequency interval (continual setting)
\r
34 ** ticks : 16000 to 320000 ticks
\r
35 ** microseconds : 1000 to 20000 microseconds
\r
36 ** milliseconds : 1 to 20 milliseconds
\r
37 ** seconds (real) : 0.0010000 to 0.0200000 seconds
\r
38 ** Hz : 50 to 1000 Hz
\r
45 ** Counter : TCNT [68]
\r
48 ** Prescaler : TSCR2 [77]
\r
50 ** Compare registers
\r
51 ** Compare : TC0 [80]
\r
53 ** Flip-flop registers
\r
54 ** Mode : TCTL2 [73]
\r
56 ** Enable - byte TickTimer_Enable(void);
\r
57 ** SetPeriodTicks16 - byte TickTimer_SetPeriodTicks16(word Ticks);
\r
58 ** SetPeriodTicks32 - byte TickTimer_SetPeriodTicks32(dword Ticks);
\r
59 ** SetPeriodUS - byte TickTimer_SetPeriodUS(word Time);
\r
60 ** SetPeriodMS - byte TickTimer_SetPeriodMS(word Time);
\r
61 ** SetFreqHz - byte TickTimer_SetFreqHz(word Freq);
\r
63 ** (c) Copyright UNIS, spol. s r.o. 1997-2002
\r
64 ** UNIS, spol. s r.o.
\r
68 ** http : www.processorexpert.com
\r
69 ** mail : info@processorexpert.com
\r
70 ** ###################################################################*/
\r
73 /* MODULE TickTimer. */
\r
76 #include "TickTimer.h"
\r
78 /* Definition of DATA and CODE segments for this bean. User can specify where
\r
79 these segments will be located on "Build options" tab of the selected CPU bean. */
\r
80 #pragma DATA_SEG TickTimer_DATA /* Data section for this module. */
\r
81 #pragma CODE_SEG TickTimer_CODE /* Code section for this module. */
\r
83 static word CmpHighVal; /* Compare register value for high speed CPU mode */
\r
87 ** ===================================================================
\r
88 ** Method : SetCV (bean TimerInt)
\r
91 ** This method is internal. It is used by Processor Expert
\r
93 ** ===================================================================
\r
95 static void SetCV(word Val)
\r
97 if (Val == 0) /* If the given value is zero */
\r
98 Val = 65535; /* then change it to the maximal one */
\r
99 TC0 = Val; /* Store given value to the compare register */
\r
100 TC7 = Val; /* Store given value to the modulo register */
\r
104 ** ===================================================================
\r
105 ** Method : SetPV (bean TimerInt)
\r
108 ** This method is internal. It is used by Processor Expert
\r
110 ** ===================================================================
\r
112 static void SetPV(byte Val)
\r
114 TSCR2_PR = Val; /* Store given value to the prescaler */
\r
118 ** ===================================================================
\r
119 ** Method : HWEnDi (bean TimerInt)
\r
122 ** This method is internal. It is used by Processor Expert
\r
124 ** ===================================================================
\r
126 static void HWEnDi(void)
\r
128 TFLG1 = 1; /* Reset interrupt request flag */
\r
129 TIE_C0I = 1; /* Enable interrupt */
\r
133 ** ===================================================================
\r
134 ** Method : TickTimer_Enable (bean TimerInt)
\r
137 ** Enable the bean - it starts the timer. Events may be
\r
138 ** generated ("DisableEvent"/"EnableEvent").
\r
139 ** Parameters : None
\r
141 ** --- - Error code, possible codes:
\r
143 ** ERR_SPEED - This device does not work in
\r
144 ** the active speed mode
\r
145 ** ===================================================================
\r
147 byte TickTimer_Enable(void)
\r
149 HWEnDi(); /* Enable the device */
\r
150 return ERR_OK; /* OK */
\r
154 ** ===================================================================
\r
155 ** Method : TickTimer_SetPeriodTicks16 (bean TimerInt)
\r
158 ** This method sets the new period of the generated events.
\r
159 ** The period is expressed in Xtal ticks as a 16-bit unsigned
\r
161 ** This method is available only if runtime setting type
\r
162 ** 'from interval' is selected in the Timing dialog box in
\r
163 ** Runtime setting area.
\r
165 ** NAME - DESCRIPTION
\r
166 ** Ticks - Period to set [in Xtal ticks]
\r
167 ** (16000 to 65535 ticks)
\r
169 ** --- - Error code, possible codes:
\r
171 ** ERR_SPEED - This device does not work in
\r
172 ** the active speed mode
\r
173 ** ERR_MATH - Overflow during evaluation
\r
174 ** ERR_RANGE - Parameter out of range
\r
175 ** ===================================================================
\r
177 byte TickTimer_SetPeriodTicks16(word Ticks)
\r
179 dlong rtval; /* Result of two 32-bit numbers multiplication */
\r
180 word rtword; /* Result of 64-bit number division */
\r
182 if (Ticks < 16000) /* Is the given value out of range? */
\r
183 return ERR_RANGE; /* If yes then error */
\r
184 PE_Timer_LngMul((dword)Ticks,838592365,&rtval); /* Multiply given value and high speed CPU mode coefficient */
\r
185 if (PE_Timer_LngHi4(rtval[0],rtval[1],&rtword)) /* Is the result greater or equal than 65536 ? */
\r
186 rtword = 65535; /* If yes then use maximal possible value */
\r
187 CmpHighVal = rtword; /* Store result (compare register value for high speed CPU mode) to the variable CmpHighVal */
\r
188 SetCV(CmpHighVal); /* Store appropriate value to the compare register according to the selected high speed CPU mode */
\r
189 return ERR_OK; /* OK */
\r
193 ** ===================================================================
\r
194 ** Method : TickTimer_SetPeriodTicks32 (bean TimerInt)
\r
197 ** This method sets the new period of the generated events.
\r
198 ** The period is expressed in Xtal ticks as a 32-bit unsigned
\r
200 ** This method is available only if runtime setting type
\r
201 ** 'from interval' is selected in the Timing dialog box in
\r
202 ** Runtime setting area.
\r
204 ** NAME - DESCRIPTION
\r
205 ** Ticks - Period to set [in Xtal ticks]
\r
206 ** (16000 to 320000 ticks)
\r
208 ** --- - Error code, possible codes:
\r
210 ** ERR_SPEED - This device does not work in
\r
211 ** the active speed mode
\r
212 ** ERR_MATH - Overflow during evaluation
\r
213 ** ERR_RANGE - Parameter out of range
\r
214 ** ===================================================================
\r
216 byte TickTimer_SetPeriodTicks32(dword Ticks)
\r
218 dlong rtval; /* Result of two 32-bit numbers multiplication */
\r
219 word rtword; /* Result of 64-bit number division */
\r
221 if ((Ticks > 320000) || (Ticks < 16000)) /* Is the given value out of range? */
\r
222 return ERR_RANGE; /* Range error */
\r
223 PE_Timer_LngMul(Ticks,838592365,&rtval); /* Multiply given value and high speed CPU mode coefficient */
\r
224 if (PE_Timer_LngHi4(rtval[0],rtval[1],&rtword)) /* Is the result greater or equal than 65536 ? */
\r
225 rtword = 65535; /* If yes then use maximal possible value */
\r
226 CmpHighVal = rtword; /* Store result (compare register value for high speed CPU mode) to the variable CmpHighVal */
\r
227 SetCV(CmpHighVal); /* Store appropriate value to the compare register according to the selected high speed CPU mode */
\r
228 return ERR_OK; /* OK */
\r
232 ** ===================================================================
\r
233 ** Method : TickTimer_SetPeriodUS (bean TimerInt)
\r
236 ** This method sets the new period of the generated events.
\r
237 ** The period is expressed in microseconds as a 16-bit
\r
238 ** unsigned integer number.
\r
239 ** This method is available only if runtime setting type
\r
240 ** 'from interval' is selected in the Timing dialog box in
\r
241 ** Runtime setting area.
\r
243 ** NAME - DESCRIPTION
\r
244 ** Time - Period to set [in microseconds]
\r
245 ** (1000 to 20000 microseconds)
\r
247 ** --- - Error code, possible codes:
\r
249 ** ERR_SPEED - This device does not work in
\r
250 ** the active speed mode
\r
251 ** ERR_MATH - Overflow during evaluation
\r
252 ** ERR_RANGE - Parameter out of range
\r
253 ** ===================================================================
\r
255 byte TickTimer_SetPeriodUS(word Time)
\r
257 dlong rtval; /* Result of two 32-bit numbers multiplication */
\r
258 word rtword; /* Result of 64-bit number division */
\r
260 if ((Time > 20000) || (Time < 1000)) /* Is the given value out of range? */
\r
261 return ERR_RANGE; /* If yes then error */
\r
262 PE_Timer_LngMul((dword)Time,52412023,&rtval); /* Multiply given value and high speed CPU mode coefficient */
\r
263 if (PE_Timer_LngHi3(rtval[0],rtval[1],&rtword)) /* Is the result greater or equal than 65536 ? */
\r
264 rtword = 65535; /* If yes then use maximal possible value */
\r
265 CmpHighVal = rtword; /* Store result (compare register value for high speed CPU mode) to the variable CmpHighVal */
\r
266 SetCV(CmpHighVal); /* Store appropriate value to the compare register according to the selected high speed CPU mode */
\r
267 return ERR_OK; /* OK */
\r
271 ** ===================================================================
\r
272 ** Method : TickTimer_SetPeriodMS (bean TimerInt)
\r
275 ** This method sets the new period of the generated events.
\r
276 ** The period is expressed in miliseconds as a 16-bit
\r
277 ** unsigned integer number.
\r
278 ** This method is available only if runtime setting type
\r
279 ** 'from interval' is selected in the Timing dialog box in
\r
280 ** Runtime setting area.
\r
282 ** NAME - DESCRIPTION
\r
283 ** Time - Period to set [in miliseconds]
\r
284 ** (1 to 20 milliseconds)
\r
286 ** --- - Error code, possible codes:
\r
288 ** ERR_SPEED - This device does not work in
\r
289 ** the active speed mode
\r
290 ** ERR_MATH - Overflow during evaluation
\r
291 ** ERR_RANGE - Parameter out of range
\r
292 ** ===================================================================
\r
294 byte TickTimer_SetPeriodMS(word Time)
\r
296 dlong rtval; /* Result of two 32-bit numbers multiplication */
\r
297 word rtword; /* Result of 64-bit number division */
\r
299 if ((Time > 20) || (Time < 1)) /* Is the given value out of range? */
\r
300 return ERR_RANGE; /* If yes then error */
\r
301 PE_Timer_LngMul((dword)Time,204734464,&rtval); /* Multiply given value and high speed CPU mode coefficient */
\r
302 if (PE_Timer_LngHi2(rtval[0],rtval[1],&rtword)) /* Is the result greater or equal than 65536 ? */
\r
303 rtword = 65535; /* If yes then use maximal possible value */
\r
304 CmpHighVal = rtword; /* Store result (compare register value for high speed CPU mode) to the variable CmpHighVal */
\r
305 SetCV(CmpHighVal); /* Store appropriate value to the compare register according to the selected high speed CPU mode */
\r
306 return ERR_OK; /* OK */
\r
310 ** ===================================================================
\r
311 ** Method : TickTimer_SetFreqHz (bean TimerInt)
\r
314 ** This method sets the new frequency of the generated
\r
315 ** events. The frequency is expressed in Hz as a 16-bit
\r
316 ** unsigned integer number.
\r
317 ** This method is available only if runtime setting type
\r
318 ** 'from interval' is selected in the Timing dialog box in
\r
319 ** Runtime setting area.
\r
321 ** NAME - DESCRIPTION
\r
322 ** Freq - Frequency to set [in Hz]
\r
325 ** --- - Error code, possible codes:
\r
327 ** ERR_SPEED - This device does not work in
\r
328 ** the active speed mode
\r
329 ** ERR_MATH - Overflow during evaluation
\r
330 ** ERR_RANGE - Parameter out of range
\r
331 ** ===================================================================
\r
333 byte TickTimer_SetFreqHz(word Freq)
\r
335 dlong rtval; /* Result of two 32-bit numbers division */
\r
336 word rtword; /* Result of 64-bit number division */
\r
338 if ((Freq > 1000) || (Freq < 50)) /* Is the given value out of range? */
\r
339 return ERR_RANGE; /* If yes then error */
\r
340 rtval[1] = 799744000 / (dword)Freq; /* Divide high speed CPU mode coefficient by the given value */
\r
341 rtval[0] = 0; /* Convert result to the type dlong */
\r
342 if (PE_Timer_LngHi1(rtval[0],rtval[1],&rtword)) /* Is the result greater or equal than 65536 ? */
\r
343 rtword = 65535; /* If yes then use maximal possible value */
\r
344 CmpHighVal = rtword; /* Store result (compare register value for high speed CPU mode) to the variable CmpHighVal */
\r
345 SetCV(CmpHighVal); /* Store appropriate value to the compare register according to the selected high speed CPU mode */
\r
346 return ERR_OK; /* OK */
\r
350 ** ===================================================================
\r
351 ** Method : TickTimer_Init (bean TimerInt)
\r
354 ** This method is internal. It is used by Processor Expert
\r
356 ** ===================================================================
\r
358 void TickTimer_Init(void)
\r
360 CmpHighVal = 3124; /* Compare register value for high speed CPU mode */
\r
361 SetCV(CmpHighVal); /* Store appropriate value to the compare register according to the selected high speed CPU mode */
\r
362 SetPV(3); /* Set prescaler register according to the selected high speed CPU mode */
\r
363 HWEnDi(); /* Enable/disable device according to status flags */
\r
367 ** ===================================================================
\r
368 ** Method : TickTimer_Interrupt (bean TimerInt)
\r
371 ** This method is internal. It is used by Processor Expert
\r
373 ** ===================================================================
\r
375 #pragma CODE_SEG __NEAR_SEG NON_BANKED /* Interrupt section for this module. Placement will be in NON_BANKED area. */
\r
376 __interrupt void TickTimer_Interrupt(void)
\r
378 TFLG1 = 1; /* Reset interrupt request flag */
\r
379 TickTimer_OnInterrupt(); /* Invoke user event */
\r
382 #pragma CODE_SEG TickTimer_CODE /* Code section for this module. */
\r
384 /* END TickTimer. */
\r
387 ** ###################################################################
\r
389 ** This file was created by UNIS Processor Expert 03.33 for
\r
390 ** the Motorola HCS12 series of microcontrollers.
\r
392 ** ###################################################################
\r