KEMBAR78
Example 3 Explanation | PDF | Computer Programming | Computer Science
0% found this document useful (0 votes)
7 views4 pages

Example 3 Explanation

The program continuously shifts the contents of a shift register to portb, controlling 8 connected LEDs. It initializes portb as output, clears it, and loads the shift register with a specific value, displaying the output through LEDs after a delay. The process involves displaying the current shift register value, pausing for visibility, and then rotating the register left, repeating this cycle indefinitely.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Example 3 Explanation

The program continuously shifts the contents of a shift register to portb, controlling 8 connected LEDs. It initializes portb as output, clears it, and loads the shift register with a specific value, displaying the output through LEDs after a delay. The process involves displaying the current shift register value, pausing for visibility, and then rotating the register left, repeating this cycle indefinitely.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Example 3:

This program continuously shifts the contents of the shift register and display
to portb every after shift.
* 8 LEDs connected to portb.
* C on the leftmost refers to the carry flag
shift portb (output)
C Bit Bit Bit Bit Bit Bit Bit Bit
RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0
7 6 5 4 3 2 1 0
LED LED LED LED LED LED LED LED
0 0 1 0 0 1 0 0 1
7 6 5 4 3 2 1 0

status equ 0x03 ; assign status register to address 0x03


portb equ 0x06 ; assign portb to address 0x06
shift equ 0x0C ; assign shift register to adres oxoc
ncount equ 0x0D ; assign ncount register to address 0x0D
mcount equ 0x0E ; assign mcount register to address 0x0E
c equ 0 ; assign variable c as equal to 0
org 0x00 ; tells the assembler that next instruction should go at
address 0 in program memory
start movlw 0x00 ; teach portb
tris portb ; as output
clrf portb ; clear portb
bcf status, c ; clear bit o (carry flag) of status register (take note
that c=o)
movlw b’01001001’ ; move 01001001 or 0x49 to w register
movwf shift ; move w register to shift register
get_bit movf shift, w ; move contents of shift register to w register
movwf portb ; move contents of w register to portb (this is the
time that output is visible through the LEDs)
call pause ; call subroutine pause
rlf shift, f ; rotate shift register one bit to the left
goto get_bit ; go to subroutine get_bit
pause movlw 0xFF ; move 0xFF to w register
movwf mcount ; move contents of w register to register named
mcount
loadn movlw 0xFF ; move 0xFF to w register
movwf ncount ; move contents of w register to register named
ncount
decn decfsz ncount,f ; decrement ncount
goto decn ; if not zero, go to subroutine decn
decfsz mcount, f ; if zero, decrement mcount
goto loadn ; if not zero, go to subroutine loadn

EXAMPLE 3 | Writing the Source Code


return ; if zero, go back to the line next to the instruction
that calls the subroutine pause
end ; end of the program
============================================================
Summary of how the program code works:
1. teach portb as output
2. clear portb
3. clear bit 0 (carry flag) of status register
4. load shift register with 0x49 (b’01001001’)
5. display contents of shift register to portb
6. call delay – delay is used for us to view the output for some time
7. rotate shift register 1 bit to the left (through the carry flag)
8. go back to 5.

Additional explanations:

 Take note that everytime we use the rotate (rlf or rrf) operation in our
code, the carry flag is involved so it will be best if we will set or clear the
carry flag (C). In the program code above, carry flag was cleared (C=0).
 The subroutine pause is used for delay.

pause movlw 0xFF ; move 0xFF to w register


movwf mcount ; move contents of w register to register named
mcount
loadn movlw 0xFF ; move 0xFF to w register
movwf ncount ; move contents of w register to register named
ncount
decn decfsz ncount,f ; decrement ncount
goto decn ; if not zero, go to subroutine decn
decfsz mcount, f ; if zero, decrement mcount
goto loadn ; if not zero, go to subroutine loadn
return ; if zero, go back to the line next to the instruction that calls
the subroutine pause

 Here’s how it works as a delay:


 Steps:
1. Load mcount with 0xFF which is the highest possible value for 2-
digit hexadecimal
2. Load ncount with 0xFF
3. Decrement ncount until it reaches zero
4. Once ncount is zero, decrement mcount
5. Once mcount is decremented (-1), go back to step 2.

EXAMPLE 3 | Writing the Source Code


6. When mcount reaches zero, program execution returns to the
line next to the instruction that calls the subroutine pause
 Delay happens by just decrementing the 0xFF being loaded to mcount
and ncount. If you want to shorten the delay time, you may reduce the
hexadecimal value being loaded to mcount and ncount.

Sample output of the program code shown above:

* Notice the sequence/cycle: DISPLAY  DELAY  ROTATE

DISPLAY
* Right after the line
get_bit movf shift, w display contents of shift register to portb
movwf portb (shift register  w register  portb)
is executed:

shift register portb (output)


C Bit Bit Bit Bit Bit Bit Bit Bit
RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0
7 6 5 4 3 2 1 0
LED LED LED LED LED LED LED LED
7 6 5 4 3 2 1 0
0 0 1 0 0 1 0 0 1 OFF ON OFF OFF ON OFF OFF ON
* Note: the carry flag is not included in the display, only the contents of shift
register (carry flag is only involved in the rotation)

DELAY (call delay) – to give time for the programmer to view the output
through LEDs

ROTATE
* Right after the line
rlf shift, f rotate shift register 1 bit to the left through the
is executed: carry flag

shift register portb (output)


C Bit Bit Bit Bit Bit Bit Bit Bit
RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0
7 6 5 4 3 2 1 0
LED LED LED LED LED LED LED LED
7 6 5 4 3 2 1 0
0 1 0 0 1 0 0 1 0 OFF ON OFF OFF ON OFF OFF ON
Note: rotating the contents of shift register does not affect the display in
portb (rotated contents of shift register will be displayed once they are
moved to portb)

EXAMPLE 3 | Writing the Source Code


DISPLAY

shift register portb (output)


C Bit Bit Bit Bit Bit Bit Bit Bit
RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0
7 6 5 4 3 2 1 0
LED LED LED LED LED LED LED LED
7 6 5 4 3 2 1 0
0 1 0 0 1 0 0 1 0 ON OFF OFF ON OFF OFF ON OFF

DELAY

ROTATE

shift register portb (output)


C Bit Bit Bit Bit Bit Bit Bit Bit
RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0
7 6 5 4 3 2 1 0
LED LED LED LED LED LED LED LED
7 6 5 4 3 2 1 0
1 0 0 1 0 0 1 0 0 ON OFF OFF ON OFF OFF ON OFF

 Then go back to DISPLAY then DELAY then ROTATE and so on……….


 As a result, we can observe in the output the following output transitions
(in binary):

portb (output)
RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0
LED7 LED6 LED5 LED4 LED3 LED2 LED1 LED0
st
1 0 1 0 0 1 0 0 1
nd
2 1 0 0 1 0 0 1 0
3rd 0 0 1 0 0 1 0 0
th
4 0 1 0 0 1 0 0 1
th
5 1 0 0 1 0 0 1 0
6th 0 0 1 0 0 1 0 0
th
7 0 1 0 0 1 0 0 1
th
8 1 0 0 1 0 0 1 0
and so on……
* notice the 3 high (ON) bits moving to the left every after transition
 This is an example of a program code that requires a DELAY
subroutine.
 DELAY is ONLY REQUIRED if you want to display different outputs
without waiting for a particular input
 If output depends on the input, then DELAY is not required.

EXAMPLE 3 | Writing the Source Code

You might also like