PORT FILE NUMBERS

In the example of Listing 2, the files whose bits have been set or cleared are those which control Port B.

In terms of the basic commands used for writing to or reading from Port A and Port B, the two ports are treated as any other file source or destination. There is a significant difference, though, in the way in which these registers respond compared to "normal" file registers.

Port A and Port B are different to other files in that they are for communication with the outside world. There are, in fact, two file registers associated with each port that are accessible to the programmer. One is used to set the direction in which the bits are to act, i.e. as inputs or outputs. The other deals with the data written to (for output to the world) or read from (input from the world).

Each bit of the port direction registers can be individually set or cleared so that the same port may be used simultaneously for data input and output via different bits.

The file that controls the direction in which Port B pins respond is named in the PICmicro data sheet as TRISB. It is one of the "special function files" and is contained in memory byte address H'86' (we won't translate hexadecimal numbers into decimal since the latter are irrelevant in the context of file addresses).

The file that holds Port B's data, whether as input or output, is at memory address H'06' and, helpfully, is known in the PICmicro data sheet as PORTB. A minor inconvenience exists in the PICmicro in that addresses H'80' to H'8B' can only be accessed by changing the value in another file, the STATUS file.

A single bit within STATUS is used to direct the program to numbers either below H'80' (known as Page 0 or Bank 0 addresses) or above H'7F' (known as Page 1 or Bank 1 addresses); this bit is number 5.

This is because when the assembly program is translated to machine code, the file address in each instruction is actually represented by 7 bits (i.e. from 0 to H'7F'). When the PICmicro executes an instruction the 8th bit (the MSB) is retrieved from bit 5 of the STATUS file.

When STATUS bit 5 is set (logic 1), it effectively adds H'80' to the memory byte address being accessed. When the bit is clear (logic 0), addresses below H'80' are accessed.

Thus, if you instruct that file 6 is to be accessed when STATUS bit 5 is low, the file actually at address 6 will be accessed. Conversely, if you instruct that file 6 is the subject when STATUS bit 5 is high, the file at address H'86' will be accessed.

This is the reason why we are using the statement "CLRF 6" to clear TRISB even though TRISB is located at address H'86', and not at H'06' - setting STATUS bit 5 will mean that "CRLF 6" actually clears address H'86'.

A better way of writing this statement would be: CLRF H'86', because this tells us that the register is in Page 1. This is for our benefit only - MPASM will change the address to H'06' when it assembles the instruction because it knows the MSB will be retrieved from bit 5 of the STATUS register when the program is run. Note that when you use file addresses greater than H'7F', MPASM will add a message in the .LST and .ERR files reminding you that these must be accessed within Page 1.

With the PICmicro we are using, only files numbered H'00' to H'4F' and H'80' to H'8C' are available for use by the programmer. Note that files H'07' and H'87' have no function (refer to the Data section for Special Function Register file details).

Now use the contents tree to navigate to Tutorial 2.8.