; 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. ; .NOLIST .INCLUDE "C:\avrtools\appnotes\8515def.inc" .LIST ; ; Registers ; .DEF erg=R0 ; The LPM-command requires R0 .DEF mpr=R16 ; Multi-funktion register ; ; Uses the registers ZL (R30) and ZH (R31)! These are defined ; in 8515def.inc, so we don't need the definition here. ; ; Reset-/Interrupt-Vector ; rjmp main ; main: clr mpr ; Load 0 to Register mpr out DDRD,mpr ; all D-Ports are input for the switches dec mpr ; Load FF to Register B out DDRB,mpr ; All B-Ports are outputs to the LEDs out PORTD,mpr ; All Pullups on D on loop: ldi ZL,LOW(liste2) ; Register pair Z points to the 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 lesen ; Jump to lesen if all switches are off incp: inc ZL ; Point Z to next byte in the list brne rolle ; No rollover to next MSB inc ZH ; LSB rollover, increment MSB rolle: ror mpr ; Shift Bit 0 in Carry-Flag brlo incp ; Carry=1, switch not pressed, next in list lesen: lpm ; Read the byte in the list, that register Z points to, to R0=erg out PORTB,erg ; Transfer byte to the LEDs rjmp loop ; Move in circles ; ; The list in the code segment, every byte represent a switch ; The values have to be defined as words! Otherwise a byte, inserted ; with the .DB xx directive, is always paired by the assembler with a ; zero byte and stored as word in the list. When using .DB xx,yy no ; extra zero byte is inserted. The same applies to text strings ; (.DB "abcd..."). Even numbers of bytes or characters are ok, ; odd numbers will be added a zero byte. ; When adding a word like 1234h to the list, the first byte in the list that ; is received from the LPM command is the LSB (34h), not the MSB (12h)! ; liste: .DW 0xFEFF ; 1 LED, 0 LEDs (0 ist second, 1 is 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 the adress is ; organized word-wise, while the read- ; operation is byte-wise. ;