Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 13th October 2009, 04:15 AM   #1
Default Adapting Code from PIC16 to PIC18

Hi,

I'm having problems adapting a set of LCD routines found on http://www.winpicprog.co.uk/pic_tutorial3.htm 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.

Attached Files
File Type: asm LCD 16F4520.asm (4.8 KB, 10 views)
CR33PN is offline  
Old 13th October 2009, 05:54 AM   #2
Default

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 by Pommie; 13th October 2009 at 05:55 AM.
Pommie is offline  
Old 13th October 2009, 05:58 AM   #3
Default

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.
__________________
Inside every little problem, is a big problem trying to get out.

Last edited by kchriste; 13th October 2009 at 06:00 AM.
kchriste is offline  
Old 13th October 2009, 06:10 AM   #4
Default

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 by Pommie; 13th October 2009 at 06:17 AM. Reason: Added comments.
Pommie is offline  
Old 13th October 2009, 06:20 AM   #5
Default

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

Mike.
Pommie is offline  
Old 15th October 2009, 04:17 AM   #6
Default

OK I think it may have worked but can you please exaplain what this line does:

movlw high $
CR33PN is offline  
Old 15th October 2009, 04:20 AM   #7
Default

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

Mike.
Pommie is offline  
Old 15th October 2009, 07:57 AM   #8
Default

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
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Reply

Tags
adapting, code, pic16, pic18

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
about PIC18 assembly code fengshao Micro Controllers 13 8th May 2009 01:58 PM
Pic16 Family ahmaaa Micro Controllers 7 14th March 2009 03:18 PM
PIC18 C Code for Dragonfly ThiSel Micro Controllers 5 3rd March 2008 04:19 PM
Adapting LCD code for 16F876 bh00 Micro Controllers 12 22nd February 2007 04:03 PM
adc0804 interface code for pic18 ulas Micro Controllers 5 27th June 2006 08:40 AM



All times are GMT. The time now is 11:04 PM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker