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.

Adapting Code from PIC16 to PIC18

Status
Not open for further replies.

CR33PN

New Member
Hi,

I'm having problems adapting a set of LCD routines found on from the 16F628 to a 18F4520.

The line I am having trouble with is:

Text addwf PCL, f

For a start I don't quite understand what the f signifies and the problem that it doesn't add the working register to the Program counter low, but just jumps to some other random line after that.

Anyone who can shed some like on my problem would be greatly appreciated.

Thanks in advance.

HTML:
 

Attachments

  • LCD 16F4520.asm
    4.8 KB · Views: 210
Addwf PCL,f adds the working register to PCL and stores the result back in the File (PCL) register. Had it been addwf PCL,w then the result would have been placed in the Work register. To convert this code to 18 series is a little tricky as each location is counted as two bytes and so instructions are only stored on even byte boundaries and you have to add twice the value to PCL to get the same result as a 16 series chip. If you post what you have there may be a simple work around.

Edit, didn't notice you have posted your code. I'll take a look.

Mike.
 
Last edited:
The F signifies that the result of adding W to PCL goes into PCL. If you had a W instead of the F, the result would go into W.
It is possible that your table is crossing a boundary. You can have the compiler check that for you by doing this:
Code:
Table1: 
        ADDWF   PCL, f   ;jump forward by value of W 
        retlw   'A' 
        retlw   'B' 
        retlw   'C' 
        retlw   'D' 
        retlw   'E' 
        retlw   'F'  ;end of table 
 IF HIGH($)!=HIGH(Table1) 
 ERROR "End of table is not in same page as start of table" 
 ENDIF 

;It will give an error at compile time if it crosses that boundry.
 
Last edited:
A simple bodge would be to add a variable to mutiply by two at the start of each table and setup PCLATH. You also need to make sure each table doesn't cross a (256 byte) page boundary.

Code:
		org	0x200

HEX_Table  	movwf	bodge		;save W
		movlw	high $		;setup PCLATH
		movwf	PCLATH
		rlncf	bodge,w		;Mutiply by 2
		ADDWF   PCL , f		;and add to PCL
           	RETLW   0x30
           	RETLW   0x31
           	RETLW   0x32
           	RETLW   0x33
           	RETLW   0x34

Edit, you don't need the org. But you will if your code extends past 0x200 and you have a table straggling the boundary.

Mike.
 
Last edited:
BTW, you will need to write 0x0f to ADCON1 to make your ports digital - they default to analogue.

Mike.
 
It loads the work register (W) with the high byte of the program counter.

Mike.
 
If it's any help, I've also been doing some work converting the tuorials to 18F, here's a string table example from Tutorial 3.3

Code:
Text		movwf 	taboff 			; save table offset
		bcf 		STATUS,	C 		; clear STATUS bit
		rlcf 		taboff,	F 		; multiply by 2, save in taboff
		movlw 	HIGH(String1) 		; get high byte of table start
		btfsc 	STATUS,	C 		; test carry bit
		incf 		WREG,	W
		movwf 	PCLATH 			; modify PCLATH if required
		movlw 	LOW(String1) 		; get low byte of table address
		addwf 	taboff,	W 		; add in offset
		btfsc 	STATUS,	C 		; test for overflow
		incf 		PCLATH,	F 		; increment if needed
		movwf 	PCL 				; make jump, PCLATH and PCLATU are
								; written to PCH and PCU
String1	
		retlw	'1'
		retlw	'6'
		retlw	' '
		retlw	'B'
		retlw	'i'
		retlw	't'
		retlw	' '
		retlw	'C'
		retlw	'o'
		retlw	'u'
		retlw	'n'
		retlw	't'
		retlw	'e'
		retlw	'r'
		retlw	'.'
		retlw	0x00
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top