Path: Home => AVR-Übersicht => Software => LPM-Befehl ;

Uses the LPM-command for reading bytes from a table

; located in the code segment
; Reads the keys on the STK200 board and translates
; the position of the key to the number of LEDs by use
; of a table in the code segment and displays these
; LEDs (switch 0: 8 LEDs, switch 1: 1 LED, switch 2:
; 2 LEDs, etc.).
; A rather useless program, but it demonstrates the use
; of the LPM command to access the code segment and
; some ROLling and JUMPing.

; Registers

.DEF   erg=R0 ; The LPM-command uses R0
.DEF   mpr=R16 ; Multi-funktion-register
; Registers ZL (R30) and ZH (R31) are also used but are already
; defined in, so we don't need to do it here.

; Reset-/Interrupt-Vector

   RJMP   main
main:   CLR   mpr ; Load 0 to register mpr
   OUT   DDRD,mpr ; all D-Ports are Input switches
   DEC   mpr ; Load FF to Register B
   OUT   DDRB,mpr ; All B-Ports are outputs to LEDs
   OUT   PORTD,mpr ; All Pullups Port D on

loop:   LDI   ZL,LOW(liste2) ; Register pair Z points to
   LDI   ZH,HIGH(liste2) ; first Byte (FF) in the list
   IN   mpr,PIND ; Read switches
   CPI   mpr,0xFF ; All switches off? All LEDs off!
   BREQ   ;read
incp:   INC   ZL ;Point LSB to next byte on list
   BRNE   rolle ;overflow to MSB?
   INC   ZH ;MSB overflow, increment
rolle:   ROR   mpr ;shift Bit 0 to carry
   BRLO   incp ; Carry=1, switch is off, next list element
lesen:   LPM ; Read byte on which Z points to, to R0
   OUT   PORTB,erg ; Output to LEDs
   RJMP   loop ; Loop
; The list with the LED combinations, each byte refers to a switch
; The values must be defined word-wise to avoid addition of a zero Byte.
; Use of .DB xx always adds a zero Byte. To define a word, you can also use ; .DB xx,yy, which are linked to a word. The same applies to text input
; using .DB "abcd...". Even number of bytes or characters are linked, odd
; numbers receive a zero byte at the end of the list.
.DW   0xFEFF
; 1 LED, 0 LED (0 ist second, 1 first!)
.DW   0xF8FC ; 3 LEDs, 2 LEDs
.DW   0xE0F0 ; 5 LEDs, 4 LEDs
.DW   0x80C0 ; 7 LEDs, 6 LEDs
.EQU   liste2=liste*2 ; This is needed, because adresses
;      of the list are wordwise, access is bytewise

©2002 by