Monday, January 09, 2006

8051: Example1

LED Blinking

The purpose of this example is not to demonstrate the operation of LEDs, but the operating speed of the microcontroller. Simply put, in order to enable LED blinking to be visible, it is necessary to provide sufficient amount of time to pass between on/off states of LEDs. In this example time delay is provided by executing a subroutine called Delay. It is a triple loop in which the program remains for approximately 0.5 seconds and decrements values stored in registers R0, R1 or R2. After returning from the subroutine, the pin state is inverted and the same procedure is repeated...
;************************************************************************
;* PROGRAM NAME : Delay.ASM
;* DESCRIPTION: Program turns on/off LED on the pin P1.0
;* Software delay is used (Delay).
;************************************************************************
;BASIC DIRECTIVES
$MOD53
$TITLE(DELAY.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING

;STACK
        DSEG    AT    03FH
STACK_START:    DS    040H

;RESET VECTORS
       CSEG     AT    0
       JMP      XRESET                  ;Reset vector
       
       ORG      100H
       
XRESET: MOV     SP,#STACK_START         ;Define Stack pointer
        MOV     P1,#0FFh                ;All pins are configured as inputs

LOOP:
        CPL     P1.0                    ;Pin P1.0 state is inverted
        LCALL   Delay                   ;Time delay
        SJMP    LOOP

Delay:
        MOV     R2,#20                  ;500 ms time delay
F02:    MOV     R1,#50                  ;25 ms
F01:    MOV     R0,#230
        DJNZ    R0,$
        DJNZ    R1,F01
        DJNZ    R2,F02

END                                     ;End of program