PIC16F ASM Bank Selection - Page 1
Forum section: electronics / general

author: aless2056
author_rank: Contributor
timestamp: April 29, 2021, 01:32:32 am
content: Since when I started doing some things using uc, first the pic 16f628, then the 16f877 I never selected the bank before accessing a registered one.Did MPlab Ide 8.92 always fix it on its own or everything I have done so far has a terrible bug, but at first it is working?

author: Dabbot
author_rank: Regular Contributor
timestamp: April 29, 2021, 01:36:46 am
content: You need to ensure you are in the correct bank.I approach this by staying in bank 0. Switch out to the required bank to read / update the target registers, then back to bank 0.

author: DavidAlfa
author_rank: Super Contributor
timestamp: April 29, 2021, 06:36:43 am
content: Usebanksel registerIt's very useful, whatever the register you want to access, the assembler will select the correct banks for you.There are registers mapped to all banks.It seems you need to do some reading in the datasheet for the memory structure!

author: Lindley
author_rank: Regular Contributor
timestamp: April 29, 2021, 08:42:21 am
content: Note that Banksel is a Directive, you can find a list of them here.https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&ved=2ahUKEwj-6MvhhaPwAhXStXEKHXrwDR0QFjABegQIAhAD&url=http%3A%2F%2Fww1.microchip.com%2Fdownloads%2Fen%2Fdevicedoc%2F33014g.pdf&usg=AOvVaw0-H8QSb92O-ny9WGrqeKn6Here is a bit of code showing how it can be used.Code:startclrfPORTA; Set up I/O PortsclrfPORTBbankselTRISA ; select Bank 1movlw0xFF; make PortA inputsmovwfTRISAclrfTRISB; make PortB outputsbanksel 0 ; return to bank 0

author: westfw
author_rank: Super Contributor
timestamp: April 29, 2021, 08:57:37 am
content: If you are using a c compiler (xc8?), it will do the bank selection for you.There are important registers that aren’t in the default bank - it seems unlikely that you would have had much success programming in asm without any bank selection...

author: DavidAlfa
author_rank: Super Contributor
timestamp: April 29, 2021, 09:19:46 am
content: I assumed he's programming in asm. That's obvious in C.Usually you don't usually need to worry about these things in high level languages!

author: Dabbot
author_rank: Regular Contributor
timestamp: April 29, 2021, 09:22:32 am
content: Another alternative is to grab a couple PIC10F322. No BANKSEL required as the chip only has one bank. It's only 6 pins but that's fine for learning.

author: jpanhalt
author_rank: Super Contributor
timestamp: April 29, 2021, 11:23:12 am
content: Agree that C does it for you.In Assembly, it might be worth noting that neither the 16F877 nor 16F628 recognize "banksel." The 16F887 and later (including all enhanced 16F1xxx and 16f1xxxx) chips do.One little tidbit: The .inc file defines registers such as PORTA and LATA as offsets. So, if you are in the wrong bank and write something to affect a bit of a register in another bank, it will change that bit in the register of the current bank with the same offset.One consequence of that is when disassembling a hex file, you will get warnings about checking banks and a lot of misnamed registers in other banks. Consider the 16F877 that you use. If you are in Bank1 and write something like bsf PORTA,1, that will actually change PORTA,1 to an input, as TRISA has the same offset in that bank as PORTA has in Bank0.I write in Assembly almost exclusively for enhanced and later 16F chips. When doing anything involving banks, I keep track of the memory bank in a far right column, e.g., "<code> ; <comment> |B0 ." That avoids unnecessary use of "banksel" with almost every instruction. I also tend to use the actual instruction, "movlb <bank#> " rather than banksel. That's just a personal way to remind me of what bank I am in. Eventually, it becomes almost automatic.

author: DavidAlfa
author_rank: Super Contributor
timestamp: April 29, 2021, 03:17:07 pm
content: Are you sure? BANKSEL is an assembler thing, it translates that to BCF/BSF STATUS,RP0/RP1.The chip does nothing.In single memory bank PICs, it's ignored or translated as always bank0.

author: Lindley
author_rank: Regular Contributor
timestamp: April 29, 2021, 03:48:14 pm
content: Good point, would also have thought it was a function of the Assembler rather than the chip...?However its a very long time since we used MPLab and do not have it loaded now to check, but just in case the Banksel Directive does not work with the earlier 16F chips here is the code example from the datasheet on how the OP should SET those the Bank Bits.

author: jpanhalt
author_rank: Super Contributor
timestamp: April 29, 2021, 04:05:42 pm
content: Quote from: DavidAlfa on April 29, 2021, 03:17:07 pmI haven't used either of the chips the TS is using in ages. It was always a pain setting STATUS to change banks.Yes, I knew it was an Assembler macro, but I only checked the datasheet before posting. It is described for the later chips. I was too lazy to check the Assembler at the time, but I just checked, and banksel is handled correctly for the 16F628A with MPASM 8.92.Thanks for pointing that out.BTW, I wish Microchip would have allowed those two bits to be R/W'able in the later versions. They would sure be convenient to use as flags.