![]() |
||||
![]() |
||||
Outputting multiple bitsBoth previous examples showed you how to change just one bit. If you have indeed done some experimenting of your own, you may have toggled multiple bits at the same time. Toggling a few bits is the easiest part. But what if we have, let's say 3 bits, that must be send to the output without disturbing the rest of the output bits? In our next example we're going to have a counter that counts binary from 0 to 7, requiring 3 bits. These three bits must be sent to the bits 2, 1 and 0 of output port A, without disturbing the other 5 bits of the output port. |
|
;------------------------------------------------------------------------
;
; LESSON4C.ASM
;
; Binary counting in bits 2, 1 and 0 of port A only
;
;------------------------------------------------------------------------
.IN INIT02 Initialize the assembler
;------------------------------------------------------------------------
MVI A,11111111B Switch all port B LEDs off
OUT PORTB
MVI A,00110000B Just a random state of port A
OUT PORTA
MVI D,0 Clear the binary counter
COUNT MOV A,D Mask only the bits 2, 1 and 0 of the
ANI 00000111B binary counter
XRI 00000111B Invert the bits (LED is on if bit = 0)
MOV B,A
IN PORTA Mask all other bits of rest of the
ANI 11111000B output port
ORA B Combine both bytes
OUT PORTA and send it to the output
MVI B,200 Wait about one second
CALL DELAY
INR D Increment binary counter
JMP COUNT Repeat endlessly
|
||
|
So what's new in this program?
First of all I show a different way of keeping a copy of the output port.
Until now we have kept a copy of the output port in a processor register.
In this example we keep a copy of the output port in the output port itself!
Here's how this program works:
|
|
Same goal, different bits
In our previous program we were lucky that we had to change the 3 least significant bits, because the neatly aligned with the 3 bits of the counter.
But what if we had to output the counter in bits 6, 5 and 4 instead?
|
|
;------------------------------------------------------------------------
;
; LESSON4D.ASM
;
; Binary counting in bits 6, 5 and 4 of port A only
;
;------------------------------------------------------------------------
.IN INIT02 Initialize the assembler
;------------------------------------------------------------------------
MVI A,11111111B Switch all port B LEDs off
OUT PORTB
MVI A,00000011B Just a random state of port A
OUT PORTA
MVI D,0 Clear the binary counter
COUNT MOV A,D Roll bits 2, 1 and 0 into place
RLC
RLC
RLC
RLC
ANI 01110000B Mask the required bits
XRI 01110000B Invert them
MOV B,A
IN PORTA Mask all other bits of rest of the
ANI 10001111B output port
ORA B Combine both bytes
OUT PORTA and send it to the output
MVI B,200 Wait about one second
CALL DELAY
INR D Increment binary counter
JMP COUNT Repeat endlessly
|
||
|
Only minor changes had to be applied to the previous program to get to this one.
Mainly the bit masks were different, and we had to roll the 3 bits from 2,1,0 to 6,5,4.
The 4 RLC instructions just do that.
Hopefully this example teaches you that you should design your hardware carefully. Planning your output lines ahead will possibly reduce the program overhead of moving the bits in place. |
|
| Continue To Lesson 5 - Introducing Inputs | |
| [Home] [Latest News] [Essentials] [Hardware] [The Build] [Programs] [Projects] [Downloads] | |||