
248
CHAPTER 8 8/16-BIT TIMER/COUNTER
r Coding example
DDR2
T2CR
T1CR
T2DR
T1DR
T1IF
ILR2
NT_V
IRQ5
EQU
DSEG
ORG
DW
ENDS
000DH
0018H
0019H
001AH
001BH
T1CR:7
007DH
ABS
0FFF0H
WARI
; Address of the Port 2 data direction register
; Address of the Timer 2 control register
; Address of the Timer 1 control register
; Address of the Timer 2 data register
; Address of the Timer 1 data register
; Define the timer 1 interrupt request flag bit.
; Address of the interrupt level setting
register 2
; Set interrupt vector.
;---------- Main program --------------------------------------------------------------------------------------------
CSEG
; [CODE SEGMENT]
; Stack pointer (SP) etc. are already
initialized.
:
MOV
CLRI
MOV
SETI
DDR2,#00000000B
ILR2,#11111011B
T1DR,#088H
T2DR,#013H
T2CR,#00001100B
T1CR,#01001101B
; Set P20/EC pin as an input.
; Disable interrupts.
; Set interrupt level 2.
; Set lower 8 bits of counter comparison value.
; Set upper 8 bits of counter comparison value.
; Set timer 2 to 16-bit mode.
; Clear timer 1 interrupt request flag, enable
interrupt
; request output, set P22/TO
; as general-purpose port (P22), select
external clock,
; clear counter, and start operation.
; Enable CPU interrupts.
:
;---------- Data read subroutine ----------------------------------------------------------------------
:
READ16
MOVW
CMPW
BEQ
XCHW
CMPW
BNE
RET
A,T2DR
A
RET16
A,T
A
READ16
; 16-bit read, T1DR + T2DR.
; 16-bit read, T1DR + T2DR, save old value in
T register.
; Check first and second reads, compare A
and T registers.
; If match, return.
; Old value + 1
; If mismatch, read again.
:
;---------- Interrupt program ---------------------------------------------------------------------