Before getting into the minute details of PIC programming, it would be first important to learn a few good programming methods.
To begin with suppose you type a ;(semicolon) at any point of the program, all’s that come after this semicolon would get ignored by the compiler, until of course the carriage get’s back into the position.
The above feature allows us to add comments or remarks such that they don’t become the part of the program yet facilitates us to identify the program with the help of the comments beside it. Putting comments is a recommended practice while programming any IC.
Next important thing in the course is to assign names to the various constants (you would learn them later elaborately). This aso makes it simpler to understand what’s being written to, or regarding the involved values, instead of getting confused with the included numbers.
The above must be done in the form of actual names for instant recognition, for example COUNT, it would be important to note that here all capital letters are employed to make it distinct and also indicate that it’s a constant value.
As we can see, the above is done in the form of a box made of semicolons; this just makes it look cleaner. Additionally try documenting the program on paper as well, this practice will help to understand things in a step wise manner.
2. The Registers.
The register within a PIC is an area which accepts written details as well allows reading from it. You may compare it to a sheet of paper where you can visualize contents and aso add by writing over it.
The figure below depicts a typical register file map embedded within a PIC16F84. The format is not something which is actually set inside the PIC, it’s simply to indicate how the bits may be arranged inside the chip and to understand a few of the involved commands.
You can see that it’s basically divided into Bank 0 and Bank 1. Bank 1 is responsible for controlling the actual working of the PIC, for instance it tel the PIC which bits at Port A are assigned as inputs and which are as outputs.
Bank 2 is just for manipulating the information.
Let’s understand this through the following example:
Suppose we wish to assign one bit at PortA high. For this we would first need to go to Bank 1 for setting the specified bit or pin at Port A in the form of an output. After this we return to Bank 0 and deliver a logic 1 (bit 1) to that specific pin.
The most common registers which we woud like to use in Bank 1 are STATUS, TRISA and TRISB.
STATUS helps us to return to Bank 0, TRISA permits us to choose which pins at Port A are outputs and which may be inputs, while TRISB facilitates to select between output and input pin at Port B.The SELECT register in BANK 0 permits the user to flip to Bank 1.
Let’s summarize the whole concept with the following description:
In order to switch from Bank 0 to Bank 1 we command the STATUS register. This is implemented by setting bit#5 of the STATUS register to 1. In order to return back to Bank 0, we assign bit 5 of the STATUS register to 0. The STATUS register is positioned at address 03h, here h signifies tat the number may be in Hexadecimal.
TRISA and TRISB:
These are situated at address 85h and 86h correspondingly. For programming a pin as an output or an input, we just deliver a zero or a one to the particular bit in the register. Now this may be done in two ways, via binary, or Hex. In case one is unable to convert the parameter he or she may go for a scientific calculator for implementing the values.
Now we have5 pins at Port A, which corresponds to 5 pins. If we intend to fix one of the pins as inputs, we deliver a “1” to the particular bit.
In case we wanted to assign one of the pins as outputs, we would set the specific pin to “0”. The bits are aid down exacty corresponding to the bits, or more precisey bit 0 is RA0, bit 1 would be RA1, bit 2 = RA2 and so forth. Let’s understand it in this way:
Suppose you wish to fix RA0, RA3 and RA4 as outputs, while RA1/RA2 as i/ps, you would do this by sending 00110 (06h). Check out that bit 0 is toward the right as indicated here:
Port A Pin RA4 RA3 RA2 RA1 RA0
Bit Number 4 3 2 1 0
Binary 0 0 1 1 0
The same goes for TRISB.
PORTA and PORTB
In order to deiver one of the output pins high, we just offer a “1” to thr respective bit in our PORTA or PORTB register. An identical procedure may be followed for TRISA and TRISB registers also.Before we speed into our first example coding, let’s just understand a coupe of more registers, viz: w and f.
W and F
The W register is an ordinary register which enables you to assign any value of your choice. As soon as you assign a magnitude to W, you may proceed by adding this to another value or simply move it. With another value assigned, the details simply get overwritten on W.
The F register forwards its written matter over to a register. We would require this F register to assign a value over a register, may be over the STATUS or the TRISA registers, as these won’t allow us to put the values directly over them.An Example Program
Let’s examine the following example code which will show us how the above instruction is implemented and would also witness a few of the instructions in the course.
Let’s begin by fixing Port A as discussed above.
For this we need to shift from Bank 0 to Bank1, this is done by setting up the STATUS register situated at address 03h, bit 5 to 1.
The BSF Means Bit Set F. We are using two numbers after this instruction – 03h, which
is the STATUS register address, and the number 5 which corresponds to the bit number.
So, what we are saying is “Set bit 5 in address 03h to 1”.
We are now in Bank 1.
We are putting the binary value 00110 (the letter b means the number is in binary) into
our general purpose register W. I could of course have done this in hex, in which case
our instruction would be:
Either works. The MOVLW means ‘Move Literal Value Into W’, which in English
means put the value that follows directly into the W register.
Now we need to put this value onto our TRISA register to set up the port:
This instruction indicates “Move The Contents Of W Into The Register Address That
Follows”, in this instance the address refers to TRISA.
Our TRISA register at this point bears the figure 00110, or presented graphically:
Port A Pin RA4 RA3 RA2 RA1 RA0
Binary 0 0 1 1 0
Input/Output O O I I O
So now we possess our Port A pins, we must return to Bank 0 to adjust one of the
This instruction accomplishes the reverse of BSF. It implies “Bit Clear F”. The a pair of numbers that
correspond are the address of the register, here the STATUS register, as well as the bit
figure, in this instance bit five. What exactly we have completed at present is, defined bit five on our
STATUS register to 0
We have at this point returned in Bank 0.
The following is the code all in one block:
BSF 03h,5 ;Go to Bank 1
MOVLW 06h ;Put 00110 into W
MOVWF 85h ;Move 00110 onto TRISA
BCF 03h,5 ;Come back to Bank 0