Chapter 4 Timer Functions
69
16-bit Timer Operation (timer 4)
4-3 16-bit Timer Operation (timer 4)

4-3-1 Overview

Timer 4 is a 16-bit programmable counter that can be used as an event counter.
A signal with a frequency of 1/2 of the timer 4 overflow signal can be output from the
TM4IO pin. An input capture function and pulse added type PWM output function can
also be used.

4-3-2 Operation

Timer Operation
Settings for timer operation are listed below.
(1) Set the TM4EN flag of the timer 4 mode register (TM4MD) to "0" to stop the count
operation of timer 4.
(2) Set the TM4CK2 to 0 flags of the TM4MD register to select fosc, fs/4, or fs/16 as the
clock source.
(3) Set the TM4PWM flag of the TM4MD register to "0" to select 16-bit timer operation.
(4) Set a value in compare register 4 (TM4OCH, TM4OCL).
(5) Set the TM4EN flag of the TM4MD register to "1" to start the timer.
(6) When timer 4 begins operation, binary counter 4 counts upward from X'0000'.
(7) When the value of binary counter 4 matches that of the TM4OCH and TM4OCL
registers, the timer 4 interrupt request flag is set, the value of binary counter 4 is reset
to X'0000', and counting begins again.
When servicing an interrupt, reset
the timer 4 interrupt request flag
before operating timer 4.
During a count operation, be
careful if the value set in TM4OCH
and TM4OCL is smaller than the
value of binary counter 4, since
the count-up operation will
continue until overflow occurs.