Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Clone this wiki locally
First thing you will notice is that it is split into two - Bank 0 and Bank 1. Bank 1 is used to control the actual operation of the PIC, for example to tell the PIC which bits of Port A are input and which are output. Bank 0 is used to manipulate the data. An example is as follows: Let us say we want to make one bit on Port A high. First we need to go to Bank 1 to set the particular bit, or pin, on Port A as an output. We then come back to Bank 0 and send a logic 1 (bit 1) to that pin. The most common registers in Bank 1 we are going to use are STATUS, TRISA and TRISB. The first allows us to come back to Bank 0, TRISA allows us to select which pins on Port A are output and which are input, TRISB allows us to select which pins on Port B are output and which are input. The SELECT register in Bank 0 allows us to switch to Bank 1. Let us take a closer look at these three registers,
Table of Contents
To change from Bank 0 to Bank 1 we tell the STAUS register. We do this by setting bit 5 of the STATUS register to 1. To switch back to Bank 0, we set bit 5 of the STATUS register to 0.
These are located at addresses 85h and 86h respectively. To program a pin to be an output or an input, we simply send a 0 or a 1 to the relevant bit in the register. Now, this can either be done in binary, or hex. I personally use both, as the binary does help visualize the port. If you are not conversant with converting from binary to hex and vice versa, then use a scientific calculator. So, on Port A we have 5 pins, and hence 5 bits. If I wanted to set one of the pins to input, I send a 1 to the relevant bit If I wanted to set one of the pins to an output, I set the relevant bit to 0. The bits are arranges in exactly the same way as the pins, in other words bit 0 is RA0, bit 1 is RA1, bit 2 is RA2 and so on. Lets take an example. If I wanted to set RA0, RA3 and RA4 as outputs, and RA1 and RA2 as inputs, I send this: 00110 (06h). Note that bit zero is on the right, as shown:
The same goes for TRISB
To send one of our output pins high, we simply send a 1 to the corresponding bit in our PORTA or PORTB register. The same format follows as for the TRISA and TRISB registers. To read if a pin is high or low on our port pins, we can perform a check to see if the particular corresponding bit is set to high (1) or set to low (0). Before I give an example code, I need to explain just two more register w and f.
The W register is a general register in which you can put any value that you wish. Once you have assigned a value to W, you can add it to another value, or move it. If you assign another value to W, its contents are overwritten.