# Pic Programming

Status
Not open for further replies.

#### Beth

##### New Member
Hi there,

Just found this site and am hoping that someone might be able to help me with a pic query!

label
retlw 05H
retlw 0FH
retlw 14H
etc......

Is it possible to move the address of the first instruction (e.g. retlw 05H) of a rountine (named "label") into the working reg using the following...

movlw label

And then move this value from the working reg into the PCL...

movwf PCL

to 'jump' to that part/instruction in the program.

I know that this is similar to a goto but i require it for a large kinda lookup table.

Apologies if this is a really silly question. Still getting the hang of the whole pic programmnig thing!!

Cheers all

#### Nigel Goodwin

##### Super Moderator
Most Helpful Member
Beth said:
Hi there,

Just found this site and am hoping that someone might be able to help me with a pic query!

label
retlw 05H
retlw 0FH
retlw 14H
etc......

Is it possible to move the address of the first instruction (e.g. retlw 05H) of a rountine (named "label") into the working reg using the following...

movlw label

And then move this value from the working reg into the PCL...

movwf PCL

to 'jump' to that part/instruction in the program.

I know that this is similar to a goto but i require it for a large kinda lookup table.

Apologies if this is a really silly question. Still getting the hang of the whole pic programmnig thing!!

Cheers all

I'm not very clear about what you are trying to do, but you can make a table of goto's - it works like this:

Code:
addwf   PCL, f
goto	SwitchC
goto	SwitchB
goto	Switch0
goto	SwitchA
goto	SwitchD
goto	Switch9

You call the routine with W holding the value of the routine you want, in this particular case it's showing part of a table for 16 different key presses. Although the PIC limits the size of a single table to 256 bytes, this isn't totally true - an article in EPE showed how to use a large part of a 16F877 as a big table to store grahics.

#### Exo

##### Active Member
Beth said:
Hi there,

Just found this site and am hoping that someone might be able to help me with a pic query!

label
retlw 05H
retlw 0FH
retlw 14H
etc......

Is it possible to move the address of the first instruction (e.g. retlw 05H) of a rountine (named "label") into the working reg using the following...

movlw label

And then move this value from the working reg into the PCL...

movwf PCL

to 'jump' to that part/instruction in the program.

I know that this is similar to a goto but i require it for a large kinda lookup table.

Apologies if this is a really silly question. Still getting the hang of the whole pic programmnig thing!!

Cheers all

An program adress consists of 2 bytes. So you can't put a complete adress into W reg or PCL.

To modify the program counter directly you must first load register PCLATH with the highest bits of the adress <3:0> (an adress = 14 bits) where it will be kept on hold. Then load PCL with the lowest bits of the adress <7:0>. Once you write to PCL execution jumps to the complete adress given in PCLATH<3:0>CL<7:0>

Do note that jumping to a lable this way DOES NOT!! put the return adress on the stack. so if you jump to a adress this way, and then do a RETURN, execution will continue to the previous adress pushed on the stack with a normal CALL, not from the adress wich loaded PCL directly

Example: Jump to MyLabel directly by loading PCLATH and PCL
Code:
MOVLW     HIGH(MyLabel)    ;copy high part of MyLabel adress to W
MOVWF     PCLATH           ;PreLoad lath with this value - this does not jump yet
MOVLW     LOW(MyLabel)     ;copy low part of MyLabel adress to W
MOVWF     PCL              ;load PCL with this value - NOW the jump occurs
NOP                        ;This does not get executed ...

MyLabel:  ;Some Code here

#### Beth

##### New Member
Hi Nigel,

Thank you very much for the info. I am outputting a number of custom characters to an LCD display (which i have defined in the CGram).

I am using lookups to send certain charaters at certain times in certain positions, and was trying to find a way to shrink down my code (by only using two routines to retrieve and send screen data).

The lookup table of goto's might solve it. I'll have a go.

Kind Regards

#### Beth

##### New Member
Cheers Exo,

I had forgotten that the PCL is stored over two registers. Thank you for the info. This might work.

#### motion

##### New Member
I have done similar work and here's how I did it.

Code:
;-----------------------------------------------------------
BOOT_HELLO:
MOVLW    HELLO_BEG-HELLO_END
MOVWF    TEMP3
BOOT_HELLO2:
CALL     GET_HELLO
CALL     LCDWRITE_REG
;
DECFSZ   TEMP3,F
GOTO     BOOT_HELLO2
;
RETURN
;-----------------------------------------------------------
GET_HELLO:
MOVLW    HIGH HELLO_END
MOVWF    PCLATH
;
DECF     TEMP3,W
ADDWF    PCL,F
HELLO_END:
RETLW    'O'
RETLW    'L'
RETLW    'L'
RETLW    'E'
RETLW    'H'
HELLO_BEG:

Just make sure the address from label HELLO_END up to HELLO_BEG does not cross a 256 address page boundary. There are ways to automatically detect that.

#### Beth

##### New Member
Hello Motion,

Cheers for the info. This has pretty much done the job. Screens up and running.

Thank you

Status
Not open for further replies.

Replies
18
Views
2K
Replies
0
Views
1K
Replies
6
Views
5K
Replies
1
Views
1K
Replies
4
Views
2K