Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
A Caution for PIC Assembly Programmers
There is a subtle point that you should keep in mind when writing
your assembly program for the PIC chip. The program counter is
a 13-bit unsigned integer, but most of the commands that change
the value of the program counter provide fewer than 13 bits.
The commands CALL and GOTO provide only 11 bits, and any operation
such as ADDWF applied to PCL provides only 8 bits. The remaining
of the 13 bits are obtained from the register PCLATH, which is
initially 0. Hence, CALL and GOTO will operate only within
the first 2K of memory and the ADDWF PCL,f line that we use
to implement lookup tables works only in the first 256 words of
memory unless we explicitly set PCLATH to the proper value.
Since your program is not likely to exceed the 2K word limit,
we only need to worry about the situation where the ADDWF PCL,f line
of a lookup table gets pushed beyond the address 100h. If you
add code to proto.asm and suddenly find that the program stops
working completely, it may be because you have pushed the ADDWF PCL,f
line used in the motor routine to a location beyond the 255 byte
limit.
To check for the problem, look at the .lst file that the assembler
produces and check every line of the form ADDWF PCL or ADDWF PCL,f
and check its address (the first number on the line; it is in
hexadecimal). If that number is larger than 0FFh, the program
counter will be set to some address at the beginning of the
program instead of a few words further in the code where you want
it to go. To fix the problem, insert the lines
MOVLW 1
MOVWF PCLATH
somewhere just before you get ready to execute ADDWF PCL. You
will also want to write the lines
MOVLW 0
MOVWF PCLATH
at the beginning of the main loop so that ADDWF PCL instructions
in the first 255 words of memory will work properly.
If your code gets so big that you find an ADDWF PCL instruction
at an address between 200h and 2FFh, you would use MOVLW 2 instead
of MOVLW 1, and so on. Similarly, if your program should get so
big that a CALL or GOTO instruction either resides at a location
with an address beyond 7FFh or jumps to a location beyond 7FFh,
you will have to insert
MOVLW n
MOVWF PCLATH
where n is the result of integer division of the target address
by 256. (Drop the last 2 hexadecimal digits of the address being
jumped to or called to get n.) Again, make sure that PCLATH is
properly set to the right value before every CALL and GOTO and
ADDWF PCL. (You don't have to worry about CALL and GOTO instructions
even though PCLATH is being set to handle ADDWF PCL instructions if
the your program is less than 2048 instructions long.)
Note also that when ADDWF PCL executes, if the number in W and
PCL add to a number that is larger than 255, the carry bit gets
thrown away and the program counter will jump backwards instead
of forwards. This is why the entire lookup table must not
straddle an address that is divisible by 256.