Search code examples
debuggingmicrocontrollermicrochipmplab

Wrong value in the working register after a MOVF operation


How do I resolve obtaining the wrong value in the W-register after a MOVF operation?

I am debugging a code which builds & programs successfully, but does not have the desired result when powered up as part of the circuit and I have noticed that the working register does not contain the right value after a MOVF operation, whilst debugging.

If the value that is being copied to the W-reg is manually written in during debugging, then the code functions as it should.

The following images, I believe, illustrate the my issue quite well.

enter image description here

1. Port definition.

enter image description here

2. LEDportA value of 0x02 is to be moved to the W-reg.

enter image description here

3. After the operation, W-reg contains 0x00, instead of 0x02.

enter image description here

4. Final image shows that LEDportA is the same as LATA, as it is cleared after the "clrf LEDportA instruction.

I am totally bewildered as to what could be causing it and any insights or advice that anyone can provide will be very much appreciated.

Please note that the PIC MCU in use is the PIC16F1829.


Solution

  • Since you don't provide enough information like PIC model you are using and the rest of the code, as far as I see, your problem is because of wrong bank selection. Your TempC register is located 0x70 in bank 0, and the LATA register is located at 0x10C bank 2 in memory. Thus when you attempt to read the LATA, actually you read the corresponding 0C addres location in bank0. You have to switch the correct bank before you attempt to read or write from any register in RAM. Check the code snippet that has right way to access to the registers. You can switch to a bank either using the BANKSEL directive which is more convenient for programmers or loading a bank value to the BSR (Bank Select Register).

    UpdateDisplay:
            BANKSEL     LEDportA    ; Switch to LEDportA bank before any access
            MOVF        LEDportA, w
            andlw       0x0f
            BANKSEL     TempC       ; Switch to TempC bank before any access
            movwf       TempC
            bsf         TempC, 4
            rrf         TempC, F
            btfss       STATUS, C
            bcf         TempC, 3
            btfsc       TempC, 0
            ....