Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PCL and lookup tables

Status
Not open for further replies.

crash@home

New Member
i know there are posts about this on various websites but ive tried myself and searched and still not found the answer

i've written code for a lookup table for a 3 digit 7 set

but although the program counter shows it is being added with the wreg value, the location only jumps to half where it should be, ie

if 6 is in WREG and PCL is 50
after addwf pcl,f

PCL will be at 58, and although the next piece of code is the lookup table it will only jump to instruction 3


Code:
#include <p18f26j11.inc>
    CONFIG  XINST = OFF           ; Extended Instruction Set (Disabled)
	CONFIG	IOL1WAY = ON
	CONFIG	OSC = INTOSC
	CONFIG	WDTEN = OFF
	CONFIG	DEBUG = ON

		cblock	0x5F
	
		huns
		tens
		ones
		temp
		fetchcount
		endc

;
;Reset vector:
		org		0X00
		GOTO	START


FETCH
			DECF	fetchcount									;Enables software to make decisions as to which digit is being 
			BTFSS	fetchcount,1								;considered, 
			GOTO	ONES										;
			BTFSC	fetchcount,0								;fetchcount 3 (1,1) = hundreds
			GOTO	HUNS										;fetchcount 2 (1,0) = tens
			GOTO	TENS										;fetchcount 1 (0,1) = ones 

HUNS		MOVFF	huns,temp						
			GOTO	LOOKUP
TENS		MOVFF	tens,temp
			GOTO	LOOKUP
ONES		MOVFF	ones,temp
			GOTO	LOOKUP


LOOKUP	
			MOVFF	temp,WREG					;temp register allows for adjustable PCL reference without multiple lookup tables
			ADDWF	PCL,f
			RETLW	b'01111110'		;0
			RETLW	b'00001100'		;1
			RETLW	b'10110110'		;2
			RETLW	b'10011110'		;3
			RETLW	b'11001100'		;4
			RETLW	b'11011010'		;5
			RETLW	b'11111010'		;6
			RETLW	b'00001110'		;7
			RETLW	b'11111110'		;8
			RETLW	b'11001110'		;9
	


START	

;initialise variables
		CLRF	ones
		CLRF	tens
		CLRF	huns
		CLRF	fetchcount
		
		MOVLW	d'4'
		MOVFF	WREG,fetchcount

		MOVLW	d'6'		;1
		MOVWF	huns
		MOVLW	d'4'		;2
		MOVWF	tens
		MOVLW	d'8'		;3
		MOVWF	ones



Main
		CALL	FETCH
		MOVFF	WREG,huns
		CALL	FETCH
		MOVFF	WREG,tens
		CALL	FETCH
		MOVFF	WREG,ones
		MOVLW	d'4'
		MOVWF	fetchcount


		END
 
In your listing, once you get as far as 'GOTO LOOKUP' the program will go to where you sent it, then continue. lookup tables need a 'Call LOOKUP'. That way the program will go to the routine and get 'whatever the value is' then return (to the next line on from the call).

*That said, whatever is 'MOVFF' all about? :/
 
i designed the whole 'lookup sequence' as its own subroutine. reason being because i'm using a 3 digit 7 seg i didn't want to have to have three separate lookup tables it seemed inefficient

so if the program calls the 'Fetch' subroutine
which then determines which digit to do first (always huns)

it places the huns number (offset index) into a temp register then places that in WREG (that way each digit can use the same temp reg)
-MOVFF is just move from file reg to file reg..

once thats done the number in WREG (formally the index offset 'huns') is added to PCL which SHOULD move to the desired address in the table
the RETLW will return to the main program where it will store that new output port sequence in huns for subsequent multiplexing

the sequence is then repeated with the digits 'tens' and 'ones'

This is why the GOTO LOOKUP has been used, the program was intentionally designed to goto there, as that part of the code was specifically designed to occur within the same call function.
 
The reason your code is only jumping by 3 instructions instead of your expected 6 is that the PC in the 18F device increments by 2 every instruction. You could multiply the offset by 2 before adding to PCL if you wanted. For lookups on 18F devices, you should consider using the table read instructions.
 
thanks dougy83 you were right, though instead of altering my entire code to accommodate TBLPTR instruction i just added a RLCF instruction just before moving temp to WREG and its working perfectly now
 
I'm glad it's working.

You may choose to use table read in the future. Using them is not difficult; they are also more storage efficient, execution efficient, and allow built-in post/pre increment/decrement.
 
Status
Not open for further replies.

Latest threads

Back
Top