| PIC PRESS HELP | Program Counter |
The Program Counter stores the address (in the program memory) of the next intrustion to be executed. Normally the program counter increases by one each time an instruction is executed, but when it comes to a jump in the program such as a goto, the number in the program counter changes more significantly. The table below demonstrates how the program counter works:
| address in program memory | label | instruction | operand | number in program counter |
| 0045 | movlw | b'00111111' | 0046 | |
| 0046 | main | btfss | PortA, 3 | 0047 or 0048 |
| 0047 | goto | main | 0046 | |
| 0048 | subwf | Store1 | 0049 | |
| 0049 | etc. |
When the bit is tested using the btfss instruction, if it is clear 0047 is moved into the program counter (i.e. it doesn't skip), but if the bit is set, 0048 is moved into the program counter (i.e. it skips the next line).
PCL is a Special Function File Register that holds the lowest 8 bits of the program counter. If the PIC you are using can store 512 instructions in its program memory, we can deduce that the program counter is 9 bits long. As the PIC's file registers can only hold up to 8 bits, we see only the 8 least significant bits of the program counter in the PCL, but this is enough for it to be useful in applications such as look-up tables.
The essence of a look-up table involves you putting a certain number in, and getting another number out, which depends on what you put in. For example a look-up table that performs a x3 multiplication would produce the number 15, when you input a 5. This look-up table is shown below:
| Multiply3 | addwf | PCL | ; skips this many instructions |
| retlw | 0 | ; 0 x 3 = 0 | |
| retlw | d'3' | ; 1 x 3 = 3 | |
| retlw | d'6' | ; 2 x 3 = 6 | |
| retlw | d'9' | ; 3 x 3 = 9 | |
| retlw | d'12' | ; 4 x 3 = 12 | |
| Etc. |
As you may have guessed, a look-up table is a subroutine, this particular one is called with the number which you wish to multiply by 3 in the working register. This number is then added to the program counter. Adding a number to the program counter effectively makes the program skip that many instructions. Hence adding the number 2 to the program counter makes it skip 2 instructions, which in this case makes it perform the retlw d'6' instruction. This returns from the subroutine with the number 6 in the working register. In this way we have a subroutine which when we input a 2, we get back a 6 (which we call a look-up table). A good example of a look-up table can be found here. PIC PRESS has a look-up table macro.
Important note: Do not have look-up tables which span the parts of the program memory where 9th or higher bit of the program memory changes, for example when the program memory goes from 0FF to 100. This is because the PCL only houses the lowest 8 bits (as you already know), and so adding the number 5 to PCL when it has the number 0FD, results in 002 and NOT 102!!
Related topics
Seven Segment Displays
Subroutines