Wednesday, January 11, 2006

8051: Example 3

Timer T0 in mode 1

This program spends most of its time in an endless loop waiting for timer T0 to count up a full cycle. When it happens, an interrupt is generated, routine TIM0_ISR is executed and logic zero (0) on port P1 is shifted right by one bit. This is another way of demonstrating the operating speed of the microcontroller since each shift means that counter T0 has counted up 216 pulses!
;************************************************************************
;* PROGRAM NAME : Tim0Mod1.ASM
;* DESCRIPTION: Program rotates "0" on port 1. Timer T0 in mode 1 is
;* used
;************************************************************************

;BASIC DIRECTIVES

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

;DECLARATION OF VARIABLES

;STACK

        DSEG     AT     03FH
STACK_START:     DS     040H

;RESET VECTORS
       CSEG      AT 0
       JMP       XRESET             ; Reset vector

       ORG       00BH
       JMP       TIM0_ISR           ; Timer T0 reset vector

       ORG       100H

XRESET: MOV      SP,#STACK_START    ; Define Stack pointer
       MOV       TMOD,#01H          ; MOD1 is selected
       MOV       A,#0FFH
       MOV       P1,#0FFH
       SETB      TR0                ; Timer T0 is enabled
       MOV       IE,#082H           ; Interrupt enabled
       CLR       C

LOOP1: SJMP      LOOP1              ; Remain here

TIM0_ISR:        RRC     A          ; Rotate accumulator A through Carry flag
                 MOV     P1,A       ; Contents of accumulator A is moved to PORT1
                 RETI               ; Return from interrupt

       END                          ; End of program