by Tom Dickens
First published for the SRS Encoder, November 1997
I've finally decided to jump into the 68HC12 technology, the big brother of the 68HC11 microcontroller. I picked up a complete HC12 kit from Kevin Ross at the October 1997 SRS meeting. The kit included the HC12 board and components, including the 68HC812A4 CPU, and a BDM12 (Background Debugging Mode) circuit board and parts. The BDM12 is needed as an intelligent interface from your PC to the HC12.
I was successful in getting Kevin Ross' HC12 board talking, programmed, and running. There were a lot of hurdles to overcome to accomplish this; hopefully sharing my experiences with you will aid you in getting similar results with much less effort. In the coming months I will be covering more HC12 experiences, and I will be adding HC12 information under my HC11 web-site listed above.
The web-site for Kevin Ross:
Kevin's robotic products web-site, including the HC12 and BDM boards
The web-site for Karl Lunt (author of the AS12 assembler, one of many
Here is a running dialog I had with myself as I started down the 68HC12 path.
The two circuit boards solder up quite easily and the provided documentation is good. Here are just a few notes on them.
BDM12 Board: Pay attention to the placement of the 2 IC's; the MAX232 chip is up-side-down in relation to the writing on the board. R1, the resistor network, is placed with the DOT on the square pad. The 2 LEDs are placed with the green one closer to the (top) edge of the board, and they both have their anodes (the small part inside the LED) closest to the (right) edge of the board. Green means "activity" and red means "error". Two of the caps are 22pF and the rest are 0.1uF. The two 22pF caps (C1-C2, marked 220) are close to the crystal, the rest are the 0.1uF caps. Headers J1 and J4 are not used. Header J2 has a 4-pin Molex connector. The convention is to place the tang toward the inside of the board; on both this and the HC12 board. The 4-pin cable is wired straight through. Use the 4-pin Molex header with the 4 crimp-on contacts; I suggest you crimp with needle-nose pliers and then solder. The 4-switch DIP switch should be placed with the numbers near the (bottom) board edge, and the initial settings should be with switches 1, 3, and 4 off, and switch 2 on (OFF-ON-OFF-OFF).
68HC12A4 Board: Similar to the BDM board, there are two 22pF caps by the crystal. Header J5 is not used, and headers S0 and S1 are used for serial communications if desired, not for programming. J6 is the matching BDM header and should have the tang on the 4-pin Molex connector away from the edge of the board. Watch the polarity of C2. Soldering the CPU in place is not as difficult as it first appears. The main trick is: DO NOT USE SOLDER, but USE FLUX. Using a liquid or paste flux, wet all of the pads on the board. Place the CPU down and check to be sure it is aligned all around. Use a temperature controlled soldering iron around 630 degrees and tack the corners to secure the board. Like I mentioned, do not apply any solder. The board has been tinned, and this provides enough solder to flow onto the pins of the chip. Place to tip of the iron on the trace of the board about 1/8" to 1/16" away from the pin, and let the heat and the flux suck the solder from the trace to the pin. Once the corners are tacked down, check for proper alignment of the board before continuing. For the rest of the pins, two or three pins can be heated at once by the iron, depending on the size of your tip, letting the solder on the traces adhere to the pins.
Installing the BDM Software
[Note from Kevin Ross: It is not necessary to copy the files from the diskette to the hard drive, as Tom is doing below. Just run the install program from the diskette.]
The kit comes with a disk containing software for the BDM. Note: This software requires Windows 95 to install. I copied the files from the floppy to a directory on my hard disk, and it didn't run. I discovered that there was a hidden file on the floppy, _setup.dll. In the View menu pick of the directory window I selected Options, and under the View tab selected "Show all Files". _setup.dll was now visible on the floppy and I was able to copy it to the hard disk (there are probably better ways of doing this). I then double-clicked on Setup.exe and the BDM software was installed.
OK, I've got the two boards built, let's test them. Hmmm... With the HC11 I always run a quick test doing a binary count on Port B, what is the similar test on the HC12?
Luckily I have a copy of the necessary manuals, the 68HC12 CPU12 Reference Manual (document number CPU12RM/AD), and the MC68HC812A4 Technical Data Summary (document number MC68HC812A4TS/D, revision 0.2). I will refer to these manuals as RM and TDS respectively.
The first question I have is, "What operating mode does the processor come up in?" Table 9, page 24 of the TDS, shows the combination of three pins, BKGD, MODA, and MODB (pins 19, 49, and 50 respectively) determine the operating mode. On this board, they are wired to be V+, GND, and GND respectively, which, according to Table 9, puts us in Normal Single Chip mode.
The second question I have is, "Where are things placed in memory?" Figure 4, page 30 of the TDS shows the memory-map of the HC12A4. The key items are:
Note that the registers and the RAM can be mapped to any 2K address space, and the EEPROM can be mapped to any 4K address space, but these are the default locations and it is easiest to leave them there.
Table 7, pages 12-15 of the TDS detail the registers on the HC12. The HC12 has 244 registers compared to just 64 of the HC11. For general digital I/O, it looks like ports A, B, C, D and F are easily used. Ports H, and J are used for key wakeup (whatever that is). Port E contains the interrupt lines and other special functions (better leave this alone until I know more). Ports S and T are used for timing and serial communications. Port AD is eight A/D inputs. Some of the simple I/O registers and their locations in memory are:
OK, it looks like we've got 5x8 = 40 pins of general digital I/O. Where do these map to on the HC12 board? I don't see this mapping in the documentation, so here is my version of it:
The highlighted pins identify the ports A, B, C, D, and F. For ease of making am 8-LED test jig I will use Port D since there is power (Vdd) and ground (Vss) near-by... [half an hour at the electronics bench] ... OK, I've got the LED board build and tested. Now let's use it to test the HC12!
For the program, all I want to do is to place a binary count on Port D. Let's see, something like:
ORG $F000 ldaa #$ff staa $07 ; set the data-direction of Port D to outputs Top: inc $05 ; increment Port D bra Top ; Do it again
Notice that I don't care about the initial value of Port D, I just want to increment it.
In a DOS-Shell window, I used the EDIT editor to create this file (pasting the above code into EDIT was horrible--the relative indentation caused grief). I then saved the file and exited from EDIT.
To assemble the file I used Karl Lunt's AS12 assembler. I found this on Karl's web-site at:
There is a pick for the file as12rel.zip which I downloaded and unzipped. To use the assembler, I typed:
> AS12 countd.asm
This generated the following output:
as12 (v1.2) absolute assembler for the Motorola CPU12 Executed: Tue Oct 21 17:20:52 1997 F000 ORG $F000 ; F000 86 ff ldaa #$ff F002 5a 07 staa $07 ; set the data-direction of Port D to outputs Top: F004 72 00 05 inc $05 ; increment Port D F007 20 fb bra Top ; Do it again Total errors: 0 Total warnings: 0
The AS12 assembler also generated the file COUNTD.S19:
> type countd.s19 S014000046696C653A20636F756E74642E61736D0A0B S10CF00086FF5A0772000520FB8B S9030000FC
OK, Now let's download this puppy and see some lights blink. I double-click on BDM12, Kevin's loader program. This brings up a window in which you can set the COM port, the port's speed, and to ADD .S19 files, then DOWNLOAD the file(s). The default PORT setting is COM1, this is OK. The default SPEED is 9600. I assume that faster is better, but I check the BDM help to be sure. Opps, the context-sensitive help looks like is has the default verbiage, not the BDM specific help. The general help is better, but I don't see what I'm looking for. Hey, the BDM manual is sitting here on my desk, and sure enough, it talks me through this. The SPEED must be set according to the DIP-Switch setting on DIP-Switch 1. We have this OFF, which corresponds to 19200. Good! DIP-Switch 2 is the BDM mode, either "translator" or "debugger". Translator mode is used with the BDM program (the manual says), so switch 2 should be ON. Good! I select "Add S19 File" and a typical Windows file-picker box comes up. I locate the COUNTD.S19 file and select it. The file name now appears in the "source files to download (S19 files)" area in the window. So far so good, let's hit the "Download Now" button and see what happens... Well, the green LED blinks for about a second. I assume the code has been loaded. Now what? How do I run the code? I hit the RESET button; nothing. I unplug the BDM cable and hit RESET; nothing. Darn, I guess I need to read some more. The BDM document talks about using a terminal program to talk to the processor. Too bad, I don't have a terminal program; is there a "standard" one that is part of Windows 95? Probably not. I do have PCBUG11 and it has the "term" capability. I try this, reset the BDM in both 9600 and 19200 modes (DIP-switch 1). I do get the characters "EC returned to the terminal when the BDM is in 9600. Opps, I also need to switch the BDM into debug mode (DIP-switch 2). With the mode set to debugger and speed set to 9600 (DIP-switches 1 and 2 set to ON and OFF respectively), when I reset the BDM I get:
HC12 Pod v1.2
>[blinking cursor here]
Great!!! OK, now how does the debugger work? On page 7 of the BDM manual, step 10, says to type an s' and you should get a single-byte dump, either a 40 or C0. I do this and I get 00. I do this a dozen times and get 00 each time. Hmmm... Oh Oh, I just notice that my 4-wire BDM to HC12 cable has 2 wires crossed (green-red-white-blue on one end and green-red-blue-white on the other)! Opps. I switch the wires, plug things back together, reset the BDM, and hope the CPU and the BDM boards are OK. This time s' generates a value of FF. Fear and panic set in as FF is the error mode. I issue the Z' (capital zee) command to reset, then s' returns C0. OK, I feel a bit better. I dump the start of the EEPROM "d F000 F010" and I get "FF FF FF FF etc..." I assume that the cable error caused the program to not load. Let's get out of PCBUG11 and back into the BDM loader to re-load the program.
I bring up the BDM12 program, set the BDM switches to OFF-ON-OFF-OFF, reset the BDM and the HC12, and hit "Download Now". I get an error "Unable to open COM port". I save the BDM session, exit, and re-enter BDM12. Same error! I now close the DOS-Shell window I ran PCBUG11 from and re-enter BDM12. OK, the download now works (the DOS-Shell window, once it ran PCBUG11, must have retained ownership of the COM1 port. I just love PCs). Leaving BDM12 running, I open a new DOS-Shell window, enter PCBUG11, and enter its terminal mode. Oh yes, I also need to change the BDM switches to be ON-OFF-OFF-OFF. OK, reset both the BDM and the HC12. The Pod 1.2 message comes up. (I wonder of the BDM12 program can still talk to the BDM board, but I'll leave that as an exercise for the reader). s' generates FF. Z' and s' generates C0. D F000 F010 generates the assembler output of 86 FF 5A 07 72 00 05 20 FB. Great, the code is loaded!
Now, how to get it to run? Looking in the BDM manual I see the r and R commands. To see the registers I type r'. It returns
>r P:0180 D:00D8 X:0008 Y:7BA5 S:FFFB C:D8
Using R' I set the program counter to the start of EEPROM, $F000.
>R p F000 >r P:F000 D:00D8 X:0008 Y:7BA5 S:FFFB C:D8
Now to run, I type g' for GO. All of the LEDs light up. I know this sort of program works well on the HC11, but possibly on the HC12 it is running so fast that I can't even see the last few LEDs blinking.
I decide to poke around on my own for a bit to confirm the working of the LEDs. I reset the boards and do a dump of the registers (d 0000 0020), which gives me various values in return; a promising result. My program sets the data-direction of port D to outputs, then places values on Port D. Let's see, the data-direction register is $0007. I write an $FF here (w 0007 ff). All the LEDs come on! I write different values to Port D (w 0005 nn) and the value nn appears on the LEDs. Great, I'm actually controlling the LEDs!
Let's go back into the countd.asm program and add a delay so a mere human can see then blink (no, I don't have an O-Scope here at my desk otherwise I would look for pulses with it). Here is the new code:
ORG $F000 ldaa #$ff staa $07 ; set the data-direction of Port D to outputs Top: ldx #$0000 Delay: inx bne Delay inc $05 ; increment Port D bra Top ; Do it again
I assemble the new program (using AS12), load it in (using BDM12), bring up PCBUG11, go into terminal mode, set the PC to $F000 (R p F000), and run it (g). Wow, I have a binary count I can see on the LEDs. The most significant bit (MSB) is on for about 4 seconds, then off for about 4 seconds. This gives the LSB a frequency of about 1/16th of a second. My last task is to find out how to get the program to start by itself. The last 64 bytes of the EEPROM is a set of vectors (i just happened to have page 32, figure 5, of the TDS open) and there should be a RESET vector in there. Section 9.5.5, page 49 in the TDS, confirms this. Now to find this vector table... I find a short exception table in the other book (RM) on page 7-1, table 7-1. This shows the 16-bit vector at $FFFE-$FFFF as the reset vector. Looking in the TDS manual I find table 16, page 47, the complete interrupt vector table. This also shows the reset vector at $FFFE-$FFFF. I think we're on the right track.
Adding 2 lines to the countd.asm file I can load the reset vector with the start of the EEPROM. The new countd.asm file is now:
ORG $FFFE FDB F000 ; RESET Vector to the start of the EEPROM ORG $F000 ; Start of the EEPROM ldaa #$ff staa $07 ; set the data-direction of Port D to outputs Top: ldx #$0000 Delay: inx bne Delay inc $05 ; increment Port D bra Top ; Do it again
Assemble and load the program (change BDM switches, Load). Great, the AS12 program knows about the assembler directives from AS11 like, like FDB. Thanks Karl! I unplug the BDM cable from the HC12 board and hit the reset on the HC12 board. The LEDs count for about 2 seconds, then they all go out. Hmmm. Is there a watch-dog timer (COP) running or something? There is one on the HC11, but it is off by default; could it be on by default in the HC12? Looking at table 20, page 58 of the TDS, the default time for the COP Watchdog timer is 2.097 S; looks like this is it. The COP is reset in software by writing the sequence $55 $AA to register $0017. We could look into turning this feature (the COP) off, but it is a very good idea to use it. Let's modify our countd.asm program to reset the COP each time through the loop. The final version of our code is:
ORG $FFFE FDB $F000 ; RESET Vector to the start of the EEPROM ORG $F000 ; Start of the EEPROM ldaa #$ff staa $07 ; set the data-direction of Port D to outputs Top: ldd #$55AA staa $17 stab $17 ; reset the COP Watchdog timer ldx #$0000 Delay: inx bne Delay inc $05 ; increment Port D bra Top ; Do it again
Hitting reset on the HC12 board we now get a continuous binary count. Whew; this was a lot of work, but we learned a lot along the way. I think these are the key pieces to work with this system.
Next time I will detail my further experiences with the 68HC812A4.