This is only a preview of the July 1993 issue of Silicon Chip. You can view 37 of the 96 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:
Articles in this series:
Items relevant to "Build A Single Chip Message Recorder":
Items relevant to "Light Beam Relay Extender":
Items relevant to "Build An AM Radio Trainer; Pt.2":
Articles in this series:
Items relevant to "A Low-Cost Quiz Game Adjudicator":
Articles in this series:
Articles in this series:
|
Lesson 1
Programming the Motorola
68HC705C8 microcontroller
Following our series on the MAL-4 Microcon
troller Aid for Learning, we now present a series
of lessons on programming its central device:
the MC68HC705C8 microcontroller. If you’ve
always wanted to learn about microcontrollers,
this series will give you a good grounding.
By BARRY ROZEMA
Welcome to the fascinating world of
microprocessors and microcontrollers.
Over the next few months, we will
be showing you how to program the
Motorola MC68HC705C8 single chip
microcontroller. This microcontroller
unit (MCU) is a member of the large
6805 family and the lessons will be
based on its internal architecture and
instructions set. We will be using
the MAL-4 Microcontroller Aid for
Learning for the practical parts of the
lessons (see SILICON CHIP – Nov-Dec.
1992 & Feb. 1993).
The lessons are aimed at everyone
from keen beginners to those with
some programming experience but
who need to refresh their knowledge
of microcontrollers. However, to get
the most from the lessons, you should
at least have a reasonable grounding
in digital electronics.
Each lesson will consist of:
• Theory.
• Practical program examples.
• Things to do between lessons.
• A new program to try.
Note, however, that the lessons
Fig.1: this diagram is called
a “programming model” &
shows the various internal
registers of the 68HC705C8
microcontroller.
7
6
5
4
3
2
ACCUMULATOR
7
6
5
4
3
INDEX REGISTER
7
1
6
1
5
4
3
STACK POINTER
1
0
2
1
0
2
1
0
8 BITS
8 BITS
15
0
14
0
13
0
12
0
11
0
10
0
9
0
8
0
Hardware
6 BITS
15
0
14
0
13
0
12
11
10
9
8
7
6
5
PROGRAM COUNTER
4
3
2
1
0
4
H
3
I
2
N
1
Z
0
C
13 BITS
CONDITION CODE REGISTER
7
1
6
1
5
1
HALF CARRY (FROM BIT 3)
INTERRUPT BIT
NEGATIVE
To carry out the practical aspects of
the course, you will require a MAL-4
Microcontroller Aid for Learning with
a power supply and loudspeaker. You
should be well read with regard to the
MAL-4 operations manual and you
should be able to load programs and
run them.
Programming concepts
ZERO
CARRY (FROM BIT 7)
5 BITS
22 Silicon Chip
will be specific to the MC68HC705C8
micro
controller and its associated
hardware. It will also be necessary for
you to do some further reading after
completing each lesson.
As you are probably aware, when
it comes to electronics, you can never have too many text and reference
books. The microcontroller area is no
exception.
There have been many good text
books written about microprocessors,
most of which have been written for
specific devices. Unfortunately, I have
not seen any based on the 6805 family
to date. However, any microprocessor
text written around an 8-bit Motorola
processor or 6502 processor may suffice; eg, Microprocessors & Microcomputers, The 6800 Family or The 6502
Family, by Ronald J. Tocci & Lester P.
Laskowski (Prentice-Hall).
A good digital text book will also be
of value; eg, Digital Systems, Principles & Applications, by Ronald J. Tocci
(Prentice-Hall).
You will also need the MAL-4 Operation and Construction Manual, plus
two publications from Motorola:
(1) Microprocessor, Microcontroller &
Peripheral Data, Volumes 1 & 2.
(2) MC68HC705C8 8-Bit Micro
con
troller Unit. Technical Summary. BR
594/D Motorola.
All microprocessors and microcon
trollers, from those used in main
frames to the simplest MCUs, must
be capable of running a “program”. A
program is simply a list of instructions
that are to be carried out (executed)
in a given order. All microcontrollers
run in “machine code”, although this
is usually hidden from us by what
are called “higher level languages”.
Some typical higher level languages
include Pascal, Forth, Basic, DOS
and Windows. Even the humble word
processor can be regarded as a higher
level language.
The level of instructions in a given
application depends on the power of
the processor. For example, an adult
human can easily understand and carry out an instruction “go to the shop
and get some milk please”.
No other information would be
necessary. But if you wanted a child
to perform the same task, you would
have to give the correct change for the
milk, instruct him on how to cross the
road safely, and provide other information that would not be necessary
with an adult.
Now try to give the same task to a
robot. You would have to give every
detail of the job. For example, you
would have to tell the robot how and
when to lift its legs, how to maintain
its balance and how to get out of the
door – and that’s before you even tell
it how to successfully find its way to
the shop. In fact, the task is probably
impossible given today’s level of robot
development.
Machine code
All microcontrollers are similar
in the way they are pro
grammed.
You cannot say “go and get the milk
please”. Instead, you have to lay out
each step in machine code form.
Machine code – also called opcode (for operation code) – is issued
in binary form and is simply an instruction code for a given machine or
microprocessor. In the case of the 8-bit
MC68HC705C8 microcontroller, it
takes the form of an 8-bit binary code.
To make it easy for us, this binary code
is hidden in the form of other numbering formats, such as octal, hexadecimal
or decimal.
In most cases, the hexadecimal format is used for programming and this
also applies to the MAL-4. But what
ever the programming format used,
the processor still works in binary.
Hexadecimal numbers in these lessons
will have a “$” sign in front of them;
eg, $1234, $EA, $1FFE.
If we wanted a program larger
than say 10 bytes, it would be very
difficult to write in either binary or
hexadecimal format. That’s because
we are not very good at remembering
numbers and because we find it hard
to relate hexadecimal numbers to
events. Although the microprocessor
understands binary instructions, we
need to program in “plain” language.
For example, to load an accumulator we could use the in
struction
“Load Accumulator”, or other in1
START
structions which are then converted
into hexadecimal op-code. This code
is stored in the memory and the MCU
then “runs” the program.
Programming model
In order to program the
MC68HC705C8, we need to find out
what internal registers are available. Fig.1 shows the details for this
device. This diagram is called the
“programming model” (all microprocessors have one) and it enables the
programmer to best utilise the CPU.
A brief explanation of the registers in
the MC68HC705C8 follows.
Accumulator
2
SET
REGISTER
3
DECREMENT
REGISTER
4
ZERO?
NO
YES
5
END
Fig.2: this flowchart shows an
MCU counting down to zero.
Symbols 1 & 5 are terminators,
symbols 2 & 3 are process blocks,
& symbol 4 is a decision block.
structions like “Store Accumulator”
and “Or Accumulator”. This is how
the instructions are written for the
human side of the program, though in
a shortened form called “mnemonics”
(pronounced nu-mon-ics). The last
three instructions in mnemonic form
would look like this:
LoaD Accumulator - LDA
STore Accumulator - STA
OR Accumulator - ORA
To write a program using mnemonics, the programmer first thinks of the
instruction in plain language. He then
finds the correct mnemonic and looks
up the applicable binary machine code
(usually given in hexadecimal). The
MC68HC705C8 has 62 of these mnemonic instructions – see p.28 of the
MC68HC705C8 Technical Summary.
A typical microprocessor program
consists of a series of mnemonic in-
The accumulator is the workhorse
of the CPU. Some micropro
cessors
have more than one accumulator but
the 6805 only has one. A better name
for the accumulator would be “general purpose, do-almost-everything
register”.
Like the MCU, the accumulator
is eight bits wide. Its cont
ents can
be loaded from memory or stored in
memory. We can add, subtract and
multiply with it; perform logical functions (AND, OR, NOT & XOR) with it;
shift it right or left; rotate it right or
left; increment or decrement it; and
negate it (2s complement).
To understand how the accumulator
is used, picture yourself at the kitchen
sink washing dishes. With one hand
you grab a dirty plate and place it in
the sink. You wash it. With your other
hand, you then take the clean plate out
of the sink and place it on the dish
rack to dry.
This process is analogous to the
way in which we use the accumulator
– for example, to take data from the
input port and place it on the output
port. To do this, we must first LOAD
the accumulator from the input port,
then STORE the accumulator at the
output port.
Going back to our analogy, when
washing up, we can not send the
dirty plate directly to the dish rack –
we must handle it and wash it up. A
similar situation applies to data – it
must be “handled” by the accumulator.
The 6805 family has no instruction to
direct data around the memory; eg,
from output to input.
Index register
The index register operates in a
similar manner to the accumulator.
July 1993 23
Table 1: Noise 1 Laser/Spaceship Sound Generator Program
Address
Date
Label
Mnemonic
0030
A6 00
Start
LDA
#$00
;Set highest frequency
0032
B7 BF
STAZ
TEMP
;Store at temporary memory
0034
B6 BF
LDAZ
TEMP
;Get delay time
0036
CD 14 A1
JSR
D100US
;100us X ACC delay
0039
B6 02
LDAZ
PORTC
;Get speaker
003B
A8 80
EOR
#$80
;Complement speaker
003D
B7 02
STAZ
PORTC
;Store speaker
003F
3C BF
INCZ
TEMP
;Inc delay time
0041
B6 BF
LDAZ
TEMP
;Get delay time
0043
B1 00
CMPZ
PORTA
;Compare with Port A
0045
27 E9
BEQ
START
;Start again if equal
0047
20 EB
BRA
LOOP1
;Back to Loop 1
Loop 1
Comments
Comment: this program causes the MAL-4 to generate a laser/spaceship sound from its loudspeaker.
The program is loaded from RAM location $0030. The DIP switch on PORT A changes the sound. Be
sure to enable PORT A by turning DIP SW2 7 (E) on. Try changing location $0031 from $00 to other
values.
Like the accumulator, it is an 8-bit
register but it can not do all the
arithmetic and logic operations that
the accumulator can do. However,
the index register can perform a very
powerful form of programming called
indexed addressing, whereby it is
used to point to individual memory
locations.
START
SET DELAY
TIME
Stack pointer
This 6-bit register is used by the
CPU itself. The stack pointer points
to a RAM location where the CPU can
store or load data. The stack pointer
moves down to store new data or
moves up to load old data. In this
respect, it functions like a FILO (First
In Last Out) register. The stack pointer
memory range is $00FF-$00C0.
Program counter
The program counter is a 13-bit
register that keeps track of a program’s
location in memory. When we start
a program from a given memory location, the program counter must be
loaded with the start location. As the
program progresses through memory,
the program counter keeps track of
the location. If the program has to
jump or branch to another memory
location, this is achieved by changing
the program counter. The 13 bits of
the register give an addressing range
of 8Kb ($0000-$1FFF).
Condition code register
The condition code register (some24 Silicon Chip
DELAY
100us X
ACC
Flowcharts
The old saying “a picture is worth
a thousand words” is very true, especially when we are programming
MCUs. The “picture” that programmers use is called a flow chart. A flow
chart con
sists of symbols “strung”
together, usually stacked vertically
from top to bottom. Each symbol indicates a particular function, either
carrying out an instruction or making
a decision.
For example, the flowchart in Fig.2
shows an MCU counting down to zero.
This could be used to give a time delay.
Symbols 1 and 5 are terminators – they
indicate the start and finish of a program (or part of a program). Symbols 2
and 3 are process blocks and indicate
a process or operation.
Symbol 4 is a decision symbol. It
has one input and two outputs. One
output is for a result that is true,
while the other is for a result that is
untrue. The result from a decision
is bi
nary; ie, true/false, yes/no or
zero/one.
The best way to study a flow chart
is to follow it with your finger in
the direction of the arrows until you
come out at the end. Although there
are many flow chart symbols, we will
only use five basic types during this
course.
Subjects to study
COMPLEMENT
SPEAKER
INCREMENT
DELAY
EQUAL TO
PORT A?
decide if it should branch. A detailed
look at each instruction will show how
and which flag is affected.
YES
NO
Fig.3: this is the flowchart for the
laser/spaceship sound program
listed in Table 1.
times called the flag register) is a 5-bit
register. This register tells us what
happened in the ALU (arithmetic logic
unit) on the previous instruction(s).
If we use conditional branching, the
CPU looks at the flags in this register to
Following is a list of things to study
before next month’s lesson, listed in
order of importance:
(1.) Microprocessor fundamentals: basic operation; block diagrams; central
processing unit (CPU)
(2.) Numbering systems: binary to
hexadecimal to decimal
(3.) Motorola MC68HC705C8 8-Bit
MCU Technical Summary; pages 1-11,
28-31.
Sample program
Finally, Table 1 list a simple program that generates laser/spaceship
sounds using the MAL-4. Try entering the program as set out in the
instruction manual and be sure to
enable PORT A by turning DIP SW2
7 (E) on.
The program is loaded from RAM
location $0030 and the DIP switch on
SC
PORT A changes the sound.
|