Friday, January 20, 2006

8051: Examples 9

Writing and changing digits on LED display

This program is only an extended verson of the previous one. There is only one digit active- the first one on the right, and there is no use of multiplexing. Unlike the previous example, all decimal numbers are displayed (0-9). In order to enable digits to change at reasonable pace, a soubroutine L2 which causes a short time delay is executed prior to each change occurs. Basically, the whole process is very simple and takes place in the main loop called LOOP which looks as follows:
  1. R3 is copied to Accumulator and subroutine for masking digits Disp is executed;
  2. Accumulator is copied to the port and displayed;
  3. The contents of the R3 register is incremented;
  4. It is checked whether 10 cycles are counted or not. If it is, register R3 is reset in order to enable counting to start from 0; and
  5. Instruction labeled as L2 within subroutine is executed.
;************************************************************************
;* PROGRAM NAME: 7Seg2.ASM
;* DESCRIPTION: Program writes numbers 0-9 on 7-segment LED display
;************************************************************************

;BASIC DIRECTIVES

$MOD53
$TITLE(7SEG2.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      R3,#0                    ; Counter initial value
         MOV      P1,#0                    ; Turn off all display segments
         MOV      P3,#20h                  ; Activate display D4

LOOP:
         MOV      A,R3
         LCALL    Disp                     ; Perform appropriate masking for number in
                                           ; Accumulator
         MOV      P1,A
         INC      R3                       ; Increment number in register by 1
         CJNE     R3,#10,L2                ; Check whether the number 10 is in R3
         MOV      R3,#0                    ; If it is, reset counter
L2:
         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
         SJMP     LOOP

Disp:                                      ; Subroutine for writing digits
         INC      A
         MOVC     A,@A+PC
         RET
         DB       3FH                      ; Digit 0 mask
         DB       06H                      ; Digit 1 mask
         DB       5BH                      ; Digit 2 mask
         DB       4FH                      ; Digit 3 mask
         DB       66H                      ; Digit 4 mask
         DB       6DH                      ; Digit 5 mask
         DB       7DH                      ; Digit 6 mask
         DB       07H                      ; Digit 7 mask
         DB       7FH                      ; Digit 8 mask
         DB       6FH                      ; Digit 9 mask

         END                               ; End of program