Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Wichit Sirichote
1
Preface
This programming book is designed for self-study how to program the 6802
microprocessor with machine language. The 6802 is software compatible
with the Motorola 6800 microprocessor from 1976. All most more than 40
Yrs ago. No manufacturing now. In the modern day with high complicated
microcontroller why we learn this chip? There will be many answers. For me,
the 6802 CPU has very nice addressing modes, instruction set and memory
organization.
The lab book provides 10 programs with source code in Assembly language
and instruction hex code. Students can test and do the experiments by
entering the hex code and run it easily.
For hex code modification with 6802 instruction, please get the 6802
datasheet from internet. The datasheet will show the instruction table for
converting the instruction into hex code. For hardware programming, you
can get details from the 6802 Kit User's Manual.
Programming the classic CPU is fun and is good to learn for young people.
2
Contents
3
Program 1 Load and Store instructions
How to store 8-bit data to memory?
# symbol makes the 8-bit data to be constant. The method of getting such
data is called Immediate addressing mode.
The 2nd instruction is STAA $00. The Accumulator contents will be stored to
MEMORY at location 00 in page zero. The addressing mode is Direct mode.
Procedure
1. Enter the hex code from location 200 to 203.
ACCA ____
4
3. Press PC to set current display to 200.
4. Press STEP key. Press REG, ACCA again. Write down the ACCA.
ACCA ____
5. Press REG, 6 to see the contents of memory location 00.
_______ 00
6. Press PC, then STEP. Press REG, 6
_______ 00
We see that the contents of memory location 00 will be replaced with 9F.
Now if we want to load and store 16-bit data, we can use IX register.
5
Index register, IX can be used to load 16-bit data. Above code will load 1234
to IX register.
STX $00 instruction will store IX to page zero location at 00 for high byte
and 01 for low byte.
Procedure
1. Enter the hex code from location 200 to 204.
_______ 00
We see that the 2nd instruction will store $1234 to location 00 (high byte)
and 01(low byte). But the machine code is only two bytes, DF 00.
If the location to be stored is not page zero. Let us see another sample
code.
6
0005 0206
0006 0206 .END
tasm: Number of errors = 0
We see that STX $0100 has three byte hex code FF, 01 and 00. The 6802 calls
EXTENDED addressing (16-bit address) mode.
Procedure
1. Enter the hex code from location 200 to 205.
2. Write down the contents of location 100 and 101 before and after code
execution.
Location(before) Location(after)
0100 0101 0100 0101
Summary
7
Program 2 Binary Addition
Suppose we have two 16-bit binary numbers.
Num1 and Num2. We want to add it. We can use two locations in page zero.
NUM1 = 12ED
NUM2 = 35EE
We can place two numbers in memory and use ADDA and ADCA instruction
to add them.
NUM1 NUM2
00 (MS) 01(LS) 02(MS) 03(LS)
12 ED 35 EE
We can enter Num1 and Num2 to memory with ADDR and DATA key using
HEX number.
8
0009 020A 97 00 STAA $00
0010 020C
0011 020C 3F SWI
0012 020D
0013 020D .END
tasm: Number of errors = 0
We will add the LS byte with ADDA instruction then followed with MS byte
by ADCA instruction. We put the SWI, (Software Interrupt) instruction at
the end of our program. It will make CPU to jump back to monitor program
with key GO.
Procedure
1. Enter the code from location 200 to 20C.
3. Add two numbers by hand calculation using binary number for both
numbers.
12ED + 35EE =?
4. Check the result with 6802 running with key GO. What is the result that
stored at location 00 and 01.? We can use key REG, $00 to see the memory
contents at 00 and 01.
9
Practice
Try changing both numbers and check result with hand calculation. The
example above can be applied to multiple bytes addition easily.
Summary
For multiple bytes addition, we must use ADCA, Add accumulator with
carry flag. If there is carry from lower significant byte.
10
Program 3 BCD Addition
BCD is the format that represents decimal number by using 4-bit binary
number. The 6802 addition instruction is for binary adding. If we use such
BCD to add it, the 6802 CPU has DAA instruction to provide automatic
adjustment of the binary addition result. The DAA must follow the ADDA or
ADCA instruction.
For example the clock display, 12:59:59 in BCD number. We may have three
bytes to store such display values.
11
Let us see the example of program that adds two BCD numbers.
NUM1 = 2894
NUM2 = 6547
Procedure
1. Enter the code from location 200 to 20D.
12
3. Add two numbers by hand calculation.
2894 + 6547 =?
4. Run the program with key PC then GO. Check the result with 6802
running at location of NUM1 (use key REG=$00).
Practice
Try changing both numbers and check result with hand calculation.
Remember, both numbers must be entered in BCD only 0-9.
Summary
DAA is used together with binary add instruction to adjust the result of
addition to be BCD number.
13
Program 4 Logical instructions
The 6802 performs logical operation with 8-bit data. Let us see the program
that produces the result of logical AND instruction.
The STAA $8000 will store the accumulator A to location $8000. This
memory address is used as the 8-bit output port. Each bit has the LED. We
can see the data result using LED ON/OFF.
We can now use key GO to jump from monitor program to our program
easily.
Procedure
1. Enter the code from location 200 to 207.
2. Let make the result of logical AND with hand computing again.
14
1E AND 89 =____________________________________________
3. Check the result on GPIO1 LED with key PC, then GO.
We see that the result of logical AND will be '1' if both bits are '1'.
Procedure
1. Enter the code from location 200 to 207.
9B OR 40 =____________________________________________
3. Check the result on GPIO1 LED with key PC, then GO.
15
We see that the result of logical OR will be '0' if both bits are '0'.
Procedure
1. Enter the code from location 200 to 207.
2. Let make the result of logical EOR with hand computing again.
00 EOR 80 =____________________________________________
3. Check the result on GPIO1 LED with key PC, then GO.
We see that the result of logical EOR will be '0' if both bits are the same.
16
Summary
The figure below shows the truth table for digital logic gates. Such logical
operation can be programmed by using the 6802 logical instructions as well.
More details on digital Gates can get from this web page.
17
Program 5 Bit rotation
We will use GPIO1 LED to make LED running with bit rotation instruction.
0001 0000
0002 0000 GPIO1 .EQU 8000H
0003 0000
0004 0200 .ORG 200H
0005 0200
0006 0200 86 01 MAIN LDAA #1
0007 0202
0008 0202 B7 80 00 LOOP STAA GPIO1
0009 0205 8D 03 BSR DELAY
0010 0207 49 ROLA
0011 0208 20 F8 BRA LOOP
0012 020A
0013 020A
0014 020A CE 10 00 DELAY LDX #1000H
0015 020D 09 DELAY1 DEX
0016 020E 26 FD BNE DELAY1
0017 0210 39 RTS
0018 0211
0019 0211 .END
tasm: Number of errors = 0
At line 0002 the symbol GPIO1 is set to 8000H using .EQU directive.
Now we see that the main program is repeat loop forever using instruction
BRA LOOP, Branch Always to LOOP.
18
The delay subroutine is just counting the IX register using DEX instruction
which will decrement the IX contents. BNE will jump back to DEX if IX is not
equal to zero by testing zero flag bit. If IX is zero, then return back to main
program.
Then jump to the loop location to store the contents of register A to the
GPIO1 LED again.
Procedure
3. Did you see the LED running? Can you make the running speed lower?
How?
4. Can you change the program to make the running in right-hand direction?
How?
Summary
Bit rotation program demonstrates loop program by using Branch
instruction. The delay subroutine shows the method of slow down LED
running. The 6802 has 16-bit register, IX. We can use it with 16-bit load value
and use conditional branch instruction BNE that checks the ZERO flag.
The GPIO1 LED is very useful and simple to use for displaying many
programs running. We will see it in many programs there after.
19
Program 6 Tone generation
We will see another sample program that uses delay method to produce
TONE signal.
TONE signal is a single frequency with 50% duty cycle. For our kit we can
produce tone signal in square waveform.
LS1 is small speaker. The driver circuit is BC337 PNP transistor, Q1. The signal
that drives Q1 is SPEAKER signal.
Logic '0' at SPEAKER signal will make Q1 turn on, and logic '1' for turn off.
20
Since we need only one bit to drive the speaker, the signal is shared with
TXD signal and it is located at bit 7 of PORT1.
And to make this bit set to turn off Q1. (Bit 6 must be '0' for normal running)
21
Let us see the program that produces TONE signal.
Again main program is forever loop running. The body of main program has
two portions. The first portion is to make SPEAKER signal CLEAR. And the
second portion is to turn off by making it SET.
The delay subroutine is the same as Program 5, but now the load value is
only 50H.
22
Procedure
Summary
A simple delay subroutine can be used to make the period of logic '1' and '0'
or square wave signal. To make higher frequency, the delay will be shorter
and lower frequency, longer delay.
23
Program 7 Reading key status
Reading key status will make the program get the logic status high or low.
We will test with REP key.
REP key is S19, when we press it, the logic at PA6 will be LOW, and will be
HIGH when it has been released.
U13 is 8-bit memory location. It was used as the input port, PORT0. The
location of PORT0 is $8001.
24
Let us read it and write it to GPIO1 LED.
0001 0000
0002 0000 GPIO1 .EQU 8000H
0003 0000 PORT0 .EQU 8001H
0004 0000
0005 0200 .ORG 200H
0006 0200
0007 0200 B6 80 01 MAIN LDAA PORT0 ; load A with PORT0
0008 0203 B7 80 00 STAA GPIO1 ; store A to GPIO1
0009 0206 20 F8 BRA MAIN ; jump back to MAIN
0010 0208
0011 0208 .END
tasm: Number of errors = 0
The main program is forever loop reading PORT0 and write it to GPIO1 LED.
Procedure
1. Enter the code from location 200 to 207.
25
Another sample program that reads key status of REP key then make the
binary counting at GPIO1 LED when it was pressed.
26
Procedure
1. Enter the code from location 200 to 220.
Summary
The CPU speed is rather high compare to the mechanical contact like key
switch. The bounce of contact will make unstable logic at the input bit.
Debounce with delay subroutine will help reading key status correctly.
27
Program 8 Interrupt with IRQ
Refer to the kit user manual, the table below shows the vector address for
interrupts.
We see that for IRQ, the location that put vector address is located at
FFF8 (MS) and FFF9 (LS)
Vector Description
MS LS
$FFF8 $FFF9 IRQ
$FFFA $FFFB Software Interrupt
$FFFC $FFFD NMI
$FFFE $FFFF RESET
We can check at the ROM location by pressing key ADDR then FFF8 and
FFF9. We found 00F8 in the zero page of RAM.
User now can test the interrupt by placing the code to be serviced at
location 00F8 in RAM.
IRQ is the maskable interrupt. To enable it, we must clear the I flag.
When IRQ pin is active LOW, the CPU registers will be saved to STACK
memory. The program counter will be loaded with vector that stored at
location FFF8 (MS byte) and FFF9 (LS byte). The kit puts RAM vector at
00F8.
28
Let us see a very simple code that responses IRQ signal.
We see that at the location 00F8, we place the JSR SERV_IRQ followed with
RTI, return from interrupt. The interrupt service subroutine must be
returned with RTI instruction!
In the main code, CLI will clear I flag, to enable IRQ then repeat jumping at
HERE.
The service subroutine for IRQ is located at 203. It will increment the
contents of location 0 then write it to GPIO1 LED.
29
Procedure
1. Enter the code from location 00F8 to 00FB.
Summary
IRQ is the hardware interrupt. It is the maskable interrupt. We enable it by
Clearing I flag. The real vector of IRQ is located in high address ROM. The
6802 kit provides RAM vector for user testing at location 00F8.
When we select SW1 to 10ms tick, the negative pulse that active low every
10ms will make the IRQ to be activated at 100Hz rate. We can see the
counting at GPIO1 LED at 100Hz rate.
30
Program 9 Using 10ms tick
We can use 10ms tick signal to trigger the IRQ interrupt pin. From 10ms or
100Hz interrupt, for example we can make a simple digital timer using 10ms
time base easily.
This program will show the counting of BCD number on the GPIO1 LED every
one second.
31
0026 0214 8B 01 ADDA #1
0027 0216 19 DAA
0028 0217 97 01 STAA SEC
0029 0219 B7 80 00 STAA GPIO1
0030 021C
0031 021C 39 SKIP RTS
0032 021D
0033 021D .END
tasm: Number of errors = 0
We have two bytes for storing two variables i.e. , SEC100 and SEC.
The service routine for 10ms tick is now updating the SEC100 variable, check
it if it is equal to 100 ticks, or one second, it will update the SEC variable.
We use CMPA, compare instruction to check the variable SEC100. The value
in the source code is 100 ticks, the hex number of 100 is 64 (see line 21).
For SEC variable, we will use BCD number, so we must have DAA to follow
the ADDA instruction.
We will see the BCD counting every one second now. It was done by timer
hardware that produces 10ms tick. For modern microcontroller, the
hardware timer is commonly available in the microcontroller chip. No need
separate chip. The 89C2051 microcontroller gives such 10ms tick signal. It
can reprogram for another tick rate for advance experiment.
Procedure
1. Enter the code for IRQ from location 00F8 to 00FB.
2.. Enter the code for main program from location 200 to 20B.
32
4. Press PC the GO, what is happening?
Summary
We see that the function that needs second timebase can be made easily
by using a given variable that incremented with multiple of 10ms tick.
33
Program 10 Digital timer
In Program 9, the counting is running with BCD number on the GPIO1 LED. In
this program , we will show how to use monitor subroutine to display digital
number in 7-segment display. The monitor listing is shown in the Kit User's
Manual.
34
0026 0213
0027 0213 7C 00 00 INC SEC100
0028 0216 96 00 LDAA SEC100
0029 0218 81 64 CMPA #100
0030 021A 26 10 BNE SKIP
0031 021C 7F 00 00 CLR SEC100
0032 021F
0033 021F 96 01 LDAA SEC
0034 0221 8B 01 ADDA #1
0035 0223 19 DAA
0036 0224 97 01 STAA SEC
0037 0226 B7 80 00 STAA GPIO1
0038 0229
0039 0229 BD C1 C3 JSR DATA_DISPLAY
0040 022C
0041 022C
0042 022C 32 SKIP PULA
0043 022D
0044 022D 39 RTS
0045 022E
0046 022E .END
tasm: Number of errors = 0
The 2nd subroutine SCAN1 will scan the display using 6-byte buffer memory.
The SEC variable that incremented every one second will be sent to the
DATA_DISPLAY subroutine.
Main program is repeat scanning the display with JSR SCAN1 instruction.
35
Relationship between 7-segment code and kit's display is shown below.
_______ 35
Procedure
1. Enter the code for IRQ from location 00F8 to 00FB.
2.. Enter the code for main program from location 200 to 22D.
36
Summary
Digital timer can be made using 10ms timebase. The example program
shows how to use the monitor calling for displaying the contents of
accumulator using kit's display.
Note
8.
A
F G B
E C
DP
D
Byte format D7 D6 D5 D4 D3 D2 D1 D0
Segment D DP C B A F G E
37
Decimal, 4-bit Binary and Hex
38
Hex and 7-segment pattern code
39
6802 Programming registers
40
6802 Microprocessor Kit
The 6802 kit is a workable digital computer. We can enter the 6802
instructions using machine code to the memory and test it directly.
41
Memory Allocation
The 6802 has 64kB, or 65536 bytes memory space. Our kit allocates space
for RAM, Input/Output and ROM as shown below.
00
80 PAGE ZERO
FF System RAM
0100
USER STACK
7F00
system STACK
7FFF
8000
Input/Output 16kB I/O ports
Port
BFFF
C000
FFFF
42
Page Zero is RAM having address from 00 to FF. The monitor program uses
location from 80 to FF for system operation. We can use location from 00
to 7F to be variables for our code.
We can access page zero memory with only 8-bit address. This location can
be called DIRECT address
User RAM is located from 100 to 7FFF. Here we can put our code to this
spaces. At the top location of this space is allocated for user STACK and
system STACK.
Monitor ROM is located from C000 to FFFF. This memory is ROM. We put
the monitor program in ROM. Our kit use EPROM, 27C256 to store the
monitor program. The monitor program will not loss if we turn power off.
After power on reset, the 6802 will run this program. The monitor program
will scan display and keypad providing hex code entering and test code
running. Our program will put to user RAM.
43
Note
44