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.

Auto Bank Selection in Assembly

Status
Not open for further replies.
I'm wondering if it is possible to enable some feature in MPLAB/use some external program to make my assembly code automatically select the correct bank for the register which I am trying to read from or write to.

The way I figure it, all 16F PICs select banks through either the status register or the bank select register (with a MOVLB command), and W is global, so it doesn't seem like it would be hard to make the assembler automatically insert bank selection commands. In my opinion, the biggest pain of coding in assembly is having to constantly refer to your datasheet to see what bank the register you are operating on is in, and then using BANKSEL or changing the status/BSR to get to it. I think if something could do this for you, it would speed up asm coding at least 20% or 25%.

Here's an example:
Say ADRESH is in bank 2 and I want to copy its contents to a user file called ADRES_HOLD that I created in bank 0.

Code:
I'd like to be able to do this:

MOVF     ADRESH,W
MOVWF  ADRES_HOLD

And have something that auto generates this:

MOVLB   0x02    ;select BANK 2
MOVF     ADRESH,W
MOVLB   0x00    ;select BANK 0
MOVWF  ADRES_HOLD

Is there anything out there that can do this for me?

Thanks!
 
Hi,

Understand your frustration but if there was such a fix I am sure we would have heard about it by now.

You might want to consider using the larger 18F chips which allow you to use 384 bytes of ram before changing banks.
Also you have no Page boundary issues like the 16Fs
 
Only way around this is coding in C, which you should be doing if you are worried about development time. At least the newer PICs have movlb instead of playing around with status bits!
 
Thanks for the info guys. I will be migrating over to C as soon as I can understand it better. For whatever reason, C seems much more cryptic and less intuitive than assembly. IMO it was easier for me to learn assembly, with no programing experience, than it is for me to learn C with a few months of coding in asm.

This may sound crazy, but once I learn C I might just write a program where you paste in all the registers by bank and then the code and then it spits out the code for you with bank selection.

I imagine the executable prompt as follows:

How many banks does your PIC have?
<user enters number of banks>

Enter the SFRs in bank 0 just as they are refered to in the datasheet.
<user copies and pastes bank 0 FSRs into prompt window>

(Same for banks 1 through n)

Enter the user file names assigned to RAM locations common to all banks.
<user enters names of all user file names defined to global RAM positions>

How many banks are you using "name assigned" RAM that is bank specific?
<user enters answer>

Enter user file names that are specific to bank 0. Enter "none" if there are none, and enter "finished" if there are no more user defined names for bank specific RAM.
<user copies user defined RAM names in bank 0 into prompt window>

(Do above until user enters finished or all banks have been completed)

Copy in your assembly program below and press enter.
<user copies in program hits enter, and program inserts bank selection commands in subroutines where needed>

(The program will start in bank 0, and then look for "MOVLW" and "MOVF". When it seems this, the program will compare the destination file to the bank which is currently selected and insert a BANKSEL directive into the user's code prior to the next command if necessary.

Anyone see a problem with that?
 
Last edited:
Code:
I'd like to be able to do this:

MOVF     ADRESH,W


And have something that auto generates this:

MOVLB   0x02    ;select BANK 2
MOVF     ADRESH,W

You could use a macro to do something like that. That would let you use one simple line to access the variable and the macro would generate the 2 or 3 lines needed;

Code:
You would type this;

MOVMACW    ADRESH

And you macro auto generates this:

BANKSEL ADRESH
MOVF     ADRESH,W
BANKSEL 0             ; optional line, always leaves the code back on bank 0
 
Why not just use the banksel assembler directive?

Say you want to configure Port A as all input -

Code:
movlw          0xFF
banksel        TRISA
movwf          TRISA
Now to go back to Bank 0, use banksel and the name of a register in Bank 0 -

Code:
banksel       PORTB
You can use the name of any register in the bank you wish to select with the banksel directive. The only catch is that you cannot label a line containing the banksel directive.

You can even do it by using the hex address of any Bank 0 or Bank 1 address. Here I use 0x00 for Bank 0 and 0x80 for Bank 1 -

Switch to Bank 1 -

Code:
movlw          0xFF
banksel        0x80
movwf          TRISA

Switch to Bank 0 -

Code:
banksel       0x00
 
Last edited:
Not sure if Banksel works with new PICs which have the banksel register!

Somebody please confirm / deny :D

Banksel isn't a register. It's an assembler directive which MPASM works with to switch banks.
 
Here ya go
 

Attachments

  • BSR 16F193X.jpg
    BSR 16F193X.jpg
    134.4 KB · Views: 380
You could always just use AVR's and not worry about it ;) <blatant plug>
 
Birdman:

BANKSEL still works like a charm on the enhanced midranges.

I'm assuming where the assembler would change BANKSEL to a status bit manipulation command on the regular midranges it's just using a MOVLB now. BANKSEL helps a lot because I don't have to actually know which bank the register is in, but you still have to refer to a sheet or else you will be "BANKSEL"ing everything and IMO bloating your code.

Jon:
As Birdman said, the problem is the large number of registers and banks. I think some of the upper echelon of the enhanced midranges have THIRTY TWO banks, though I think alot of them are for shadow registers.

--------------------------------------------------------------
If some one wrote a program that does like I mentioned earlier, I'd be willing to pay for it! Something tells me that if a skilled programmer were to write something that was user friendly, he could sell it for 5 or 10 bucks a copy and attract a lot of interest from assembly coders. I know I'd be your first customer. I wouldn't imagine it taking more than a few hours for a guy good with writing programs in a high level language to do.
 
Ah you want the code for the sake of the code, problem is assembly language isn't compiled it's assembled. Higher level languages take care of bank switching and modern PICs don't rely on it so much. You would also have to write a custom multipass assembler that could insert RPx code as needed but that could and would cause havoc among branching and any timing sensitive routines. It would be a mess at best.

So folks that don't like bank switching...
use a compiler C, BASIC
upgrade to the 16bit core
became careful coders
switched to AVR
 
Last edited:
Wow...I wasn't aware of these new 16F series pics! Looks like they've got more resources than I could ever hope to use. That 16F1933 looks like it could give an 8031/8051 a run for its money.

In any event I just wrote a test code for the 16F1933 using banksel. Then I assembled it, then disassembled it. It looks like banksel does indeed do what it does for the other 16F series PICs, however instead of it inserting code that sets/clears STATUS RP0/RP1 register bits, banksel directs the assembler to do a MOVLB instruction based on which bank the register you're banksel'ing to resides in.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top