Friday, January 13, 2006

8051: Example 5

Simultaneous use of timers T0 and T1

This program can be considered as continuation of the previous one. They share the same idea, but in this case true timers T0 and T1 are used. In order to demonstrate the operation of both timers on the same port at the same time, timer T0 reset is used to shift logic zero (0) on the port, while Timer T1 reset is used to change rotation direction. This program spends most of its time in the loop LOOP1 waiting for an interrupt to be caused by reset. By checking the DIRECTION bit, information on rotation direction of both bits in accumulator as well as of moving port LED is obtained.
;************************************************************************
;* PROGRAM NAME : Tim0Tim1.ASM
;* DESCRIPTION: Timer TO rotates bit on port P1 while Timer1
;* changes rotation direction. Both timers are configured to operate in mode 1.
;************************************************************************

;BASIC DIRECTIVES

$MOD53
$TITLE(TIM0TIM1.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING

;DECLARATION OF VARIABLES

       BSEG    AT    0

;DECLARATION OF BIT-VARIABLES

SEMAPHORE:      DBIT    8
DIRECTION       BIT     SEMAPHORE

;STACK
       DSEG    AT    03FH
STACK_START:   DS    040H

;RESET VECTORS
       CSEG    AT    0
       JMP     XRESET                     ; Reset vector

       ORG     00BH                       ; Timer 0 Reset vector
       JMP     TIM0_ISR

       ORG     01BH                       ; Timer 1 Reset vector
       JMP     TIM1_ISR

       ORG     100H


XRESET: MOV    SP,#STACK_START            ; Define Stack pointer
        MOV    TMOD,#11H                  ; Select MOD1 for both timers
        MOV    A,#0FFH
        MOV    P1,#0FFH
        MOV    R0,#30D                    ; R0 is initialized
        SETB   TR0                        ; TIMER0 is turned on
        SETB   TR1                        ; TIMER1 is turned on
        MOV    IE,#08AH                   ; Timer0 and Timer1 Interrupt enabled
        CLR    C
        CLR    DIRECTION                  ; Rotate to the right

LOOP1:  SJMP   LOOP1                      ; Remain here


TIM0_ISR:
        JB     DIRECTION,LAB1
        RRC A                             ; Rotate contents of accumulator to the right through
                                          ; Carry flag
        SJMP   LAB2
LAB1:   RLC    A                          ; Rotate contents of Accumulator to the left through
                                          ; Carry flag
LAB2:   MOV    P1,A                       ; Contents of Accumulator is moved to port P1
        RETI                              ; Return from interrupt

TIM1_ISR:
        DJNZ   R0,LAB3                    ; When time expires, change rotation direction
        CPL    DIRECTION
        MOV    R0,#30D                    ; Initialize R0
LAB3:
        RETI
        END                               ; End of program