This article will help you to develop the knowledge of your Assembly language, which is used to program a pic. Example programs, projects would be covered in the next articles.
PIC (16f) has only 35 instructions.35 means not that much. Every specific instruction would be done a specific task. The whole article is reserved to understand the tasks of the each and every instruction.
Some PICs have slightly different instruction sets, but if you know the basics, you'll be able to handle the differences with just a little study.
Here there are some of the instruction were discripped. Take a look at them. We will use these instructions little later when we get in to, tutorials of Assembly language.
Example: MOVLW 07
Side effects: none
The W register is central to most programs. This instruction moves a constant into the W register.
MOVF temp, W
MOVF temp, F
Side effects: Sets or clears Z flag (MOVF); none (MOVWF)
These instructions allow you to move data between registers and the W register. The first example moves the value in register temp into W. The third example moves W into temp. The second example moves the value of temp into temp. At first, that seems pointless, but it sets the Z condition flags without disturbing anything else, so it is useful for conditional jumps. If the number moved is 0, the Z flag is set. Otherwise it is cleared.
While you can use a number for a register is more common & easy to name them using EQU.
temp equ 0 × 70
movlw 0 × 10
ADDWF, ANDWF, IORWF, SUBWF, XORWF
ADDWF temp, W
XORWF x, F
These instructions do the specified operation on the W register and another register of your choice. Add and subtract work as you'd expect. The logical AND, IOR (inclusive OR) and XOR functions are the classic boolean logic primitives.
The result can be placed in the W register or back in the original register as you wish. For example:
MOVLW 1; put 1 in W
ADDWF temp, F; add 1 to temp, store in temp
; w still equals 1 here
ADDWF x, W; W = x + W, x is unchanged
The subtraction operation always computes FW, so be careful that you do not mean WF. So if W contains 3 and x contains 10, performing a subtraction will result in 7.
To compute the two's compliment of a negative number, write the magnitude as a binary number. So for 7, we have 00000111. Then invert all the bits (11111000) and add 1 (11111001). You can easily reverse the process, so if I tell you the subtraction yields 0xF9, you can write it as binary (11111001), subtract 1 (11111000) and then invert the bits (00000111) to know that it means -7.
All the instructions in this section set the Z flag if their result is zero. Otherwise, the Z flag is clear.
ADDLW, ANDLW, IORLW, SUBLW, XORLW
These instruction act just like the normal add, subtract, and, inclusive or, exclusive or instructions except they operate on a literal.
adds 2 to W (and, of course, leaves the result in W). All the flags are modified just as they are in the register-based versions of these instructions (see above). The subtract instruction computates literal-W which is often confusing. So instead of writing:
SUBLW 2; computes 2-W
You probably mean to write:
ADDLW-2; This is probably what you really want
Side effect: Sets the Z flag to 1
It is a very common operation to load a zero into a register and this instruction can do that in one step. The CLRW instruction clears W, which is odd since MOVLW 0 will do this also
INCF temp, W
DECF x, F
Another common operation is to add or subtract 1 from a register. These instructions do that (INCF is +1; DECF is -1).
INCFSZ temp, W
DECFSZ x, F
These instructions are similar to INCF and DECF. They add or subtract one of the listed register and store the result as directed. However, these instructions do not set the Z flag. Instead, if the result is zero, they skip the next instruction. This is useful, of course, for loops. Here's an example:
MOVWF i; i = 10
MOVF x, W; W = X
LOOP: ADDWF y, F; Y = Y + X
DECFSZ i, F
; Leave here with the answer in y
RLF temp, F
These instructions move the bits in the register left by one bit (RLF) or right (RRF).
This is often useful to manipulate bits, but there is another interesting property: shifting left multiplies a number by 2 and shifting right divides by 2! You can often combine this with adding to get easy multiplications. For example, suppose you want to compute y = 10 * x. Well, that is the same as y = 8 * x + 2 * x, right? So try this:
BCF status, C; C = 0
RLF x, F; X = 2 * X
BCF status, C
RLF x, W; W = 2 * X (so 4 * X)
BCF status, C
RLF temp, W; W = 2 * X (so 8 * X)
ADDWF x, W; W = 8X + 2X = 10X
BCF status, Z
These instructions clear (BCF) or set (BSF) the indicated bit in a register. The bit may have a name (like Z) or you can use a number from 0 to 7 (0 is the least significant bit and 7 is the most significant). Sometimes you can use these to save a few instructions. For example, suppose you had written:
ANDWF temp, F
You could replace this with a single BCF temp, 7 instruction. Not only is this faster and takes less space, but it does not destroy the W register either!
As you'd expect, the GOTO instruction forces your program to resume execution at the label you indicate. The instruction only holds an 11 bit address, so the top bits of the new program counter come from the PCLATH register.
RETLW is exactly like RETURN except that it loads a literal value into the W register before it returns.
This will Sets GIE
RETFIE is exactly like a return, but it also sets the global interrupt enable (GIE). When a hardware interrupt occurs, it clears GIE and executes what amounts to a CALL instruction. Using RETFIE allows you to enable interrupts and return to the main program all in one step. If you do not want interrupts enabled again, just execute a RETURN instead.
BTFSS temp, 7
When you want to perform a conditional jump, you need one of these instructions. They test a bit and skip the next instruction if the bit is set (BTFSS) or clear (BTFSC). Most of the next instruction is a GOTO or a CALL, but it could be any single instruction. For example, this code tests temp to see if it is zero. If it is, it loads temp with 0 × 80. Otherwise, temp is unchanged:
MOVF temp, F; set Z flag, no data really moved
BTFSC status, Z; skip if not zero
BSF temp, 7; since temp was 0, now it is 0x80!
This instruction informs the watchdog timer that your program is still executing.
I know that the above description will not be helpful to you to carry on. We have to have the practical experience. So we will do some tutorials, based on Assembly language in future. Stay with us.
If there are any questions, feel free to ask.