RTE-V831-PC
USER’S MANUAL (Rev. 2.00)
28
11.3.
USING TIMERS
A sample time measurement is indicated below which uses timer 1 and timer 2 cascaded with each
other by an external timer (8254) on the board. Timer 1 is initialized as an interval counter (mode 2),
and timer 2 is initialized as a down counter (mode 0). By determining the counter values before and
after a routine whose execution time is to be measured, the execution time can be calculated. Note that
both timers function as down counters. Note also that command recovery (dummy read from the ROM
area) is required for successive accesses to the external timer.
/ *
Sample execution time measurement using timers
* /
# d e f i n e T I M E R C L K 2 0 0 0 0 0 0
/ *
2 M H z
* /
# d e f i n e I N T E R V A L ( T I M E R C L K * 1 0 / 1 0 0 0 )
/ *
10 ms (1/100
) * /
# d e f i n e I O W A I T ( ) ( * ( c h a r * ) 0 x 4 F F F 0 0 0 0 ) )
/ *
For I/O command recovery
* /
I n i t T i m e r ( )
/ *
Timer initialization
* /
{
o u t b ( 0 x 4 5 0 0 B 0 0 C , 0 x 7 4 ) ;
I O W A I T ( ) ;
/ *
Timer 1 set to mode 2
* /
o u t b ( 0 x 4 5 0 0 B 0 0 4 , I N T E R V A L ) ;
I O W A I T ( ) ;
/ *
Lower digit count of timer 1
* /
o u t b ( 0 x 4 5 0 0 B 0 0 4 , I N T E R V A L
/
2 5 6 ) ; I O W A I T ( ) ;
/ *
Higher digit count of timer 1
* /
o u t b ( 0 x 4 5 0 0 B 0 0 C , 0 x B 0 ) ;
I O W A I T ( ) ;
/ *
Timer 2 set to mode 0
* /
o u t b ( 0 x 4 5 0 0 B 0 0 8 , 0 x F F ) ;
I O W A I T ( ) ;
/ *
Lower digit count of timer 2
* /
o u t b ( 0 x 4 5 0 0 B 0 0 8 , 0 x F F ) ;
I O W A I T ( ) ;
/ *
Higher digit count of timer 2
* /
r e t u r n 0 ;
}
L a t c h T i m e r ( )
/ *
Count Latch
* /
{
i n t c o u n t 1 , c o u n t 2 , c o u n t s ;
o u t b ( 0 x 4 5 0 0 B 0 0 C , 0 x D C ) ;
I O W A I T ( ) ;
/ *
Timer 1/2 multiple latch
* /
c o u n t 1 = i n b ( 0 x 4 5 0 0 B 0 0 4 ) ;
I O W A I T ( ) ;
c o u n t 1 + = i n b ( 0 x 4 5 0 0 B 0 0 4 ) * 2 5 6 ; I O W A I T ( ) ;
/ *
Count of timer 1
* /
c o u n t 2 = i n b ( 0 x 4 5 0 0 B 0 0 8 ) ;
I O W A I T ( ) ;
c o u n t 2 + = i n b ( 0 x 4 5 0 0 B 0 0 8 ) * 2 5 6 ; I O W A I T ( ) ;
/ *
Count of timer 2
* /
c o u n t s = I N T E R V A L * ( 0 x F F F F - c o u n t 2 )
+ ( I N T E R V A L - c o u n t 1 ) ;
r e t u r n c o u n t s ;
}
d o u b l e t o t a l
_
t i m e ;
m a i n ( )
{
i n t s t a r t
_
c o u n t , s t o p
_
c o u n t ;
I n i t T i m e r ( ) ;
s t a r t
_
c o u n t = L a t c h T i m e r ( ) ;
/ *
Start count value
* /
f u n c ( ) ;
s t o p
_
c o u n t = L a t c h T i m e r ( ) ;
/ *
Stop count value
* /
t o t a l
_
t i m e = ( d o u b l e ) ( s t o p
_
c o u n t - s t a r t
_
c o u n t )
/ ( d o u b l e ) T I M E R C L K ;
/ *
Seconds
* /
r e t u r n 0 ;
}
i n c l u d e < t i m e . h >
f u n c ( )
/ *
Time measurement routine
* /
{
. . .
}