1. 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.
    Dismiss Notice

Bank switching and FSR

Discussion in 'Microcontrollers' started by throbscottle, Feb 11, 2018.

  1. throbscottle

    throbscottle Well-Known Member

    Joined:
    Feb 1, 2012
    Messages:
    1,892
    Likes:
    118
    Location:
    Nuneaton, Warwickshire, UK
    Quick question about PIC assembly
    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 (text):
    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 (text):
    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.
     
  2. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,660
    Likes:
    409
    Location:
    Brisbane Australia
    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.
     
  3. Wp100

    Wp100 Active Member

    Joined:
    Feb 3, 2009
    Messages:
    990
    Likes:
    45
    Location:
    England
    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
     
  4. dave miyares

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    2
    Likes:
    -10


     
  5. dr pepper

    dr pepper Well-Known Member Most Helpful Member

    Joined:
    Oct 6, 2008
    Messages:
    5,073
    Likes:
    301
    Location:
    North west UK
    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.
     
  6. atferrari

    atferrari Well-Known Member

    Joined:
    Oct 8, 2003
    Messages:
    2,898
    Likes:
    126
    Location:
    Buenos Aires - Argentina
    ONLINE
    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.
     
  7. throbscottle

    throbscottle Well-Known Member

    Joined:
    Feb 1, 2012
    Messages:
    1,892
    Likes:
    118
    Location:
    Nuneaton, Warwickshire, UK
    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.
     
  8. dave miyares

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    2
    Likes:
    -10


     
  9. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,556
    Likes:
    690
    Location:
    Derbyshire, UK
    ONLINE
    Try the 16F1827, an enhanced 16F device, which allows you to use the RAM banks as a single continuous addressable space.
     
  10. throbscottle

    throbscottle Well-Known Member

    Joined:
    Feb 1, 2012
    Messages:
    1,892
    Likes:
    118
    Location:
    Nuneaton, Warwickshire, UK
    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.
     
  11. dr pepper

    dr pepper Well-Known Member Most Helpful Member

    Joined:
    Oct 6, 2008
    Messages:
    5,073
    Likes:
    301
    Location:
    North west UK
    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.
     
  12. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,556
    Likes:
    690
    Location:
    Derbyshire, UK
    ONLINE
    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 :D

    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.
     
  13. dr pepper

    dr pepper Well-Known Member Most Helpful Member

    Joined:
    Oct 6, 2008
    Messages:
    5,073
    Likes:
    301
    Location:
    North west UK
    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.
     
  14. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    5,191
    Likes:
    156
    Location:
    morristown,tn
    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.
     
  15. dr pepper

    dr pepper Well-Known Member Most Helpful Member

    Joined:
    Oct 6, 2008
    Messages:
    5,073
    Likes:
    301
    Location:
    North west UK
    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.
     
  16. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    5,191
    Likes:
    156
    Location:
    morristown,tn
    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.
    Screenshot from 2018-02-13 01-14-56.png
     
  17. throbscottle

    throbscottle Well-Known Member

    Joined:
    Feb 1, 2012
    Messages:
    1,892
    Likes:
    118
    Location:
    Nuneaton, Warwickshire, UK
    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...
     
  18. Ian Rogers

    Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,753
    Likes:
    954
    Location:
    Rochdale UK
    ONLINE
    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!!
     
  19. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,556
    Likes:
    690
    Location:
    Derbyshire, UK
    ONLINE
    I didn't think XC8 did C++?, where XC32 does? - perhaps I'm wrong?.

    So what's the mouse click?.
     
  20. Ian Rogers

    Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,753
    Likes:
    954
    Location:
    Rochdale UK
    ONLINE
    Nope! You are not wrong. I see they have bundled XC32 and XC32++ into one compiler... XC8 and XC16 are just C..
     
  21. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,556
    Likes:
    690
    Location:
    Derbyshire, UK
    ONLINE
    Good, I'm not crazier than I though I was! :D

    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.
     
  22. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    5,191
    Likes:
    156
    Location:
    morristown,tn
    Not what they say but I does let you I've seen it in code on the mplab express

    Code (text):

    #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
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice