Proplem with MPLAB and assembly

Discussion in 'Embedded Systems and Microcontrollers' started by steinar96, Jul 9, 2009.

  1. steinar96

    Thread Starter Active Member

    Apr 18, 2009
    I'm having some proplems with MPLAB which i hope someone has run into and solved. I have this code here

    INCLUDE ""


    org 0
    nop ; Required for MPLAB ICD2

    bsf STATUS, RP0 ;Let's select bank 1
    bcf STATUS, RP1

    movlw 0x00 ;Set all ports as output
    movwf TRISA

    bcf STATUS, RP0 ;Switch back to bank 0

    movlw 0xFF ;Put logic high on all A ports
    movwf PORTA ; ------

    Start ;Loop
    goto Start


    It's quite simple as you see. Simply puts all A ports on logic high and loops forever afterwards.

    If i look at the machine code MPLAB generates it seems it replaces TRISA with PORTB everywhere. So between the bank changes during the beginning of code execution it actually performs movlw, mowlwf on PORTA (which isnt even in bank1) instead of using the correct address of 85h for TRISA it uses 05h (PORTA) even though the asm explicitly states that i want to use TRISA register.

    This is extremely wierd and quite annoying. Why would it replace all references to TRISA (at address 85h) with reference to 05h (address of PORTA). The toolsuite i'm using is the MPASM assembler which comes with MPLAB.

    Any help or advice greatly appreciated
  2. AlexR

    Well-Known Member

    Jan 16, 2008
    What you are seeing is paging. PIC16 instructions are 14 bits wide and only 7 bits are available for the register addressing. That is why you have to set the relevant page in the status register. There is no way an instruction could address memory location 85h with only 7 bits so you select the second page with the status register and address location 05h which turns out to be memory location 85h.

    By the way, if your code does not seem to work its probably because you forget set-up port a as a digital port.
    Last edited: Jul 9, 2009
  3. steinar96

    Thread Starter Active Member

    Apr 18, 2009
    I find it likely that that could be the proplem. I've already written what i needed in C but i'm trying to get a good grip on assembly nevertheless.

    Yep. I missed the part where i need to modify the ansel register to set the ports up for digial I/O.

    Thank you :)
    Last edited: Jul 9, 2009