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.

Using PCL in sub-routine

Status
Not open for further replies.

ibwev

Member
I think I am having trouble with using Program Counter Latch in the Special Function Register using Assembly language with PIC16F690. In the attached file, it works as expected. When I include it in a longer program (a little less than 1,400 lines of code), I suspect it is jumping to unexpected lines of code. It was working as expected when I was using the PicKit 2 Debugger with my circuit on a breadboard; however, it does not work as expected when I write the code to PIC16F690 embedded on a PCB board. It appears all the rest of my code is working correctly with the exception of the part of the code involved with the PCL subroutine. Might the "LightSet" subroutine in the attachment not work in lengthier code when using the MPASM Assembler v 5.50 compiler?
 

Attachments

  • PCtest.asm
    1.4 KB · Views: 162
you need to adjust the high byte of the program counter as well.

try,
Code:
LightSet
    movwf    temp        ;save w
    movlw    high(tab)   ;corrected as pointed out by ibwev
    movwf    pclath
    movlw    low(tab)
    addf    temp,w
    btfsc    STATUS,C
    incf    pclath,f
    movwf    pcl 
    ;;;;;;;;;addwf    PCL,1    ;add results to program counter and move to that line
tab    retlw    .255    ;filterDirt assigns W before calling LightSet, it can not = 0
                    ;in other words, this line will never be called
    retlw    .10    ;button pressed 11 times (5 blink cycles)
    retlw    .8    ;blinks are multiplied by 2 to factor pause
    retlw    .6
    retlw    .4
    retlw    .2
    retlw    .22
    retlw    .20
    retlw    .18
    retlw    .16
    retlw    .14    ;button pressed once, 7 blinks
    retlw    .12    ;button not been pressed

Mike.
 
Last edited:
you need to adjust the high byte of the program counter as well.

Since my code is less than 1,400 lines, why is it necessary to adjust the high byte of the program counter. Shouldn't all the code be on page 0 since it is less than 2-k words?
 
Since my code is less than 1,400 lines, why is it necessary to adjust the high byte of the program counter. Shouldn't all the code be on page 0 since it is less than 2-k words?

Yes it is in Page 0 but each page is 2Kwords (11 bit address). The PCL register is the low 8 bits of the program counter (PC) so you need to set PCLATH with the high bits. When PCL is written the data in PCLATH is copied to the high order bits of the program counter to form a complete address.

The GOTO and CALL instructions supply 11 bits of address in the instruction which is where the 2K page size comes from.
 
you need to adjust the high byte of the program counter as well.

try,
Code:
LightSet
    movwf    temp        ;save w
    movlw    high([COLOR=#ff0000]temp[/COLOR])
    movwf    pclath
quote]
 
 
Side note for any future readers.  I think "tab" was meant instead of  "temp".  Program is now working great. Thanks again for the help.
 
Side note for any future readers. I think "tab" was meant instead of "temp". Program is now working great. Thanks again for the help.

The variable temp is required, changing it to tab will cause random crashes.

Edit, just saw what you mean and you are correct, movlw high(temp) should be movlw high(tab). I'll edit it now.

Mike.
 
Status
Not open for further replies.

Latest threads

Back
Top