Programmingbook 2
Programmingbook 2
1
Contents
2
Lab 1 Load and Store instruction
Line addr hex code Label Instruction
The accumulator is loaded with 8-bit data, AA. Then write it to gpio1 LED at location
8000. SWI is software interrupt. It makes CPU to return to monitor and save CPU
registers to user registers.
Enter the hex code to memory and run it with key GO.
What is happening?
Test run with many hex byte and see the display on GPIO1 LED. It shows in
BINARY number!
3
Lab 2 Store byte to memory
LINE ADDR HEX CODE LABEL INSTRUCTION
The accumulator is loaded with 8-bit data 00. TFR A,DP transfers the content of
accumulator to DP register. This Direct Page register is used to access the zero page
location.
The accumulator is loaded with 8-bit data, AA. Then write it to memory at location
8000, 40 and 6000.
At line 8, we write it to page zero address, at 40. This is Direct addressing mode. The
location to be accessed will be xx40. xx is DP register. Hex code has only two bytes,
97, 40.
Before test this program, check the contents and write down.
Enter the hex code to memory and run it with key GO.
What is happening?
4
Location Before After
40
6000
5
Lab 3 Store byte to memory using Index register
The accumulator is loaded with 8-bit data, AA. Then write it to memory at location
pointed to using Index addressing mode with X register.
6
Compute the effective address for each instructions, write down.
Enter the code and write down the memory contents before and after running.
7
Lab 4 Using register for offset byte
The accumulator is loaded with 8-bit data, 05. Then write it to memory pointed to
using Indexed addressing mode. Now, the contents of register B is used as the offset
byte.
Write down the effective address and its contents before and after.
Enter the hex code to memory and run it with key GO.
8
Lab 5 Indirection addressing mode
The accumulator is loaded with 8-bit data pointed to using LOCATION that stored in
address C000. Then write it to GPIO1 LED.
Enter the hex code to memory and run it with key GO.
What is happening?
What is happening?
9
Lab 6 Branch and Long Branch
6809 uses relative addressing for branch and long branch instructions.
10
OFFSET = destination – current PC.
Using CAL key is easy. Press CAL, enter destination, then key – then current PC, the
GO.
Try compute the 16-bit offset byte for long branch instruction at line 14.
11
Lab 7 Simple delay using X register
Simple delay using register counting down is a common useful subroutine for
program testing.
We use X register loaded with initial 16-bit value. Decrements it, until ZERO flag set.
Main code is a loop running with bit rotation. We can see the bit rotation on GPIO1
LED easily.
Can you compute the OFFSET byte of this instruction? How F8 comes? Why?
12
Enter the hex code and test run.
What is happening?
13
Lab 8 Testing IRQ interrupt with 10ms tick
Instead of making the delay by counting the X or Y register, we can use interrupt with
10ms tick generator.
The 6809 IRQ vector is stored in monitor ROM at location FFF8, FFF9
The monitor program of the 6809 kit has relocated the IRQ vector to RAM location at
7FF0. When triggered by IRQ the CPU will jump to location 7FF0.
14
The program starts with setting DP register to 0 for direct addressing within zero
page.
Then inserts the JUMP instruction code, 7E to location 7FF0 and the address of
service routine 6000 to 7FF1, 7FF2.
The service routine is located at 6000. The zero page byte 0 will be counting. When it
reaches 100 or 1000ms, clear it to zero. The zero page byte 1 will be incremented and
sent to gpio1 LED. We will see binary counting at 1s or 1Hz rate.
Can you change the counting rate from 1Hz to 10Hz? How?
15
Lab 9 Running code using 10ms tick
0001 * RUNNING CODE USING 10ms TICK
0002
0003 700E TICK EQU $700E
0004
0005 0200 ORG $200
0006
0007 0200 86 00 MAIN LDA #0
0008 0202 1F 8B TFR A,DP SET PAGE 0
0009 0204 3C EF CWAI #%11101111 ENABLE IRQ
0010
0011 * BELOW CODE 10mS
0012
0013 0206 B6 70 0E LOOP LDA TICK
0014 0209 81 64 CMPA #100
0015 020B 26 0D BNE SKIP
0016 020D 7F 70 0E CLR TICK
0017
0018 * BELOW CODE 1000mS
0019
0020 0210 96 00 LDA 0
0021 0212 8B 01 ADDA #1
0022 0214 19 DAA
0023 0215 97 00 STA 0
0024 0217 B7 80 00 STA $8000
0025
0026 021A 20 EA SKIP BRA LOOP
0027
0028 END
Variable tick is 8-bit memory location at 700E. By default, the monitor program
prepares the service routine for IRQ after CPU was RESET.
If we enable the IRQ flag, I, the CPU will enter IRQ service routine every 10ms (with
SW1 set to 10ms position). The service routine will increment tick variable every
10ms.
Above code demonstrates how to read the tick variable to provide 10ms and 1000ms
time slot for a given task running.
16
Lab 10 Calling monitor c function
The monitor program listing provides symbol for reference. We can call the function
in c written directly.
For c function without parameter passing, we can use JSR instruction and the location
of that function directly.
The example one is jsr init_lcd, jump to subroutine to initialize the LCD module.
For the function that needs char size input parameter, register B will be used to pass
value. Passing is done by using system stack. We see that, line 12 push register d to
system stack. Then jump to subroutine putch_lcd(). When completed, the original
stack will be restored with leas 2,s instruction.
Enter the code, test run it. What is happening on the LCD display?
17
Lab 11 Display message on LCD display
We can use monitor function that displays message on the LCD, pstring function.
The input parameter is location of message. Register d is loaded with the address of
text1, 020E.
Then push it on system stack, jump to subroutine pstring, then restore the original
location of the system stack with leas 2, s instruction.
Enter the code, test run it. What is happening on the LCD display?
18
Lab 12 Display message on LCD two lines
19
63 6F 64 65 20 69
6E 20 48 45 58
If we use 20x2 line LCD, we can display two lines by using function goto_xy(a,b)
easily.
For two parameters, a and b are for position x,y, again we push twice sending both
parameters. After that restore original location of system stack with leas 4,s
instruction.
Enter the code, test run it. What is happening on the LCD display?
20
Lab 13 Sending ASCII character to terminal
This lab will need RS232 terminal. We can use PC running terminal emulator, says
VT100 for the test.
Serial wiring between 6809 kit and RS232 terminal is cross cable. Therminal speed is
19,200 bit/s.
21
Lab 14 Sending message to terminal
To display message, we can use monitor function puts() at location E041. By loading
the start address of the message to register d. Then put it to system stack, call the
function.
Enter the code, test run it.
What is happening on the terminal display?
22
Lab 15 Sending message to terminal every one second
The monitor function wait1s() located at E750 uses 10ms tick for counting 100 times
to exit. We can use it for slow down the output display.
23
Function newline() will enter the new line control character.
Can you change the interval from one second to three seconds? How?
24