Lookup

finds a table entry corresponding to an index number.

A classic application for Lookup is to convert the numbers 0 through 9 (or 0 through F in hexadecimal) into patterns for seven-segment displays. Throughout this book, you will see other applications, such as the table Pinz that some routines use to change a number in the range of 0 through 7 into a byte with a 1 in the corresponding position for use as a bit mask.

Lookup tables are an inherent PIC capability made possible by the retw instruction, which stands for "return with literal value in w." A table consists of a way to select from a list of retws. The instruction jmp pc+w takes care of this. Since the program counter always points to the next instruction, a 0 in w selects the first retw from the list; a 1 the second; a 2 the third, and so on.

The Parallax assembler allows great flexibility in formatting data for tables. You can type one retw n per line, or use the shorthand retw n1,n2,n3...For tables of text, you can type retw 'Text data in retw table'. The assembler will turn this into a series of retws with the ASCII codes for each character of the text. For an example of such a table, see the program listing for Lookdown.

A couple of cautions about using tables. First, your program must ensure that the number in w never exceeds the maximum entry in the table. If it does, program execution will jump completely over the table. Second, tables must be in the first 256 words of a 512-word program-memory page because of the way the PIC's computed-jump mechanism works.

Demonstrating Lookup.

To see Lookup in operation, either run the program with the PSIM simulator, or connect the circuit below to an erasable PIC or PIC emulator, such as the Parallax downloader. Assemble and run LOOKUP.SRC. When you apply power to the PIC, the display will count upward in hexadecimal from 0 to F.

James Newton says: To drive multiple displays, just connect the first displays cathode to ra.0 and the second displays cathode to ra.1 Use the same code you would to drive one display, but call it twice, once with ra.0 low and ra.1 high and again with ra.0 high and ra.1 low. Continue to do this as quickly as possible. See also Driving 8 or 9 7 segment displays with 8 or 9 io pins (and no extra circuitry)


;
; ***************************************************************************
; ***  Bubble Software Parallax to PIC Source Converter. Copyright 1999.  ***
; ***  http://www.bubblesoftonline.com                 email: sales@picnpoke.com  ***
; ***************************************************************************
;
; LOOKUP index (in w), table
; Finds the table entry corresponding to index (in w) and 
; returns it in w. This demonstration uses a table to map the 
; numbers 0 through F hex to patterns on a seven-segment LED
; display. It assumes that the display is connected with segment
; a to rb.0, b to rb.1... and g to rb.6 through current-limiting 
; resistors of 220 ohms minimum. When the program runs, it counts
; upward 0, 1, 2, 3...through F, then begins the count again at 0. 

; Device data and reset vector
	P = pic16c55
	#include <16c55.inc>   ; processor assembler definitions
	_CONFIG _xt_osc & _wdt_off & _protect_off
 reset start

 org     8
count	Res d'1' ; Counter variable for demo.
temp	Res d'1' ; Variables for delays
temp2	Res d'1' ; used in demo subroutines. 

 org 0

Lookup       ADDWF pcl                  ; Jump to entry spec'd by w.
             RETLW d'63'                ; 0, 1, 2, 3
             RETLW d'6'
             RETLW d'91'
             RETLW d'79'
             RETLW d'102'               ; 4, 5, 6
             RETLW d'109'
             RETLW d'125'
             RETLW d'7'                 ; 7, 8, 9
             RETLW d'127'
             RETLW d'103'
             RETLW d'119'               ; A, B, C
             RETLW d'124'
             RETLW d'57'
             RETLW d'94'                ; D, E, F
             RETLW d'121'
             RETLW d'113'

start        MOVLW d'0'                 ; All outputs for LEDs
             TRIS 6h
             CLRF 6h                    ; All LEDs off to begin.
             CLRF count                 ; Start count at 0. 
start_loop   MOVF count,w               ; Put number to look up into w.
             ANDLW b'00001111'          ; Strip off high bits. 
             CALL Lookup                ; Call the table. 
             MOVWF 6h                   ; Write pattern in w to LEDs. 
start_wait   DECFSZ temp                ; Short delay. 
             GOTO start_wait
             DECFSZ temp2               
             GOTO start_wait
             INCF count                 ; count = count + 1
             GOTO start_loop            ; Do it again.

             
             
             end


; LOOKUP index (in w), table
; Finds the table entry corresponding to index (in w) and 
; returns it in w. This demonstration uses a table to map the 
; numbers 0 through F hex to patterns on a seven-segment LED
; display. It assumes that the display is connected with segment
; a to rb.0, b to rb.1... and g to rb.6 through current-limiting 
; resistors of 220 ohms minimum. When the program runs, it counts
; upward 0, 1, 2, 3...through F, then begins the count again at 0. 

	org     8
count	ds	1	; Counter variable for demo.
temp	ds	1	; Variables for delays
temp2	ds	1	; used in demo subroutines. 

; Device data and reset vector
	device	pic16c55,xt_osc,wdt_off,protect_off
	reset	start
	org	0

Lookup	jmp	pc+w		; Jump to entry spec'd by w.
	retw	63,6,91,79	; 0, 1, 2, 3
	retw	102,109,125	; 4, 5, 6
	retw	7,127,103	; 7, 8, 9
	retw	119,124,57	; A, B, C
	retw	94,121,113	; D, E, F

start	mov	!rb, #0		; All outputs for LEDs
	clr	rb		; All LEDs off to begin.
	clr	count		; Start count at 0. 
:loop	mov	w,count		; Put number to look up into w.
	AND	w,#00001111b	; Strip off high bits. 
	call	Lookup		; Call the table. 
	mov	rb,w		; Write pattern in w to LEDs. 
:wait	djnz	temp,:wait	; Short delay. 
	djnz	temp2,:wait
	inc	count		; count = count + 1
	jmp	:loop		; Do it again.

See also:

Questions:

Comments: