PIC users group?

Electronics Computer Programming Q&A
User avatar
MrAl
Posts: 3862
Joined: Fri Jan 11, 2002 1:01 am
Location: NewJersey
Contact:

Post by MrAl »

Hello again,

Yes, did that.
LEDs vs Bulbs, LEDs are winning.
artc
Posts: 3
Joined: Wed Dec 21, 2005 1:01 am
Location: NYS
Contact:

Post by artc »

Thanks guys for all the very, very helpful
info you have posted here. I have learned
so much. I have taken six pages of notes
and have a lot of research to do. Using
Bytecraft and CCS "C" compilers I managed
to build some PIC based test fixtures for
my former company.

Right now, I'm trying to teach myself
Microchip PIC assembly language. I'm
finding banking issues and indirection
kind of hard to understand.

Thanks for all the help......Art
Art
User avatar
MrAl
Posts: 3862
Joined: Fri Jan 11, 2002 1:01 am
Location: NewJersey
Contact:

Post by MrAl »

Hi Art,

Im not sure if this helps or not but...


Banking first came into being because you could use one address
pin of a microprocessor to make the decision to turn either one
RAM chip (or ROM chip) on or another RAM chip.
In other words, lets say you had two RAM chips each storing
1024 bytes of data. Together they can store 2048 bytes, but
because they are different chip packages (two packages rather
than one because bigger ones were not available yet) you have
to enable one chip and disable the other, then when the higher
address line switches state you have to enable the second one
and disable the first one. This way you can use the whole 2048
bytes for storing data.

In the above scenario each chips 10 address lines (for the
1024 bytes that they each can store) come from the same place:
from the first 10 address lines of the microprocessor.
They dont have more address lines than that, but they do have
enable inputs that usually enable with a logic "LOW" (or zero).
This means we can connect the first chips enable input to address
line 11 of the CPU and since it will be initially low when the CPU
is reset (or powered on) the first chip will be enabled first.
We drive the second chips enable pin with the same CPU pin
(line 11) but first invert it with an inverter chip. This means the
second chips enable will be HIGH when the CPU chip is reset, keeping
it turned off. Later, when the program runs and the address goes
above 1024, address line 11 becomes high, which turns off chip 1
and turns on chip 2, which allows us to access the next 1024 bytes
of data (in chip 2).
This is what "bank switching" is really all about.

In the PIC it's a little different though. You have to drive that 'enable'
yourself with an instruction like "BANK 1" or whatever. Note that
if you didnt do this (as well as with the two chips mentioned above)
the address lines would restart at address zero which would start
your program all over again, instead of continuing on to the next
address.

In the case of storing data, some of the stuff you want to store
has to go into the higher address range (chip 2 above) so you
have to specify that it is in the second bank by executing and instruction
to tell the system what bank it is in. Then, when you are done, you
have to switch back to the original bank if you want to continue to
use that bank.

Each PIC data sheet shows where each item is located so you
can always find out what bank it is in.
What i like to do is load one bank with data, then the next bank
if that is possible with the chip im using. This means i only have
to switch banks once to store into one bank, again to store
into the higher bank, then one more time to get back to the first
bank. It saves program steps to do it this way, rather than
switch banks for every single item you want to store.
LEDs vs Bulbs, LEDs are winning.
artc
Posts: 3
Joined: Wed Dec 21, 2005 1:01 am
Location: NYS
Contact:

Post by artc »

Hi Al,
Thanks for all the good info.
I really appreciate it.

The light is slowly but surely beginning
to come on.

Lots of good info about this ( banking ) on the piclist and in Microchip forum.

Heres a question for you:
Suppose I'm using a PIC micro with 4K usser
sram(Microchip file registers). Lets say that
each bank is 1024 bytes of storage, for a
total of 4 banks of storage. Suppose I have
a variable,stored in a register in bank 4.
Now, in my program, I need to load the W reg
with the variable stored in bank 4.

My Question: Don't I have to select bank 4 first
(using proper banksel RP0--RP1 statements) in order
to load W with the byte that I have stored
in bank 4?? I THINK I HAVE TO.....Right??

I havent even began to get around "Lookup Tables"
yet. According to the piclist, these require
special consideration, even if your program is
less that 2K in size.

Your explanation of banking brings back old memories.
I worked on boards using the TI 4004 microprocessor.
I believe we had a total of 2048 bytes of ram which
consisted of 16 chips arranged in 2 banks.
Each bank was 8 chips...each chip held 1 bit at
1024 different addresses. What FUN those boards were
to troubleshoot....hehehehe

Take Care....Thanks again.....Art
PS...hope my question makes sense.
Art
User avatar
MrAl
Posts: 3862
Joined: Fri Jan 11, 2002 1:01 am
Location: NewJersey
Contact:

Post by MrAl »

Hi again Art,

Yes that is correct. In order to load the W register with a value
from a variable that is located (according to appropriate data sheet)
in bank 4 you would have to first switch to that bank.

In pseudo statements:

Load W,MyValue

would not work if MyValue was located in bank 4, unless of course
you already switched to bank 4. You would have to do this:

BANK04 (note: might actually be BANK03)
Load W,MyValue

Then, if you have to do something else back in bank 00 then
you of course have to switch BACK to bank 00.

Note however if you are loading two values in the same bank, you
only have to switch one time, until you are done:

BANK03
Load W,0x01
Load MyValue1,W
Load W,0x02
Load MyValue2,W

Notice we only had to switch once, except of course at the end
if we want to get back to bank 00 in which case we have to add
something like
BANK00
after the last statement above.

I think you got it already though :smile: just want to be clear.

I did this many times to load the default values for various
built in registers by first switching to bank 1 and then loading
a whole bunch of values, then switching to bank 0 and loading
the rest. Since my actual program expected the 'bank select' to
be in bank 0 to start with i didnt have to switch any more unless
i needed something else in another bank.

So far i've only worked with devices that have two banks; i think
they were bank 0 and bank 1.

I find these are very versatile chips, even with the bank switching
and what not.

Oh yeah, just in case you feel like using the interrupt routine
(if i rem right it's at hex 0x004 or something like that) you may
want to remember that if your interrupt routine alters the value
of the W register *or* the STATUS register, once it returns to the
main program there might be an error because the main program
might have been just about to either test the W register (used so
much with these chips) or test the STATUS register (also used
so much). For example:

INTERRUPT ROUTINE:
load w, 0x01
call MyRoutine1
call Myroutine2
ret

p/o MAIN PROGRAM:
subwf MyVal,w
btfss STATUS,0
'continue on'

Note that if a hardware interrupt occurs just after the subwf statement,
the interrupt routine will alter the value of STATUS, so that when
it returns the btfss statement will actually be testing the state of
STATUS that the interrupt routine exited with, not the result of
the subwf statement like it is supposed to!

I thought i would mention this because it can easily be overlooked,
and there is a very neat solution around this problem. The solution
is while in the interrupt routine, save the contents of W and the
contents of STATUS upon entering, then just before returning
restore the contents of both these registers. Just in case you havent
heard of this before, it's usually called, "saving the context".
The context being the W and STATUS registers in this case.
Also, there is a trick to doing this because if you try to simply save
the two registers that in itself alters the STATUS register, so MicroChip
came up with a unique solution which is in one of their data sheets.
If you cant find it, i'll look it up if you need it. It involves storing
one of the values by using the SWAP instruction rather than a
MOVF or MOVWF instruction (i cant remember which one causes
a problem, but without using the SWAP instruction you would have
to eventually use both of those instructions which would be a problem).
Let me know if this is something you need and i'll look it up unless
you can find it on the Microchip site.
LEDs vs Bulbs, LEDs are winning.
artc
Posts: 3
Joined: Wed Dec 21, 2005 1:01 am
Location: NYS
Contact:

Post by artc »

Hi Al,
THANK YOU so much for all the good info.
I really, really appreciate it.
Especially the interrupt info. My pic coding
projects are getting larger and larger, so I
expect to have to use interrupts soon.
Right now I have a list of six seperate
pic assembly language coding routines that
I have to write for different projects that
I have going on.

I spend huge amounts of time on Microchip
forum trying to learn about banking,linking,
table lookup issues...etc....etc....

Two or three of my present projects, I hope
to get published in an electronics magazines.

In my SPARE time I want to start playing with
and coding pic 18F devices...I've never used
these before...I have been trying to find some
type of Microchip pic assembly language 18F
programming course.

Also in my spare time I want to teach myself
how to write relocatable pic assembly language
code using the linker....right now I have no
idea how to do this. All I have is a very old
Microchip MPASM and LINKER book.

My old company threw away an almost brand new
Microchip emulator that they used to develop
code for a 16C620 chip. I want to set this up,
try to figure out how to get it working with
MPLAB....and hopefully....hopefully..this
will save me a few cycles of crash and burn
programming.

Most of my code is now at the point that it runs
fine using MPLAB--SIM. It now has to be programmed
into an actual pic micro, put into the actual
operating circuit and tested and debugged
from that point....so its back to crash and burn
mode for me.....hehehehe

I guess I really should buy myself an ICD2.

Right now I hope to soon start some type
of internet based PIC coding,programming
and debugging business.

TAKE CARE...Thanks again for all the help.
Art
Art
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests