LISTING 2 COMMANDS

We can now examine each command of Listing 2 in turn and understand its purpose.

PICmicros have been told in manufacture to adopt certain "default" conditions when first switched on or reset. One of these conditions is that Port A and Port B are configured (set) to act as inputs.

Another condition is that the STATUS file (accessible via address 3) is set to a default value with its bit 5 low. All file addresses are thus treated as being below H'80'. (A table of all default values is given in the Reference section.)

What is not configured at this time is the binary value that is available to be output via the port pins when they are first set as outputs from within the program.

At switch-on, any number could be set randomly within the PICmicro's memory, of between 0 and 255 (B'00000000' to B'11111111') for Port B, and 0 to 31 (B'00000000' to B'00011111') for Port A.

It is often preferable, therefore, to set port output values to a known number, usually zero, as part of the opening program statements (even though they are not actually being output at this immediate time).

We have established that address 6 is that which holds the data for Port B. The first command, CLRF 6, thus clears the data which is held in Port B as a value available to be output, i.e. Port B is instructed to hold a value of zero.

The purpose of the program in Listing 2 is to output data to the eight LEDs on Port B, and it has already been said that the default value of Port B's direction register (at H'86') is for all bits to be set for input (all bits are high - 11111111. Consequently we must now set them all as outputs, i.e. each to logic 0 - 00000000.

To do this, first the STATUS register at address 3 must have its bit 5 set high to point to addresses of H'80' and above; hence the command BSF 3,5.

Now we configure all of Port B for output mode with the command CLRF 6. Yes, it's the same command as cleared Port B's data, but because STATUS bit 5 is high, the value of 6 (H'06') has its MSB set high, so the address actually accessed is that at H'86'.

The commands which output data to the LEDs are all concerned with Port B's data file at "real" address 6, so the MSB of all other addresses must be zero. The next command, BCF 3,5, thus clears bit 5 of the STATUS register at address 3.

All remaining commands in Listing 2 can now, in turn, set high each data bit of Port B at address 6: BSF 6,0, BSF 6,1, etc., so turning on the LEDs in sequence from LB0 to LB7. Simple!

END OF TUTORIAL 2

Now click on the Exercise buttons and then, when you have finished with the exercises, move on to Tutorial 3. Alternatively, use the contents tree to navigate to Tutorial 3 straight away.