A problem of bank selection 16F877A

Discussion in 'Embedded Systems and Microcontrollers' started by anhnha, May 17, 2012.

  1. anhnha

    Thread Starter Active Member

    Apr 19, 2012
    773
    45
    Hi everyone,
    I am programming for pic 16F877A in PICC. The program is writen in assembly, There is a strange that make me confused:
    In order to set all pins of port B are output , I used two command:
    MOVLW 0x00 // assign W=0X00
    MOVWF TRISB // set all bit of trisb to zero
    but when I watch in list file after compilation, a feel confused when it is like that:
    MOVLW 00 // assign W=0X00
    BSF 03.5 // set RP0 bit to choose bank1 contain TRISB
    MOVWF 06 // Move W to f
    I think it have to MOVWF 86 because 86 is address of TRISB , 06 is address of PORTB.
    But my program still work exactly.
    Would you help me explain why it is happen like that?
    Thanks.
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Hopefully by now you have found the data sheet for the 877A. If you look at "FIGURE 2-3: PIC16F876A/877A REGISTER FILE MAP" you can see the special function registers and memory registers are placed in 4 "banks."

    Why? The entire memory address runs from 0x000 to 0x1FF, which takes 9 bits to hold. If you peek into "TABLE 15-2: PIC16F87XA INSTRUCTION SET" you can see the instructions only have place to hold 7 bits to specify a register.

    So where do the other 2 bits come from? The "Register Bank Selection" bits of the Status register. See "2.2.2.1 Status Register" Also note in Fig 2-3 the Status register appears in all banks, so it is always accessible.

    You could write your own code to set the bits as needed, or you can use the banksel [label] macro. That macro takes the register of [label] and makes the correct code to set the bank bits. Look for it in the MPASM Assembler Help file found under MPLAB's help menu.
     
  3. anhnha

    Thread Starter Active Member

    Apr 19, 2012
    773
    45
    Thanks a lot.
    I got it now.
     
Loading...