This is only a preview of the September 1988 issue of Silicon Chip. You can view 43 of the 104 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Articles in this series:
Items relevant to "High Performance AC Millivoltmeter":
Articles in this series:
Articles in this series:
Articles in this series:
Articles in this series:
|
DIGITAL
FUNDAMENTALS
Microprocessors don't do anything until they
are programmed. Our final chapter in the series
takes a look at programming basics.
LESSON 10: PROGRAMMING MICROCOMPUTERS
By Louis E. Frenzel
MICROCOMPUTERS BY THEMSELVES DON'T do
very much. They are simply a collection of electronic
circuits and other hardware waiting for directions
which tell them what to do. Those directions come
from the programs that cause the microcomputer circuits to perform in a specific way. Once given a program, the microcomputer will accomplish some useful
work. All the programs that computers use are referred to as software.
In this final lesson, we discuss the basic process of
creating the software that will make the microcomputer do something worthwhile. More specifically, we
will show you how to write some simple programs using the microcomputer's instruction set.
·
Microcomputers have, for the most part, replaced
large complex collections of smaller SSI and MSI integrated circuits. By programming a microprocessor,
you can cause it to perform all of those basic operations which are usually carried out by individual gates
and flipflops wired in many different configurations.
Microprocessors can be programmed to perform
arithmetic operations, logical operations such as AND,
OR and NOT, counting, shifting, comparing, and many
other standard digital functions. We will show you
how to accomplish many of those basic operations in
this lesson.
Introduction to Programming
Programming is the process of converting a problem
to be solved into a form that the microcomputer can interpret. During that process, a program is created.
Recall that a program is a sequential list of microcomputer instructions that tell how to accomplish some
specific result.
Programming is basically a two-step process. The
66
SILICON CHIP
first step is problem solving where you define the work
to be done and develop a solution. The second step is
called coding. That is the process of converting your
problem into a specific computer language. In this
case, that language is the specific instruction set of
the microcomputer. You will hear it called machine
language. Following the work we started in Lesson 9,
we will use the instruction set of the type 6800/6502
microprocessors.
Problem Solving
The first step in programming is to clearly define
the problem. That is where you ask yourself what it is
that you are trying to do. The best way to begin the
process is to write down a general statement of what
you are trying to accomplish. Anything else you can do
to explain or illustrate the problem will also be helpful
to you in coming up with a solution.
For example, draw yourself a picture using blocks
that might show the flow of the problem from one step
to the next. If necessary, make charts and tables or
lists that specifically state what needs to be done.
Keep in mind a picture of the basic computing process,
which involves input data of some kind that is then
processed to create some useful new output data.
Defining the available inputs and the desired outputs
will help you understand exactly what processing
must take place.
Next, analyse all of the material you have collected.
That will result in your coming up with an algorithm.
An algorithm is a detailed step-by-step procedure for
solving the problem. It could be a basic algebra statement or formula to compute. Otherwise, it might be
even more like a recipe with individual steps to be accomplished one after another.
Fig.1: a flowchart is useful for
describing the problem to be solved.
This one is for determining the
larger of two numbers, A and B.
START
ACC A
CONDITION CODE
REGISTER (CCR)
ACCUMULATORS{'
•
INSTRUCTION REGISTER (OP CODE)
ACC B
,...L=====
NEGATIVE
ZERO
---CARRY
PROGRAM COUNTER
STACK POINTER
NO (A>B)
INDEX REGISTER
Fig.2: this is the basic register organisation in the 6800
microprocessor. Note that there are two accumulators.
STORE B
STORE A
But to refresh your memory, let's take a brief look at
the architecture of the device and the formats of the
instructions.
STOP
One way to illustrate the problem you are trying to
solve and test is to draw yourself a flow chart. That is
a graphical description of the problem using standard
symbols. Fig.1 illustrates the solving of a typical problem. The oval symbols represent start and stop conditions. The rectangular boxes state specific processing
operations to be carried out on your data. The
diamond-shaped figure represents a decision, which
usually asks a question with a "yes" or "no" answer.
Work your way through the problem in Fig.1 to be sure
that you understand how it works.
Coding
Once your problem is well-defined and your
algorithm is available, you can begin writing the program. That is the process of creating a sequential list
of the microprocessor's instructions.
When you finish coding the program, you should
check it over carefully for errors. Programming is a
very time-consuming and tedious process and it is extremely easy to make a mistake. You may goof in thinking through your algorithm and there may be some
faulty logic that you will not discover until you test the
program. Or you may simply make a coding error,
leaving out an instruction or entering an incorrect op
code or address. In any case, attempt to get out all the
bugs.
Next, you will load the program into the microcomputer and test it. If everything is satisfactory, the computer will run the program and will give the desired
results. If not, you will need to do some debugging.
Debugging is typically standard procedure, particularly for large, complex programs. Short programs
can usually be written and run the first time without
error. But big programs, using hundreds or even
thousands of instructions, generally require hours of
debugging to make them work properly.
· As stated earlier, we will use the architecture and
instructions of the 6800 and 6502 microprocessors.
Architecture
Fig.2 shows the general layout of the 6800
microprocessor. Note that it has two 8-bit accumulators labelled ACC A and ACC B. An 8-bit instruction register holds the op code, and an 8-bit condition code register (CCR) indicates CPU status. Also included is a program counter, a stack pointer and an index register. Those are 16-bit registers.
We won't show the block diagram of the 6502 since
it is basically similar in its organisation. The main difference is that the 6502 has only a single accumulator
(A), but it does have two index registers labelled X and
Y. The instruction sets are also similar and we will use
instructions that are common to both.
In operation, the microprocessor fetches instructions which are in the memory, puts them into the instruction register, then decodes and interprets them.
The logic in the microprocessor then causes the circuitry to carry out the designated operations. Most of
those operations take place on data that is in the
accumulator.
Data may be brought in from the memory and stored
in the accumulator, then used in some arithmetic or
logic operation with the result stored in the accumulator. That result can then be transferred to a
storage location in memory. Once an instruction is executed, the next instruction in sequence in memory is
then fetched and executed. The process continues until the end of the program is reached.
Condition Codes
As the program instructions are executed, various
conditions are monitored and the results are stored in
the flipflops of the condition-code register (CCR).
Those flipflops are also known as status flags. Some of
the more common conditions monitored include carry
(C), zero (Z), and negative (N). For example, the carry
bit is set to binary 1 in the condition-code register if
the result of an addition in the accumulator causes a
carry to be generated out of the most-significant-bit
(MSB) position.
SEPTEMBER 1988
67
OP CODE
ADDRESS OR DATA
OP CODE
1/2 OF ADDRESS
Attit~1s { ~--------f
1/2 OF ADDRESS
Fig.3: a 2-byte instruction puts the complete address or
data in one memory location. A 3-byte instruction puts
only half the address in a memory location.
BINARY
HEXADECIMAL
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
Fig.4: binary to hexadecimal
conversion. The binary number
10001011 is the equivalent of
hex 8B.
The zero bit in the condition-code register is set if
the result of some operation in the accumulator is
zero. Should some arithmetic, logic, or other operation
result in the accumulator containing all zeros, the zero
flipflop is set to binary 1.
In a similar way, the negative or N flag of the
condition-code register will be set to binary 1 if the
result of an arithmetic operation produces a negative
result. A negative result is indicated by the most
significant bit of the accumulator [bit 7) being a binary
1.
The condition-code bits can then be monitored so
that decisions can be made by special jump and
branch instructions. We will illustrate these in the
programs to come.
Instruction Format
An instruction for the microprocessor consists of
one, two or three bytes stored in memory. Some simple
instructions contain only an 8-bit op code that
designates an operation to be performed.
Some instructions require two bytes, as shown in
Fig.3a. The first byte is the op code that tells the
microprocessor what is to be done. The second byte
usually contains an address that designates where the
data to be operated upon is stored. Alternatively, the
second byte may also contain the data itself to be used
in the operation.
A three-byte instruction [shown in Fig.3b) contains
an op code in the first byte; the remaining two bytes
contain the upper and lower halves of a 16-bit address, which usually designates the location where the
data to be operated on is stored.
The instructions, of course, are nothing more than
binary numbers. However, programmers don't work
with binary. It is too difficult to remember all those ls
and 0s. Most programmers who write programs in
68
SILICON CHIP
machine language typically use hexadecimal [hex)
notation. This is the process of replacing each 4-bit
binary group of numbers with its equivalent hexadecimal symbol. · The hexadecimal symbols include
the digits 0 through 9 and the letters A through F. Fig.4
shows the designations.
To express a given binary number in hex, simply
replace each 4-bit group with its equivalent hex digit.
For example, the binary number 10001011 in hexadecimal is 8B. This happens to be the op code for the
ADD instruction in the 6800. All of the instruction op
codes have a specific hexadecimal designation. In addition, the addresses are also expressed in hex
notation.
To keep things simple in this lesson, we are going to
use our own special notation. We will use a simple
three or four-letter mnemonic designation for each instruction and express memory locations and data in
terms of decimal numbers. We will also limit ourselves
to the use of two-byte instructions.
Simple Example
The simple program shown below is designed to add
two numbers together and store their sum. Let's step
through the program one instruction at a time so that
you can see how to read it.
LOCATION
MEMORY
INSTRUCTION
ADDRESS OR DATA
0
LDAA
1
7
2
ADD
8
STAA
3
4
5
6
1
8
9
9
HALT
25
19
44 (SUM)
The first instruction in location 0, LDAA, loads accumulator A with the number stored in memory location 7. That is designated by the second byte of the instruction in location 1. Looking down the program, you
can see that in memory location 7 is the number 25.
That first instruction brings the number 25 into accumulator A.
The next instruction in location 2 is ADD. This instruction tells the computer to add the number stored
at the memory location designated by the second byte
of the instruction [in location 3) to the number contained in accumulator A. As you can see, the number 19 is
stored in location 8. The sum, 44, will be stored in the
accumulator.
The third instruction in location 4, STAA, says to
store the accumulator in location 9. That is, the sum in
the accumulator will now be stored in the memory
location designated by the second byte of the instruction in memory location 5. The address stored there is
9. Therefore, the sum will be stored in that location.
The program ends at that point.
Now that you have the format in mind, let's take a
look at a number of sample programs that show how to
do useful functions. In each example we will show a
programming listing and explain what is happening.
We will introduce new instructions in each sample,
but those will be explained as we go along.
Arithmetic Operations
MEMORY
LOCATION
c-------~
INSTRUCTION
ADDRESS OR DATA
0
LDAA
Let's code the program
1
18
described by the flow chart
2
SUBA
3
19
in Fig.1. The purpose of the
4
BMI
program is to determine
12
5
6
LDAA
which of two numbers, A and
7
18
B, is larger. Our algorithm is
8
STAA
9
2D
to subtract B from A, then to
10
JMP
11
test to see if the difference is
22
12
LOAA
positive or negative. If the
13
19
14
STAA
difference is positive, then A
15
20
is larger than B. If the dif16
JMP
17
22
ference is negative, B is
18
A= 14
larger than A. The program
19
B = 17
20
LARGER=
17
concludes by storing the
21
larger of the two numbers in
22
HALT
texta designated memory location.
The program for solving the problem is illustrated at
the top of this page.
The first instruction, LDAA, loads the first number
A stored in memory location 18 into accumulator A.
The second instruction subtracts number B stored in
location 19 from number A. The difference between
the two numbers is stored back in the accumulator. If
A is 14 and Bis 17, the result in the accumulator is
-3.
The next instruction is a branch-if-minus (BMI) instruction. This is a decision-making instruction that
may change the sequence of program execution if a
certain condition occurs. In this case, the instruction
tests the negative flipflop in the condition code
register.
Recall that various flipflops in the condition code
register are set or reset depending upon the outcome
of the execution of the various instructions. If the subtraction results in a positive result in the accumulator,
then the negative flipflop in the condition code register
is set to binary 0. If the subtraction operation results
in a negative value, then the negative flipflop is set to
binary 1.
The branch if minus instruction looks at the
negative flipflop to determine what action to take. If
the subtraction operation results in a positive value,
no branch occurs. The next instruction in sequence
after the BMI instruction is executed as would normally be the case. Therefore, A is the larger of the two
numbers.
The LDAA instruction in location 6 causes that
number to be loaded into the accumulator. The next instruction, STAA, stores that number in the designated
memory location. Next, the JMP instruction causes an
unconditional jump to occur. The program counter is
loaded with the address part of the JMP instruction in
location 11. Therefore, the CPU resumes executing at
location 22. A HALT at that location ends the
program.
(Note: in this article, a HALT instruction is used to
stop a program. In truth, there is no such instruction in
the 6800 or 6502 . It is used here to simplify the discussion. In practice, other more complex techniques
beyond the scope of this article are used).
r
MAIN PROGRAM
I
I
I
I
INSTRUCTIONS
JUMP TO SUBROUTINE
-
'
•
SUBROUTINE
I
I
~
I
I
I
~
-- --
-
RETURN
I
l.
r
J
I
I
I
JUMP TO SUBROUTINE
_ _ _ _ _ _ _ _ _ ..JI
Fig.5: a small program which will be used many times
by the main program can be stored as a subroutine, to
which the main program jumps (and returns) when it's
needed.
If the result of the subtraction is a negative number,
as it is in this example, then the BMI instruction
detects it and causes the program to branch to a
memory address designated in the second byte of the
BMI instruction in location 6. In other words, the usual
next instruction in sequence is not executed.
The BMI instruction causes the program counter to
be loaded with the address designated in the BMI instruction, in this case 12. This indicates that the larger
of the two numbers is B. Therefore, the instruction at
12, an LDAA, causes the B to be loaded into the accumulator. The STAA instruction stores the result in
the memory location designated for the larger number.
The JMP instruction then causes the program to jump
to the designated memory location, in this case 22. At
that point, the program ends.
Subroutines
Most simple 8-bit microprocessors such as the 6800
and 6502 can only perform simple arithmetic operations such as add and subtract. But many times the
problem to be solved requires more complex math,
such as multiplication, division, square root or some
other operation. The microprocessors can perform
these operations, but they must be programmed to do
so. A special program is written to perform one of
these math operations.
Whenever the main program needs to perform, say,
a multiplication, it will branch to or call this special
multiplication program, perform the desired operation, and then return to the main program sequence.
This is illustrated in Fig.5 . Such programs, which may
be referenced several times by the main program, are
called subroutines. A subroutine is simply a specialised segment of computer code that does some specific
task.
Rather than write that sequence of code each time it
is needed, the code is written once. Then special inSEPTEMBER 1988
69
START
(PARTIAL
PRODUCT)
chosen here is simple and is easy to understand. Since
the microprocessor can add and subtract, naturally
the subroutine must use these instructions. One way to
perform multiplication is by repeated addition. For example, if you wish to multiply 5 by 3, you can accomplish it by simply adding 5 three times. This is
what our program will do.
Add to Multiply
LOOP
N
STOP
Fig.6: as this flowchart shows, multiplication is
done by repeated addition.
structions are used by the main program to call the
subroutine, use it, then return to the main program. A
jump to subroutine instruction [JSR) calls up the
subroutine. This instruction's address tells where the
subroutine is located. When a JSR is executed, the
contents of the program counter are stored in the
stack. That allows the CPU to pick up where it left off
when the subroutine has been executed.
At the end of the subroutine is a return-fromsubroutine instruction (RTS). It retrieves the address
in the stack and loads it into the program counter. The
main program then resumes executing.
The program we want to illustrate here is a
subroutine for performing multiplication. It will illustrate the concept of a subroutine and how higherlevel math functions are programmed using the
microprocessor instruction set.
There are a number of ways to produce multiplication in a microprocessor. The algorithm we have
70
SILICON CHIP
Recall that in multiplication, there are usually two
quantities involved. One number is called the multiplicand while the other is called the multiplier. The
answer or solution to the multiplication results in a
new value called the product. In the microprocessor
we will set aside three memory locations to store those
three values.
The flow chart outlining the multiplication is shown
in Fig.6. Basically, the process is to add the multiplicand for the number of times specified by the
multiplier. In the program, we designate the location
where the final product will be stored. During the
multiplication process, the partial product obtained
each time the multiplicand is added is stored in that
location. Initially, the partial product is zero. Then,
each time we pass through the program, the multiplicand is added to it and then restored.
Each time the multiplicand is added, the multiplier
is loaded into the accumulator and one is subtracted
from it. That tells us that the multiplicand has been added one time. We then reduce the multiplier by 1 and
restore it. We then check to see if the multiplier is
zero. Naturally, if it is zero, then the multiplicand has
been added the correct number of times. If not, then
the program goes back and repeats the same sequence. Note in the flow chart the use of a decision
block designated Is multiplier zero? If the answer is
yes, then the correct product is available for use.
If the multiplier is not zero, the answer is no and the
procedure begins again. That forms a program loop. A
loop in programming terms simply means that a particular sequence of instructions is repeated a number
of times as required by the algorithm. A loop is implemented with a decision making branch or jump
instruction.
The code for the problem is given below. Let's walk
through it step-by-step to be sure you understand the
process.
The first instruction, CLR, clears the memory location designated by the second byte to be cleared to
zero. As you can see, that clears memory location 20
where the partial product is to be stored.
The next instruction, LDAA, causes the partial product to be loaded into the accumulator. Of course, it is
zero at this time. The next instruction, ADD, causes
the multiplicand in location 18 to be added to the accumulator. The result in this case is simply the
multiplicand itself. The next instruction, STAA,
causes the contents of the accumulator to be stored
back into location 20, the partial product.
Next, the LDAA instruction in location 9 causes the
multiplier in location 19 to be loaded into the accumulator. The DECA instruction then decrements it.
MEMORY
LOCATION
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
INSTRUCTION
ADDRESS OR DATA
CLR
20
LDAA
20
ADO
18
STAA
20
LOAA
19
OECA
STAA
19
BNE
3
RTS
MULTIPUCAND
MULTIPLIER
PRODUCT
(PARTIAL PRODUCT)
That instruction simply causes 1 to be subtracted from
the contents of the accumulator. To decrement means
to subtract one.
The multiplier has now been reduced by 1, indicating that the multiplicand has been added once.
We restore the reduced multiplier in location 19.
Next, a BNE (branch if not equal to zero) instruction is
executed. That instruction looks at the decremented
multiplier in the accumulator and attempts to determine if its value is equal to zero. If it is not equal to
zero, then it causes the normal program sequence to
be abandoned. A branch occurs to the memory location designated in the second byte of the BNE
instruction.
In this case, the branch occurs back to memory
location 3. This causes the instructions stored between locations 3 and 13 to be re-executed. The process, therefore, is repeated. The multiplicand is added
again to the partial product and the result is restored.
The multiplier is loaded and decremented by 1 and
restored. Again, the BNE checks for any zero result. If
none occurs, then a backwards loop is again
performed.
At some point, however, the multiplier will be
reduced to zero. At that point the BNE instruction
detects the zero so no branch or loop occurs. The next
instruction in sequence at location 16 is executed.
That is a return from subroutine (RTS) instruction. In
a subroutine, the final instruction is called a return
from subroutine. The instruction simply retrieves the
content of the program counter which was previously
stored in the stack by the jump to subroutine instruction that caused the subroutine to be called. At that
point, the main program resumes.
cumulator while the other is stored in a memory location that will be referenced by the AND instruction.
The AND instruction is designated ANDA which means
to AND the contents of accumulator A with the contents of the memory location designated in the instruction. The corresponding bits in the two 8-bit words will
be ANDed together with the logical result being stored
back in that same bit position in the accumulator (see
Fig.7). The individual bits being operated on are independent of one another and do not affect adjacent
bits. Because of this, you can perform various individual bit manipulations within a word if desired.
OR operations can also be performed with the ORA
instruction. The EXCLUSIVE OR function is performed
with the EORA instruction.
As for performing the NOT function, a special instruction called complement [COM) is used. Here a
word is loaded into the accumulator and the COM instruction is executed. The result is that each bit in the
word is complemented; that is, ls are changed to Os
and Os changed to ls. With those basic logic functions,
virtually any Boolean operation can be implemented
with short subroutines.
An example of a useful logical operation is to use
the AND instruction to perform masking. Masking is
the process of blocking out or eliminating a bit or
groups of bits from a binary word. When the bits are
eliminated, they are effectively set to or replaced by
binary Os. To mask bits from a given word, that word
is first stored in the accumulator. Then, a special
mask word in some memory location is ANDed with it.
To eliminate the desired bits from the word, the corresponding bits in the mask are set to binary 0. Those
bits to be retained are ANDed with binary ls in the
mask word. Here's an example:
A - BINARY WORD 11001100
B - MASK WORD 00111100
C - RESULT OF ANDING 00001100
The truth table for the basic AND function is given
below as a refresher.
8
C
0
1
0
1
0
0
0
1
The simple program below illustrates the use of the
AND instruction for masking. The purpose of the program is to convert ASCII characters into standard
BCD numbers.
Logical Operations
As we indicated, a microprocessor can also perform
logical operations such as AND, OR and NOT. It can
even perform more complex operations such as EX-.
CLUSIVE OR. The 6800 and 6502 microprocessors, for
example, have instructions for those operations. The
instructions operate on all of the bits in the accumulator rather than one bit at a time.
For example, to perform an AND operation between
two binary numbers, one of them is stored in the ac-
A
0
0
1
1
MEMORY
LOCATION
INSTRUCTION
ADDRESS OR DATA
0
1
2
3
4
LDAA
5
6
7
8
9
10
8
ANDA
9
STAA
10
HALT
00111001 (ASCn CODE FOR 9)
00001111 (MASK WORD)
00001001 (BCD RESULT}
ASCII Code
Recall that ASCII is the 8-bit code widely used for
communications between computers and peripheral
SEPTEMBER 1988
71
ACCUMULATOR
__ MEMORY _LOC&_J:ION
Fig.7: the AND instruction is designated ANDA which
means to AND the contents of accumulator A with the
contents of the memory location designated in the
instruction.
devices or in data communications. A special 8-bit
code is given to upper and lower case letters of the
alphabet, numbers, punctuation marks and special
symbols. The ASCII codes for the numbers 0 to 9 turn
out to be the standard 4-bit BCD codes for those
numbers plus a special 4-bit code preceding it, in this
case 0011. For example, the BCD number for 5 is 0101.
The ASCII version, therefore, is 00110101.
In order to use ASCII numbers in arithmetic operations, they must first be converted into their BCD
equivalents. A simple way of doing that is to strip off
the first four bits, leaving the 4-bit BCD code. That can
Mailbag -
SILICON CHIP
Implementing Counters
A microprocessor can also be used to perform counting functions. For example, by using the interrupt
feature of a microprocessor, external pulse trains can
be counted. The pulses to be counted are simply applied to the interrupt input of the microprocessor.
continued from page 3
This was because: (a) the additional
switches installed in the caravan
were only single pole instead of double pole; and (b) the inlet socket on the
caravan had been incorrectly wired
at the factory, even though it was
clearly marked "A" and "N" on the
appropriate terminals.
Prominently displayed in the
caravan was a sign to the effect that
all electrical wiring had been inspected and passed. It had been used
like this for many years. On the table
was an old toaster with hinged sides,
so it was surprising that no-ori.e had·
been zapped. After this initial wiring
fault was corrected, the single-pole
switches were replaced with doublepole types.
The third example was reported in
another magazine many years ago.
As I recall, a young child was killed
because the electrician who wired
the house had incorrectly installed a
combination GPO with a coax TV
socket. The insulating shroud over
the back of the coax socket had been
omitted and, as a result, the outer
braid of the coax cable had come into
contact with the Active terminal.
This caused the metal roof and
downpipes of the house to become
live at 240V AC. The downpipes
finished about 15cm above ground
72
be done with a masking code of 00001111. The first
four zeros will be ANDed with the 0011 portion of the
ASCII code we wish to eliminate. ANDing those digits
with zero will cause the logical result to be 0000. The
four bit BCD portion of the code we wish to retain will
be ANDed with the 1111 portion of the mask word.
ANDing bits with binary 1 's will simply reproduce
those bits.
In the program above, the ASCII number has been
previously stored in memory location 8. That number
is now brought into the accumulator with the first instruction, LDAA.
Next, the ANDA instruction is executed. The mask
word is stored in location 9. That number is ANDed
with the contents of the accumulator. The result appears in the accumulator. In this case, the ASCII
number 9 is converted to its BCD equivalent,
00001001. That number is then stored in location 10
by the STAA instruction. Then the program halts.
level and when the child touched the
downpipe, a fatal shock was received. This house wiring had also been
inspected and passed.
In conclusion, I should say that noone is perfect but as you pointed out,
there is room for improvement in the
electrical trade, especially with
regard to council electricians and
inspectors.
M. C. Laybutt
Ainslie, ACT
Good mix of projects
Congratulations on a very good
magazine. It is well ahead of all the
other electronics magazines in the
marketplace.
Electronics and other journalists
have a privileged position as far as
keeping up with new products and
technology. Notice I did not say that it
is an easy job. They cannot really be
effective without keeping abreast.
Manufacturers and suppliers make it
easier by supplying literature, demos,
samples etc in the hope of getting
press releases published.
Compare this with the engineer
who chose to go into industry, say into
designing power supplies. Now let's
reduce his field of vision further by
saying switching power supplies. Only those suppliers that have corn-
ponents that go into switching supplies send him data - and then only
half of those because he is, unlike the
magazines, unknown to many.
His principal job is to design new
power supplies but a large proportion
would consist of other items, such as
investigating why a particular design
fails for customer A but not for
customer B. Or marketing may say
they can get an order for 100 units if
you can increase the output voltage
by a couple of volts or increase the
output current by lO0mA. Such tasks
are often made worse because he
was not the original designer.
And of course there's the paperwork like writing engineering change
instructions to manufacturing. This
engineer doesn't have a chance of
knowing about memory mapping of
microprocessors or 100 other things.
Changing the subject, magazine articles are not sufficient to act as an
instruction manual. While I and many
other people can build some circuits
from a schematic, there is a large
potential kitset builder market out
there who need step by step instruction. In this day of wordprocessing
and cut and paste desktop publishing,
there is no reason decent instruction
manuals could not be reasonably
priced.
D. Hire
Annandale, NSW
Then, each time the interrupt occurs, the
microprocessor will complete the current instruction
in process for the main program, then jump to a
subroutine for the count operation. The simple program below is the subroutine that performs the counting operation.
MEMORY
LOCATION
INSTRUCTION
ADDRESS OR DATA
100
101
102
103
104
105
106
LDAA
INCA
STAA
COUNT VALUE
RTS
Memory location 105 is set aside to store the count
value. It is previously cleared to zero in the main program. When the interrupt occurs, that word will be
loaded into the accumulator by the LDAA instruction.
Next, an INCA instruction is executed. That causes
the number in accumulator A to be incremented. Incrementing with this instruction causes 1 to be added
to the accumulator value. The next instruction, STAA,
causes the number to be restored in memory location
105.
Finally, an RTS instruction causes a return from the
subroutine. The program counter value which was
stored away in the stack when the interrupt occurred
is retrieved by the RTS instruction and loaded back into the program counter. Therefore, the main program
resumes where it left off.
Counting routines can also be used to provide for
timing, sequencing and delay operations which are
common in digital circuits. For example, the simple
subroutine shown below generates a specific time
delay when triggered by an interrupt. At the end of the
delay time, the program outputs a binary 1 in the least
significant bit position of the data bus.
MEMORY
LOCATION
INSTRUCTION
ADDRESS OR DATA
52 .
LDAA
64
OECA
STAA
64
BNE
52
LDAA
65
STAA
100
RTS
COUNT VALUE
00000001
OUTPUT PORT
53
54
55
56
57
58
59
60
61
62
63
64
65
100
START
Memory location 53 is assigned a count value. Next,
that count number which is proportional to the length
of the delay is loaded into the accumulator by the
LDAA instruction. For short delays, a low value
number will be used. For long delays, a high value will
be used. The decrement instruction (DECA) subtracts
one from it. It is then restored by the STAA.
Next, the BNE or branch if not equal to zero instruction is executed. That cheoks to see if the count value
has been decremented to zero. If it hasn't, the program loops back to location 52 for additional
decrementing. The process repeats as the counter is
decremented once each pass through the loop. When
the count value reaches zero, the BNE instruction
NO
STOP
Fig.8: this is the flow chart for the time delay
program shown on this page (the one that starts at
memory address 52). Starting at the top, we find
that the program loads a count value, which is then
decremented by 1 and eventually tested against
zero. If the decrement doesn't result in zero, it is
repeated until it does equal zero. When the count
equals zero, a binary 1 is loaded in the accumulator
and then output to port 100.
detects it. It then executes the LDAA instruction in
location 59. That loads the number 00000001 into the
accumulator. Then, the STAA instruction outputs this
value (00000001) to the address designated (100).
In this program, the length of the delay is determined by the time it takes for the various instructions to
be executed. Most of the time involved is that time required to load, decrement and store the count value.
Since most microprocessors are controlled by a
crystal clock oscillator, the clock cycle time is precisely known. With that information then, the time it takes
to execute various instructions can also be determined. By knowing the amount of time it takes for each instruction to be executed, the delay count .value can be
determined to create a delay of a specific duration.
This program ends with the RTS, which was triggered by an interrupt.
~
SEPTEMBER 1988
73
|