Author Topic: PIC16F ASM Bank Selection  (Read 1828 times)

0 Members and 2 Guests are viewing this topic.

Offline aless2056Topic starter

  • Contributor
  • Posts: 35
  • Country: br
PIC16F ASM Bank Selection
« on: April 29, 2021, 01:32:32 am »
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?
 

Offline Dabbot

  • Regular Contributor
  • *
  • Posts: 192
  • Country: au
Re: PIC16F ASM Bank Selection
« Reply #1 on: April 29, 2021, 01:36:46 am »
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.
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6207
  • Country: es
Re: PIC16F ASM Bank Selection
« Reply #2 on: April 29, 2021, 06:36:43 am »
Use banksel register
It'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! :-+
« Last Edit: April 29, 2021, 06:39:25 am by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline Lindley

  • Regular Contributor
  • *
  • Posts: 200
  • Country: gb
Re: PIC16F ASM Bank Selection
« Reply #3 on: April 29, 2021, 08:42:21 am »
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-ny9WGrqeKn6

Here is a bit of code showing how it can be used.

Code: [Select]
start    clrf PORTA ; Set up I/O Ports
clrf PORTB
banksel TRISA                               ; select Bank 1
movlw 0xFF ; make PortA inputs
movwf TRISA
clrf TRISB ; make PortB outputs
banksel 0                                 ; return to bank 0 



« Last Edit: April 29, 2021, 08:45:39 am by Lindley »
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4290
  • Country: us
Re: PIC16F ASM Bank Selection
« Reply #4 on: April 29, 2021, 08:57:37 am »
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...

 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6207
  • Country: es
Re: PIC16F ASM Bank Selection
« Reply #5 on: April 29, 2021, 09:19:46 am »
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!
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline Dabbot

  • Regular Contributor
  • *
  • Posts: 192
  • Country: au
Re: PIC16F ASM Bank Selection
« Reply #6 on: April 29, 2021, 09:22:32 am »
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.
 

Online jpanhalt

  • Super Contributor
  • ***
  • Posts: 3744
  • Country: us
Re: PIC16F ASM Bank Selection
« Reply #7 on: April 29, 2021, 11:23:12 am »
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.
« Last Edit: April 29, 2021, 12:00:47 pm by jpanhalt »
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6207
  • Country: es
Re: PIC16F ASM Bank Selection
« Reply #8 on: April 29, 2021, 03:17:07 pm »
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.
« Last Edit: April 29, 2021, 03:21:02 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 
The following users thanked this post: jpanhalt

Offline Lindley

  • Regular Contributor
  • *
  • Posts: 200
  • Country: gb
Re: PIC16F ASM Bank Selection
« Reply #9 on: April 29, 2021, 03:48:14 pm »
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.



 

Online jpanhalt

  • Super Contributor
  • ***
  • Posts: 3744
  • Country: us
Re: PIC16F ASM Bank Selection
« Reply #10 on: April 29, 2021, 04:05:42 pm »
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.

I 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.

« Last Edit: April 29, 2021, 04:07:51 pm by jpanhalt »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf