Memory Banks

Status
Not open for further replies.

lord loh.

Member
I am using the PIC 18F452 controller...

I tried experimenting with banks in the MP Lab simulator.

This is what I saw...

When I wrote to 0x0000 (movwf 0x0000)the byte was stored in 0x0000 (bank 0 selected) when I tried to write to (0x0100), again 0x0000 was written to.

Now I select bank 1 by banksel 0x0100 and execute (movwf 0x0103) is writen to 0x0103. When I give (movwf 0x0203) again 0x0103 was witten to.

Now I selected bank5 (banksel 0x0500) and execute (movwf 0x0503), my byte got written to 0x0503. And When I execute (movwf 0x0100), 0x0500 is written to...

So far so good....

Now When I try to address 0x0005, data is written to bank0 while I have selected bank5. So what is the point of banks.... I have to specify an absolute address to read or write from it.. I can't just address from 00 to FF in my specified bank....
 
I haven't checked on that particular datasheet, but on most PIC's the lower addresses are SPR's and NOT GPR's - so some of the SPR's repeat across all the banks (for obvious reasons, you don't want to have to switch banks just to write to PortA).

So try checking the datasheet about memory locations.
 
If I understand you correctly, your use of movwf is incorrect. The datasheet specifies an operand 0x00-0xFF. Also, without specifying BANKED as a final parameter, MPASM defaults to ACCESS.


Mike Willms
High School Drop-Out
 

The datasheet specifies that SFRs are in bank 15 and start at the top of the address and moves downwards... And in the register view of the MPLAB IDE, I do not see any recurrence of the registers...

And
The datasheet specifies an operand 0x00-0xFF
How is the assembler supposed to know if I am accessing the current bank or the Access RAM?
 
Okay, reading the datasheet again I get that the syntax of movwf is as....
Code:
[label] movwf f [,a]
Since a is optional, it is 0 by default... access the Access RAM

If the same synatx is written with a=1, the operation is performed on the GPR of the selected bank....

It looks like the compiler is interpreting the absolute address that I specified and used all 0x0000 to 0x00FF to address the access RAM and anything over 0X00FF is cycled (subtract n times 0x00FF) and the selected bank is accessed...

Strange behaviour....

I simulated the above and saw it to be true...
 
How is the assembler supposed to know if I am accessing the current bank or the Access RAM?

Many PIC18 (16-bit core) instructions which operate on a file include f,d,a or f,a operands/parameters (file, destination, and access)...

This gets a little complicated... For the most part, if the access parameter is omitted the assember assumes the target address is in the access bank when the file address is less than 0100h or in a BSR bank when the file address is equal to or greater than 0100h... Use the access bit parameter to overide those assumptions...

Code:
0002E8 0101           00241         BANKSEL h'0100'         ;
                      00242 ;
0002EA 5010           00243         movf    h'0010',W       ; 0010
0002EC 5110           00244         movf    h'0110',W       ; 0110
0002EE 5010           00245         movf    h'0110',W,A     ; 0010
0002F0 5110           00246         movf    h'0110',W,1     ; 0110

Good luck... Have fun... Regards, Mike
 
Last edited:
I thought I figured it.... Now again problems cropped up....
Code:
#include p18f452.inc
cblock 0x0300
var1
endc
org 0x0000
movlw 0xAA
banksel 0x0500
movwf var1
banksel var1
movwf var1
end

I define a variable at 0x0300...
select bank 5 and write... The data AA is written at 0x0500

Then I select bank where the var has been defined and the data is written at 0x0300....
So the assembler does know where the var is defined... Though the help topics says that the cblock is only to define offsets, it also seems to define banks...

I figured the movwf instruction with address but not with variables....

Please help...

PS: Please see this topic in Hybrid or Thread view to getthe thread correctly...
 
This is working as it should... The assembler is smart enough to realize the target address is not in bank zero and uses the BSR along with the lower eight bits of the target (variable) address... If you select bank 5 and write a value to 0100h, or 0200h, or 0300h, etc., that value will be written to 0500h...

If this is messing up your logic, you might consider using indirect addressing...

Code:
var1    equ     0300h
;
        lfsr    0,var1          ; setup indirect addr
        movlw   h'AA'           ;
        movwf   INDF0           ;
;
 
Last edited:
Code:
#include p18f452.inc

		udata
		vari res 1



Int0	CODE 0x0000
		goto START

		CODE
START	movlw 0xA4
		banksel 0x0200
		movwf vari,1
		END
The variable vari is written into some place (the linker is supposed to decide where isn't it?). But it is written to the location of the currently selected bank...

Is this normal?

Is there a way to just define the variables and forget it and code without bothering about banks? Or banks are inevitable?
 
According to the manual, banksel only works on predefined labels. Since you are specifying an address which is being interpreted as a non-existant label, I believe the assembler just ignores it and you stay in the bank you are currently in.

 
One thing that I am sure of is that the banks are being changed... My data is being written to the bank which I select. The banksel seems to be interpreted as the bank which contains the address 0x0300 (bank 3) and so on...
 
Okay,

I saw a lot of posts here about banking...
http://forum.microchip.com/tm.aspx?m=76071&mpage=1&key=access,ram&#76071

It looks like banking is a necessary evil. One can't address the entire memory map without changing banks. MPASM can do as much as differentiate between Access bank and the currently selected bank. if one gives the absolute address

Even if you give an absolute address 0x0090 and try to access it(absolute 0x0090) with bank 2 selected, it writes to 0x0290. MPASM knows that it is a bank on which the operation is to be performed and not access RAM. That is all the difference.

This persists even if you have used variables that are declared in one bank and you try to access it when some ther bank is selected. The variables shall be written to that offset but in some other bank. However, the assembler MPASM can know the bank where it is defined. So if you do a banksel variable_name before you write it, the appropriate bank is selected.
 
Looks like I did.... I am a new learner and have not reached there yet... So fat I have a\only been trying to fiddle with the movwf instruction...

Indirect addressing looks like doing a move operation by having the source and or destination in a pointer register and access.

So they do not need bank select?
But I guess the MPASM will not do this for us? We probably will have to load the pointer registers ourselves and start operating with these?
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…