Understanding Bit Operations: PIC Tutorial 8

Bit functions permit us to shape a single bit within a expression. They permit us to proceed,
set and get rid of single bits in registers or numbers that we stipulate.


PIC Tutorial - 7......................................PIC Tutorial - 9
At the conclusion of this course We will disclose a program designed to create a set of sequencing lights that proceed forward, then the reverse way. We observed this accomplished earlier when we examined the exclusive OR function, wherein we Exclusively ORed the ports with a expression. We have uptil now noticed a few bit functions when we establish the ports on the PIC, and

Let me reiterate their utilization here.
BCF

This instruction will wipe of a bit that we stipulate in a register that we designate. The syntax
is:
BCF      <register>,<bit>

We employed this earlier to alter from page 1 to page 0 by removing a bit in the STATUS register. We are able to likewise use it to fix a bit to 0 in any different register/location. For instance, in case we wished to set the 3rd bit in 11001101 saved in section 0C to 0, we might insert:

BCF 0C,03

BSF

This instruction would fix any bit we stipulate to 1 in any register that we indicate. We utilized this earlier to proceed from Page 0 to Page 1. The syntax is: BSF <register>,<bit>, and is utilized in precisely the same method as BCF above.

BTFSC

Up to now we could set or clear a bit in a register. However imagine if we need to basically check if a bit is a 1 or a 0 in a register? Surely, it is possible to use BTFSC. It states Bit Test Register F, and Skip If It Is Clear. This instruction is going to analyze the bit we designate in the register. In case the bit is a 0, the instruction would inform the PIC to by pass the subsequent instruction. We might utilize this instruction in case we wished to check a flag, for example the carry flag. This spares us needing to read the STATUS register and searching for the individual bits to learn which flags are fixed. 29 For instance, in case we wished to check if the Carry flag was set to 1 after we had added 2 figures, then we could type the following:

BTFSC           03h,0
carry on here if set to 1
or here if set to 0

In case the status of the bit is a 1, in that case the instruction subsequent to BTFSC would be completed. In case it is set to a 0, in that case the subsequent instruction is skipped. The following part of code exhibits in which it might be employed:
Loop    :
            :
            :
            BTFSC 03,0
            Goto Loop

In the above code, the PIC will simply get out of the loop in case bit 0 of the STATUS register (or the Carry flag) is defined to 0. Or else, the goto command would be conducted.

BTFSS

This instruction states Bit Test Register F, And Skip If Set. This can be comparable to the BTFSC instruction, apart from that the PIC would omit the subsequent instruction if the bit we have been evaluating is set to 1, instead of 0.

CLRF

This instruction would fix the whole details of a register to 0. The syntax is:

CLRF <register>
We employed this earlier to set the output of the Ports to 0, by applying CLRF 85h. We furthermore employed it to fix the Ports to include all pins to output by utilizing CLRF 05h.

CLRW

This could be resembling the CLRF instruction, except for clears the W register. The syntax is pretty simply:

CLRW

RLF And RRF

These directions would transport a bit in a register a single slot to the left (RLF) or the right (RRF) in a register. For instance, if we needed 00000001 and we employed RLF, in that case we might possess 00000010. At this point, what goes on in case there is 10000000 and applied the RLF instruction? Surely, the 1 would be positioned in the carry flag. In case we applied the RLF instruction once more, the 1 would reappear back at the start. The alike occurs, however in opposite, for the RRF instruction. The case in point below shows this for the RLF instruction, in which We have can see the 8 bits of a register, as well as the carry flag:
                            C 87654321
                            0 00000001
RLF                     0 00000010
RLF                     0 00000100
RLF                     0 00001000
RLF                     0 00010000
RLF                     0 00100000
RLF                     0 01000000
RLF                     0 10000000
RLF                     1 00000000
RLF                     0 00000001

Example Program

We are now gonna see an example code that one can compile and drive. It would generate a sequencing light beginning at PortA bit 0, going to PortB bit 8 and then returning.
Hook up LEDs to each one of the Port pins. We will have some of the bit procedures pointed out in this tutorial.

TIME EQU 9FH             ; Variable for the delay loop.
PORTB EQU 06H           ; Port B address.
TRISB EQU 86H             ; Port B Tristate address.
PORTA EQU 05H           ; Port A address.
TRISA EQU 85H             ; Port A Tristate address.
STATUS EQU 03H         ; Page select register.
COUNT1 EQU 0CH       ; Loop register.
COUNT2 EQU 0DH       ; Loop register.

BSF STATUS,5            ; Go to page 1
MOVLW 00H                ; and set up
MOVWF TRISB            ; both Ports A and B
MOVLW 00H                ; to output,
MOVWF TRISA            ; then return to
BCF STATUS,5             ; page 0.
MOVLW 00H                 ; Clear Port A.
MOVWF PORTA           ;

    ; Start of main program

RUN
MOVLW 01H              ; Set the first bit
MOVWF PORTB       ; on Port B.
CALL DELAY              ; Wait a while
CALL DELAY               ;

; Move the bit on Port B left, then pause.

RLF PORTB,1
CALL DELAY
CALL DELAY
RLF PORTB,1
CALL DELAY
CALL DELAY
RLF PORTB,1
CALL DELAY
CALL DELAY
RLF PORTB,1
CALL DELAY
CALL DELAY
RLF PORTB,1
CALL DELAY
CALL DELAY
RLF PORTB,1
CALL DELAY
CALL DELAY
RLF PORTB,1
CALL DELAY
CALL DELAY
RLF PORTB,1           ; This moves the bit into the carry flag

; Now move onto Port A, and move the bit left.

RLF PORTA,1            ; This moves the bit from the zero flag into PortA

CALL DELAY
CALL DELAY
RLF PORTA,1
CALL DELAY
CALL DELAY
RLF PORTA,1
CALL DELAY
CALL DELAY
RLF PORTA,1
CALL DELAY
CALL DELAY

; Move the bit back on Port A

RRF PORTA,1
CALL DELAY
CALL DELAY
RRF PORTA,1
CALL DELAY
CALL DELAY
RRF PORTA,1
CALL DELAY
CALL DELAY
RRF PORTA,1             ; This moves the bit into the zero flag

; Now move the bit back on Port B

RRF PORTB,1
CALL DELAY
CALL DELAY
RRF PORTB,1
CALL DELAY
CALL DELAY
RRF PORTB,1
CALL DELAY
CALL DELAY
RRF PORTB,1
CALL DELAY
CALL DELAY
RRF PORTB,1
CALL DELAY
CALL DELAY
RRF PORTB,1
CALL DELAY
CALL DELAY
RRF PORTB,1
CALL DELAY
CALL DELAY             ; Now we are back where we started,
                                     ;
GOTO RUN                 ; let's go again.


0 Response to "Understanding Bit Operations: PIC Tutorial 8"

Posting Komentar