Wednesday, 24 October 2012

Bank Select

Moving on now to the second line in our world's smallest PIC program. Here's the program again:

bsf 7,0
bsf 3,5
clrf 87

The second instruction is also a 'bit set file' instruction, but this time it's bit 5 of register 3 that's being set. The data sheet tells us that register 3 is known as the STATUS register.

Now we have to consult a different part of the 16F690 data sheet. Go to section where the Status register is described. You can see that bit 5 is one of two bits (6 and 5) called RP1 and RP0, which are the register bank select bits.

Selecting register banks is an area of PIC programming that is often overlooked and can lead to very elusive program errors. The midrange PIC microcontrollers have a reduced instruction set which is only 14 bits wide. Of those 14 bits, just seven are used to define the file (or register) number that the instruction is to operate on. 7 bits gives us just 128 registers that are directly addressable by the instruction. But the 16F690 has 512 bytes of memory where the special purpose and general purpose registers are located.

So prior to using any of these registers in an instruction, the bank select bits must be properly set up to select the correct bank.

So, for example, PORTC is register 7 which is in the first of the four register banks. Be careful here, because the first bank is also called bank zero. This is a classic example of knowing whether to start counting from 0 or from 1.

We didn't need to worry about setting the bank select bits prior to our first instruction because they default to bank zero after boot up and PORTC is in bank zero. But TRISC (register 87) is in bank 1 and that's the register we need to write to in the 3rd line of the program.

No comments:

Post a Comment