Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Designing a subroutine with the RETLW command in MPlab (PIC 16F877A)

Status
Not open for further replies.

lameturd

Newbie level 1
Joined
May 8, 2010
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Australia
Activity points
1,289
hi, I am new to this PIC stuff and I have been recently given this task:

"
Using the program counter, specifically the register PCL and the command RETLW design a subroutine that will scroll through a list of numbers and pick out the xth value entry from the list. (hint: research look up tables)
"

I am sure this is very easy for you guys but could you please show me how its done and tell me what each line does? I am using MPLab.
thanks
 

Re: PIC 16F877A help!

The PIC series allow direct access to the program counter so you can use it to store the result of a calculation. The RETLW instruction means "RETurn with Literal in W", in other words it is a subroutine return instruction that carries the value you placed in the instruction beck in the W register.

What you need to do is calculate a subroutine address from your 'x' value, call it and return with correct number in W, in other words use it as a look-up table. You call it with 'x' in W and it returns with the 'x'th value in W.

It's very easy to do and very efficient in code usage:

1. put 'x' in the W register
2. call the subroutine (must be a CALL, not a GOTO !)
3. the subroutine contains this:

addwf PCL,f ;add w to the PCL value so the next instruction is W places ahead.
retlw <value 1>
retlw <value 2>
.
.
retlw <value b>

What you are doing is carrying the offset 'x' into the subroutine, there you add it to the present program counter value. Because the program counter holds the address of the next instruction to execute, it jumps ahead 'x' places. At the 'x'th address you place your RETLW instruction which returns back to the main program with the new value in W.

Warning: PCL is an 8-bit register so you have to be careful that when you add 'x' to it, you don't overflow and end jumping to the wrong address. There are tricks to avoid this, the simplest is to place the subroutine at a page boundary so no matter what value you call it with, you never exceed the length of the page. the other is to check for overflow and if necessary, increase the value in PCLATH which is effectively the higher half of the memory address.

Brian.
 

Re: PIC 16F877A help!

The PIC series allow direct access to the program counter so you can use it to store the result of a calculation. The RETLW instruction means "RETurn with Literal in W", in other words it is a subroutine return instruction that carries the value you placed in the instruction beck in the W register.

What you need to do is calculate a subroutine address from your 'x' value, call it and return with correct number in W, in other words use it as a look-up table. You call it with 'x' in W and it returns with the 'x'th value in W.

It's very easy to do and very efficient in code usage:

1. put 'x' in the W register
2. call the subroutine (must be a CALL, not a GOTO !)
3. the subroutine contains this:

addwf PCL,f ;add w to the PCL value so the next instruction is W places ahead.
retlw <value 1>
retlw <value 2>
.
.
retlw <value b>

What you are doing is carrying the offset 'x' into the subroutine, there you add it to the present program counter value. Because the program counter holds the address of the next instruction to execute, it jumps ahead 'x' places. At the 'x'th address you place your RETLW instruction which returns back to the main program with the new value in W.

Warning: PCL is an 8-bit register so you have to be careful that when you add 'x' to it, you don't overflow and end jumping to the wrong address. There are tricks to avoid this, the simplest is to place the subroutine at a page boundary so no matter what value you call it with, you never exceed the length of the page. the other is to check for overflow and if necessary, increase the value in PCLATH which is effectively the higher half of the memory address.

Brian.

hi brian just writing to thank you for your explanation of "retlw",it has helped clear up some points that i was grasping regards john
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top