HomeArticlesProjectsBlogContact
Articles
PIC LAB 1 Extra Page 13
Colin Mitchell
Colin Mitchell
Make sure to subscribe to our newsletter and be the first to know the news.

Table Of Contents

01
Advanced Programming and Clever Commands
02
Indirect Addressing

Advanced Programming and Clever Commands


Everyone wants to advance to the “Nth degree” and rise to the top of his chosen field.
In programming, it’s producing a program or routine using the least number of instructions or applying the most-complex reasoning to a sequence.
This has some advantages and drawbacks.
The advantage is the pride you get out of creating a feature that requires thought and interpretation. The down-side is trying to work out what the instructions are achieving, when you have to re-work the program at a later date.
Advanced Programming quite often uses uses instructions called LOGIC COMMANDS to produce powerful outcomes.

To show the power of LOGIC COMMANDS we will SWAP THE CONTENTS OF W with a FILE, without Logic Commands:
Example: File 0C = A5h
File W = C3h
File 0D = temp storage
File 0E = temp storage

MOVWF 0D    ;Move W to 0D
MOVF 0C,0   ;Move 0C to W
MOVWF 0E    ;Move W to 0E
MOVF 0D,0   ;Move 0D to W
MOVWF 0C    ;Move W to 0C
MOVF 0E     ;Move 0E to W

With LOGIC COMMANDS it takes 3 instructions to swap the contents of W with a file.

XORWF 0C,1  ;XOR W with 0C, result in file 0C. W not affected
XORWF 0C,0  ;XOR W with 0C, result in W. 0C not affected
XORWF 0C,1  ;XOR W with 0C, result in 0C. W not affected.

When using LOGIC COMMANDS you have to know the outcome of each instruction and the only way to see what has happened is to look at it “bit-by-bit.”
To understand LOGIC operations we need the TRUTH TABLE for 3 terms:

The AND Truth Table applies to ANDLW 00h, ANDWF 0C,0 ANDWF 0C,1 instructions.
The OR Truth Table applies to IORLW 00h, IORWF 0C,0 IORWF 0C,1 instructions
The XOR Truth Table applies to XORLW 00h, XORWF 0C,0 XORWF 0C,1 instructions

OR and IOR means Inclusive-OR. They are both the same. The “I” reinforces the fact that the outcome includes the debate when the two outputs are HIGH. To remove the “debate,” the XOR command is provided. It specifically states that only one input can be active for the result to be logic 1.
For the operation above, the bit-changes after each instruction are as follows:
Start values: W = 1010 0101 = A5h
0C = 1100 0011 = C3h
After 1st instruction: W = 1010 0101 = A5h
0C = 0110 0110 = 66h
After 2nd instruction: W = 1100 0011 = C3h
0C = 0110 0110 = 66h
After 3rd instruction: W = 1100 0011 = C3h
0C = 1010 0101 = A5h

You can see an enormous understanding of the outcome of an operation is needed, to predict the result of two or three consecutive logic operations. That’s what we mean by Advanced Programming.

Another clever logic instruction tests two bits to see if they are both HIGH. This is the same as COMPARE.
For instance, you may have two inputs and need to know when both are HIGH.
The input lines are RA0 and RA2.

MOVLW 05      ;0000 0101
XORWF 05,0
BTFSS 03,2    ;Test Z flag
GOTO 1        ;both inputs are NOT HIGH
GOTO 2        ;both inputs are HIGH

Values:
W = 0000 0101 = 05h
Input Port 05 = 0000 0101 = 05h

W is loaded with a value equal to the bits being tested. The XOR function compares the same bit in each file and if they match, the answer is 0. If the final of all 8 matches is zero, the zero flag is “raised” (SET). Look at the zero flag in the STATUS register (file 03, bit 2) and if it is SET, the micro advances to GOTO 2 in the program.

Sometimes you may need to introduce instructions to make the understanding of a program enormously difficult - to foil “pirates.”
Here is a clever item that takes considerable understanding to see what it does.
To clear a file:

              ;File 0C = 0101 1100 or any value
MOVLW 00      ;W = 0000 0000
ANDWF 0C,1    ;AND W with 0C.  File 0C will have 000 0000

Indirect Addressing

One of the powerful features of the PIC chip is to be able to address a number of files in a block.
Data can be written into or read from the files via a loop program in which a file (called FSR) is incremented to read down the block (the read/write file is called INDF).
These two special files allow a sub-routine to be created to look at the block.
They are: INDF and FSR
INDF is a special file called INDirect File. It has the address 00.
FSR is a special file called File Select Register. It has the address 04. It is not like the other files in the micro but a POINTER FILE and is used in conjunction with INDF.
The INDF file is not a real file either. It is like a Robot Arm. It reaches down the list of files and picks up the contents or delivers the contents of a file to the program. The file it reaches is determined by the value in FSR.
FSR is loaded with the address of the file you wish to read/write.
By loading FSR with a value, you can reach a file and by incrementing FSR, you can reach the next file etc.
If you load a value into INDF, you will actually load the value into the file pointed to by FSR.
If you read INDF, you will actually read the contents of the file pointed to by FSR.
This is called INDIRECT ADDRESSING.
This arrangement has an advantage.
You can consecutively read 8, 10 or 20 files or clear 20 files or load into 20 or more files with a simple looping sub-routine. It’s a very powerful feature.
The following instructions show exactly how FSR and INDF work. A value of 8Fh is put into into file 21h. This does not show the power of the two instructions. It just shows how they work.

MOVLW 21h     ;Load W with the address of file 21h
MOVWF 04      ;Load 21h into FSR
MOVLW 8Fh     ;Put 8F into W
MOVWF 00      ;Put 8Fh into file 21h

The animation below shows how the information passes to the files:


Using INDF and FSR

The following instructions put a value of 8Fh into files 21h, 22h, 23h, 24h, 25h, 26h, 27h and 28h.

      MOVLW 08      ;8 loops of the program
      MOVWF 20h     ;File 20h is the decrementing file
      MOVLW 21h     ;Load W with start of 8 files
      MOVWF 04      ;Load 21h into FSR
      MOVLW 8Fh     ;Put 8F into W
Loop1 MOVWF 00      ;Put 8Fh into file 21h
      INCF 04       ;Increment FSR to make INDF go to next file
      DECFSZ 20h
      GOTO Loop1
      RETURN

The following instructions read files 21h, 22h, 23h, 24h, 25h, 26h, 27h and 28h and output to port B (file 06).

      MOVLW 08      ;8 loops of the program
      MOVWF 20h     ;File 20h is the decrementing file
      MOVLW 21h     ;Load W with start of 8 files
      MOVWF 04      ;Load 21h into FSR
Loop1 MOVF 00,0     ;Copy file 21h (or next file) into W
      MOVWF 06      ;Move W to output Port B
      CALL Delay    ;Show value on 7-segment display etc
      INCF 04       ;Increment FSR to make INDF go to next file
      DECFSZ 20h
      GOTO Loop1
      RETURN

Here is a very clever routine to change the value of a file without making the change obvious.
The secret lies in the fact that when a file is rotated, the end bit moves into the CARRY, and the bit in the carry enters the other end of the file. If you don’t want an unknown value from the carry to enter a file, the CARRY must be cleared before a RRF or RLF operation. (BCF 03,0)
When a file is rotated left (or right) and another file is then rotated left (or right) the end bit of the first file is transferred to the second file - via the CARRY!
With an 8-loop program, the two files will be swapped!

      MOVLW 08      ;8 loops of the program
      MOVWF 20h     ;File 20h is the decrementing file
      BCF 03,0      ;Clear the carry before starting
Loop1 RRF 1A,1      ;Rotate file 1A right
      RRF 1B,1      ;Rotate file 1B right
      DECFSZ 20h
      GOTO Loop1    ;Loop
      RRF 1A,1      ;Put the last bit from file 1B into 1A
      RETURN

Lots of other clever instructions and routines are too advanced for this course and fortunately, will not be needed. Routines for debouncing 8 switches with a matrix program, multiplying by 24 or finding the square root of a number, can be found on the PIC Webring. This is a very good ring for the PICmicro chip but as with everything on the web, you have to spend a lot of time searching through the enormous amount of material to find what you want.
Almost every requirement for a program can be performed with a number of very simple steps, involving instructions in the list: PIC16F84 Instructions.


Colin Mitchell

Colin Mitchell

Expertise

electronics
writing
PIC-Chips

Social Media

instagramtwitterwebsite

Related Posts

TODO
Transistor Test
© 2021, All Rights Reserved.

Quick Links

Advertise with usAbout UsContact Us

Social Media