# Bank switching and FSR

#### throbscottle

##### Well-Known Member
If I've got FSR pointing at a variable in bank1, and I need to (for example) copy the contents of INDF to a variable in bank2...
Does FSR get around bank switching?

so do I need
Code:
banksel bank2var
... do stuff in bank2
bansksel INDF ; value is in a bank1 variable
movfw INDF
bankssel bank2var
movwf bank2var
banksel bank1var
... do stuff in bank1
or can I get away with
Code:
banksel bank2var
... do stuff in bank2
movfw INDF ; value is in a bank1 variable but INDF is /magical/
movwf bank2var
banksel bank1var
... do stuff in bank1
I'm really hoping it's the latter because I've got a lot of bank switching otherwise can't get round it because the banks are right to avoid it in most of the code.

#### Pommie

##### Well-Known Member
You can't banksel INDF as it's in all banks.

On the pic16 range FSR is only 8 bits and points to the currently selected bank.
In the new enhanced range the FSR is 16 bit and much more flexible.
Some of the enhanced ones have two indirect registers so you can copy between banks very easily.

Mike.

#### Wp100

##### Well-Known Member
Agree, look at the newer Pic16F chips or try the 18F range, very similar /99% compatible with 16F code but just single area of flash memory, no pages, and large ram banks, 256 bytes per bank.
The 18F also has additional instructions like MOVFF

#### dr pepper

##### Well-Known Member
Banksel indf will do something odd, or nothing like already mentioned indf is in all banks.
If you have the bank you want to select pointed at by indf, copy it to a file register first then mask it & overwrite the bank select bits (logical or) with it.
Be carefull I've got totally lost doing stuff like this.

#### atferrari

##### Well-Known Member
Banksel indf will do something odd, or nothing like already mentioned indf is in all banks.
If you have the bank you want to select pointed at by indf, copy it to a file register first then mask it & overwrite the bank select bits (logical or) with it.
Be carefull I've got totally lost doing stuff like this.
That is where simulation fits nicely. Seeing what registers change to red at every step allow visualizing quickly if it is working or not. Talking of MPLAB 8.92 here.

#### throbscottle

##### Well-Known Member
Ah, so I need to banksel whatever bank INDF is pointing to. Ho Hum...

I'm using (still trying to use them!) an ancient baseline pic I got years ago which is just right for the job (lots of I/O, not much brains), so I'm kind of married to it.
But for future use I will get the newer ones... Might even try and learn c.

#### Nigel Goodwin

##### Super Moderator
Ah, so I need to banksel whatever bank INDF is pointing to. Ho Hum...

I'm using (still trying to use them!) an ancient baseline pic I got years ago which is just right for the job (lots of I/O, not much brains), so I'm kind of married to it.
But for future use I will get the newer ones... Might even try and learn c.
Try the 16F1827, an enhanced 16F device, which allows you to use the RAM banks as a single continuous addressable space.

#### throbscottle

##### Well-Known Member
Wow, does everything!
I will have to learn how to use the SPI channels. I've been bit-banging pins to do it. Works but annoying.

#### dr pepper

##### Well-Known Member
You dont have to banksel all the time, however its good programming if you do, if you go back and alter the program and you change bank, then elsewhere things ,ight get all messed as you werent thinking that at the time.
Learn C you'll wonder why you didnt do it sooner.

#### Nigel Goodwin

##### Super Moderator
Learn C you'll wonder why you didnt do it sooner.
I'm 'managing' in C now, can't say I'm good at it - but it's taken a good few decades, and multiple attempts, to get to a useable level.

My programming experience started with 6502 machine code (on a Tangerine Microtan), hand assembling the code and inputting the hex values. Next was a move to a single pass assembler, then a two pass assembler, then finally a 10K MicroSoft BASIC interpreter. Next was a Commodore Plus4, more 6502 assembler and BASIC, then an Amiga 500 - a little 68000 assembler, some BASIC, and I actually bought (at great expense) the Lattice C compiler for it - it didn't go well, and eventually I sold it to a friend.

Next would be PC's, where I dabbled in more BASIC and learned Pascal - Pascal was no problem, tried C again - failed again

I actually spent a considerable amount of time converting C code to Pascal (most books used C, very few used Pascal), so I learnt a little through doing that.

Next would have been Delphi, which was basically a useable Pascal for Windows (the actual Pascal for Windows was absolutely abysmal), and I probably tried (and failed) a few times with C again.

It's only over the last couple of years I've started dabbling with C on the PIC, I suppose mostly because of all the useful C++ code on the Arduino?, there's so much code you can adapt to C there seems little use in all the effort of writing code for various devices and sensors in assembler when you can 'borrow' the Arduino version.

#### dr pepper

##### Well-Known Member
I started with a college 8080 processor trainer, you programmed it with dipswitches.
Then I 'progressed' to 6502 like you, only I 'wrote' code using the hex editor of an eprom programmer, I must have been bonkers.
Then I found a Z80 assembler, and eventually learned a little about unix which is written in C.
I gained some knowledge of C from industrial controllers & welding robots, but only what I had to know to fix them.
I resisted the arduino for a while, however my C++ knowledge has taken a leap in recent years, the library's with the 'duino are definitely what make it.
C progresses with the programmer, from schoolkid starting out (and there are some very clever ones), to 25 years seasoned programmers.

#### be80be

##### Well-Known Member
You do no that Mplab X can handle C++ just like C with no changes just one mouse click and xc8 and you can write both ways.

#### dr pepper

##### Well-Known Member
I've used mplab quite a bit, but always with asm not C.
Do you have to purchase Xc8, or does it some with, or is it some silly low performance student version.

#### be80be

##### Well-Known Member
The free works good it makes a little bigger code but it's way better then any I've used.
I'm going to try the pay as go by the month on some USB stuff i want do.
You just pay when you need smaller code.

#### throbscottle

##### Well-Known Member
I learnt basic on a Commodore 64 when they were just going out of fashion, therefore cheap. Next experience of writing programs was (sort of concurrently) Bash scripts, Perl, VBA and a little bit of WSH. I tried to learn C from a book but didn't see the point of pointers and got bamboozled in structures, and was completely bewildered by all those different files - headers and so forth - so sort of stopped there. Couldn't get the hang of Python at all. So I can sort of make sense of reading through C programs. Also I read "Cobol for dummies" Quite enjoyed that book...

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
I'm 'managing' in C now, can't say I'm good at it - but it's taken a good few decades, and multiple attempts, to get to a useable level.
I think you are pretty good at it.... I've seen your Graphical screen demo... It looked as if your Pascal is very good as well!!

#### Nigel Goodwin

##### Super Moderator
You do no that Mplab X can handle C++ just like C with no changes just one mouse click and xc8 and you can write both ways.
I didn't think XC8 did C++?, where XC32 does? - perhaps I'm wrong?.

So what's the mouse click?.

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
I didn't think XC8 did C++?, where XC32 does? - perhaps I'm wrong?.
Nope! You are not wrong. I see they have bundled XC32 and XC32++ into one compiler... XC8 and XC16 are just C..

#### Nigel Goodwin

##### Super Moderator
Nope! You are not wrong. I see they have bundled XC32 and XC32++ into one compiler... XC8 and XC16 are just C..
Good, I'm not crazier than I though I was!

I'm liable to do doing more XC8 as well, as I'm currently in the process of been made redundant, so I'm going to go work for a friend (who I've previously done development work for in my own time) doing servicing, development, and some product assembly.

#### be80be

##### Well-Known Member
Not what they say but I does let you I've seen it in code on the mplab express

Code:
#ifdef    __cplusplus
extern "C" {
#endif /* __cplusplus */
// TODO If C++ is being used, regular C code needs function names to have C
// linkage so the functions can be used by the c code.
#ifdef    __cplusplus
}
#endif /* __cplusplus */
#endif    /* XC_HEADER_TEMPLATE_H */
So yes it can be used same as the arduino in the same way with some mouse clicks LOL