Having fun with HC08 using pic microcontroller
Here is a nice little project for a rainy weekend, all this can be accomplished over a weekend and for under USD 15.
This page has gone through several rewrites, mainly to get rid of my ‘rant mode’ writing, but it seems I just need to write it out of myself. Feel free to skip the first part.
For references purposes and convenience I’ve put all the useful links here at the top of the page:
Interestingly, this list is always almost the same regardless of what I am contemplating to do with it, pretty much like my cooking really; first I take a pound of butter, bowl of sugar, a pint of cream , half a dozen eggs and then I start to think what to cook and eat! In the following, I’ll discuss these to give a beginner one point of view on the matter. Please note that there are many views on this, none, including mine, of which are necessarily absolutely correct.
In the following I’ll discuss my views on each of the above-listed requirements to some length.
Programming is at the very heart of microcontrollers – it is what makes them tick, and it is what makes them so universally applicable to various task. Those people at the silicon foundry who crank out these chips for us don’t know or care what we do with them. And they don’t have to, that is the beauty of it, because we as programmers put in the soul, the program, that dictates what it does. So the parts can be manufactured in millions, bringing the cost down.But those darn chips only understand and obey voltage levels in their memory cells and while it sometimes is necessary and strangely satisfying specifying those voltages with ones and zeros, it surely is not the way to go on a larger scale. So we need something higher level that allows us to think in more abstract terms than boolean logic and flip flops which are the building blocks of microcontrollers.
We need a programming language.
Now there is something about programming languages that makes people foam in the mouth. Don’t know what it is, but there surely is something fascinating about them and everyone seems to have very definite opinions about them. Me too.
Lots of the argumentation and discussion on programming languages seems to center around technical characteristics or philosophical issues about why one programming language is better than another. While there certainly are differences and one can put forward well-founded arguments for various aspects of different languages, I find that this sort discussion is centered around the wrong questions.
The question is not about which programming language is the best, but rather which one you should use and thus learn.
All the programming languages are pretty much the same. How could they be different, seeing that the computing architecture is always pretty much the same old Von Neumann and the problems are always the same, control flow, data representation, computation and storage management. Make a decision on those, throw in a syntax and, hey presto, we have a programming language! Of course, there are some differences and some languages are better than others, but the essential difficulties of programming are not likely to be solved with one programming language, any programming language, anytime soon! If they could be, I’m sure they would have been solved already – just look up the 2000+ programming languages at Wikipedia.
So how do you go about choosing a programming language?
To me it the choice is obvious: you select the most popular girl in town!.
I know the old joke about flies and cow pat, but when something is popular there has got to be something good about it. And there is safety in numbers, bringing me to my pet long term view theme.
When there is a large user base you get:
As often in life, it pays to take the middle road.
Yes, it is a vicious circle that ties us to design decisions done decades ago but there is no escaping the past if you have the future in mind. Now isn’t that ironic!
And the lucky winner is C
C has never been my favorite programming language yet both professionally and hobby-wise it is the only choice I can think of when talking about small microcontrollers. The tools and the language are mature and standardized, even the C99 standard is ten years old next year.
But hey, lots of people use BASIC or some improved C-like language to program microcontrollers, surely that makes sense?
BASIC has a special place in my heart, it was the first language I learned and it still has certain appeal in its simplicity and ease of learning. However, I would never consider using it for anything real.
Well, that is not quite true; only some weeks ago I was well on my way to write a BASIC compiler for HC08, I had the linker and assembler ready, and the compiler ticking, but fortunately I came to my senses before I wasted any more time on it.
Speaking of BASIC, what BASIC are we talking about? ANSI BASIC, or some proprietary product?
The original Dartmouth BASIC (born in -64 like me) was a very simple language that contained all the basic building blocks that you need in a programming language, but lacked the basic higher level abstraction features that I think are essential, like named subroutines, local variables and parameter passing, and structural control statements, without which programming is rather similar to assembler programming, making it difficult to manage anything complex successfully. Of course, later variants of BASIC added these features, only so many of them went their own way, so they are all the same, only different!
With C the people who can (?), the language and tools will be here in twenty years time when I or or someone else needs to get back and tweak the software. Imagine getting an old BASIC program to play ball with in ten years time. And this need to tweak is not an ‘if’, it’s a ‘when’!
Talking about proprietary products versus standard languages: while you can learn to program in a few weeks and learn a new language, any language, in days, it takes years to master and learn the nitty gritty details where the evils lurk, so to me it does not make sense to invest in learning something that may not exist tomorrow. Companies come and go, they are bought, sold and merged, products discontinued and killed off. So, personally, I would never invest programming in a language that does not have an exceptionally high likelihood of survival.
A case in point, Microsoft’s Quick BASIC for Mac was discontinued, killing a successful product that I was personally involved in. The same happened to a friend on the PC version.
I go as far as making the language and tools affect the choice of the chip. If a chip is not supported by Gnu C it is a definite minus point. If it can’t be programmed in C it is a non starter. No chip is so great that it makes sense to invest on re-learning a new programming language when the competition is bound to offer something that can be programmed with C.
Before I close up this rather long and most likely boring section on programming languages, I can’t help mentioning the project Arduino. Everyone interested in getting into microcontrollers should check it out. Although I think the idea of having yet another IDE and adding some sort of automatic preprocessor on top C does not really make sense to me, the project has a lot to offer.
In professional circles, surface mount devices are not only a fact of life but a desirable characteristic. However, for the hobbyist they are a real pain in the hindquarters. Not only do you need a printed circuit board, then soldering them is also no piece of cake. It can be done in a home workshop – I’ve done it myself, but I’ve also been forced to outsource some SMD soldering on one of my hobby boards! And it is not always easy for the professionals either; only last week I witnessed two failed professional attempts to solder a QFP package to a prototype.To your left you have two MC68HC908 chips with a 20 SNT coin and a 1/4 W resistor for size reference. The left one is a GZ16 in a 32 pin LQFP package and the one on the right is JB8 in a 20 pin PDIP package. Click on the image for a larger view.
You can solder some SMD packages on a modified oven or you can do what I’ve done, which is soldering all the pins into a solid mass that I’ve then cleaned up with a solder wick.
But soldering is not the biggest problem. To me, the biggest problem is that you need a PCB.
It takes time to design and manufacture a PCB and because of that it makes sense to incorporate all of the circuitry on the board, including those you could’ve just Vero-boarded. So it takes even longer, and before you know it your enthusiasm is gone and the project just becomes one of those ‘I’ll finish it one day” -projects. Here is one project that for the time being has run out of steam, tucked away shamefully into a faraway corner of my hard drive.
What I want is something that can be whacked together in matter of hours or a few days on a prototyping board.
True, there are ready-made adapter boards for most SMD packages and one of these days I’ll get some, and although money is no concern, per se, in a hobby, it goes against the grain to use 10 USD to mount a 2 USD chip, and still having to breadboard all the rest.
Thus, one of the key requirements is DIL packaging. USB Support I seem to go through PC hardware, meaning mostly laptops, at an alarming rate. Both privately and professionally. And coming from not-too-wealthy circumstances it pains me to see completely functional stuff end up at a landfill. So I dream of using them for something. This used to be easy with parallel ports and serial communication, but in recent years the computers, especially laptops, often come without any other ports than USB. So the days of parallel and serial ports are numbered.
So what’s this got to do with microcontrollers? Well, so many of my ideas would be best implemented so that the low level real time stuff is done on the microcontroller but the rest, like networking, storage and user interface, on a proper computer. Yes, I’ve seen weather stations that boast a web server on a match box size PCB but for me using that old laptop in the corner to record and control my wind turbine parameters would make much more sense, except that it can’t do any real time stuff with Windows on it and I can’t connect anything to it because it has no serial port.
Surely you can add a USB -serial port and many times that is the answer, but what about if my cool idea is turned into a product someday? A USB serial port burdens the product with 20 USD for starters and incurs complexity and still limits the connectivity to what a serial port can offer. Came to think of it, BasicSTAMP would set it back even more.
So taking the long term view I think it makes sense for me to bite the bullet and learn to ‘do’ USB.
Like I said, I’ve had great fun with HC11 but unless you can cram your code into 512 bytes you need to use external ROM memory for the program storage. It also requires at the very minimum an external reset circuitry and a clock oscillator or a crystal. This is not today’s technology, I want something that has all in one chip. Not only will it help keeping the costs down but it also reduces the complexity.
Long gone are the days when we erased EPROMs with UV-light and ‘burned’ them in a prom-programmer, and that is good. Today’s microcontrollers, especially in a hobby project, should be programmable in-circuit. And without an expensive in-circuit programming adapter, too. I can dish out 300 USD for a evaluation kit that includes the adapter, but I don’t want to! I prefer something simple that can be hooked to a serial port more or less directly. Or better yet, since I wanted to have USB anyway, it would be ideal to program it through the USB connection as well. I guess this is the long term view again: while I can spend the money on professional gear, what if my efforts are something that some other hobbyist wants to try too? Their situation maybe completely different, think of students and schools for example.Besides doing it from the ground up just is the way I do things!
Let me state that I’m not a Free Software advocate but at the same time I see that there is a lot of sense in it and some of it is actually darn good.Many chip vendors and tool vendors provide free (as in free beer) versions of their tools and compilers. Which is great and fine, especially as some of them are better, in some measures, that their Free (as in freedom) counter parts. However, they are in it for the money, which means that there has got to be a catch somewhere, like you can use only 32 files and 1 kB code per file and max 4 kB code. Of course, market pressure and strategies seem to force the tool vendors to offer more and more free stuff and relax the crippling (=ei ole sana?) to lure customers but at the end of the day commercial tools are at the mercy of tool vendors’ business decisions. Companies get sold and merged, whole product lines get sidelined or discontinued, in any case improvements will require cash layout at some point.
So, for my hobby projects I want to use something that I hope will be there tomorrow at an affordable price.
Taking the long term view, if at all possible within the other constraints, I want to use gcc, the Gnu C-compiler, that is bound to be there for the long term. The One Tool to Rule Them All.
Having said that, it seems that for 8-bit microcontrollers, which have some rather simple or downright weird architectures, gcc is not likely to be available. It used to support things like 8051 but that has been effectively dropped for the time being. And it may be that this is the way it is going to stay as there may be a fundamental conflict between the small microcontroller and the multitude of languages the compiler supports and the kind of more powerful CPU architecture it is designed for.
Fortunately there is the microcontroller world that is the equivalent of gcc, the SDCC – Small Device C Compiler. Which just shows you the power of using standards and C – my favourite tool was not usable but I found another one!
Used by the Professionals
Maybe I’m biased, but I would not like to use something that is inferior to what the professionals use. This is not snobism, it is just that I expect the pros to have done their homework when selecting their tools and chips. And, more importantly, it is their choices, not the hobby market, that will keep the tools and chips alive.Interestingly, it is the consumer market that in turn drives the professional markets! We are all hobbyist when it comes to snapping photos with our IXUSes and it’s the development of the consumer gear which definitely drives the chip development.
Now, I know the following is going to sound stupid and stubborn, especially coming from a guy who runs Windows XP and three Linuxes under Parallels on Mac OS, but I can’t help it: I want to do my embedded development under Mac OS X. I know, it limits your options, it is more work, it makes no sense, but its great fun.
Integrated Development Enviroment
I’m not a great fan of Integrated Development Enviroments (IDEs) – a good editor, C-compiler and a Makefile leaves little room for improvement. Now, let me qualify that I’m not big fan of proprietary vendor specific IDEs such as CodeWarrior or VisualDSP. They are all the same, only different, that is the problem. They all have more or less the same features, but implement them slightly differently making me re-learn all that stuff. And when you see one vendor doing a better job in one area it makes you really frustrated to use the poor imitation, until you realize that the better one does a poorer job on some other stuff. That’s when I really get frustrated.When I use an IDE I want to use the same IDE for everything, not change it as I juggle between projects, or as the case may be, when the IDE company is busted or gets sold. Ask any CodeWright user if you want to hear more rant in this same vein. While many of the proprietary IDEs, especially for embedded markets, are decent, I’ve found none that I would call good. A lot of things are poorly executed, as if the developers themselves never used them for real work.
There is one IDE I actually like, and that is Eclipse. Incidentally, it is now Free Software although it has its roots in the IBM VisualAge for Java. It gets so many things so right that I sometimes expect there is a mind reader somewhere. The code completion and re-factoring tools are awesome, the ergonomics of the user interface leaves little to be desired, the search and code navigation tools are great and work like a breeze. It has a large user space and is available for all major platforms and support many languages. Vibrant and kicking.
And with the CDT plugin it is now a viable C/C++ environment. True, the re-factoring, build and search tools for C/C++ are not yet quite as good as for Java, but it keeps getting better and is already my favorite for C programming.
Of course, nothing is perfect and neither is Eclipse, but I like it and it seems to be destined for a great future, which is all-important.
Cost as such is not a big issue, for one-off project it makes a little difference if a chip costs 2 USD or 40 USD, but then there there is the co-later damage, investment in learning and using what you’ve learned.
If you learn to use a 40 USD part, you are likely to use a similar part again and again and if you invent the next big mouse trap the 40 USD part may make or brake the project. And needing to re-learn something as complex a new microcontroller is sure to put the brakes on for some time, at least for the hobbyist.
Which Microcontroller – Finally getting to it
At first it would seem that there is the proverbial embarras de richesses but given my constraints above it actually boils down to a few choices. Of course, I do not claim to have a complete picture of the ever-changing microcontroller landscape and may have missed new entrants, especially as I started this ‘project’ some years ago. Which sort of proves my point about requiring something that can be whacked together in one weekend and has a future!By now you should see, even if you may not agree, why BASIC Stamp and PICAXE were not even running in this competion.
My short list of candidates for a microcontroller was:
Microchip PIC18F2455/2550/4455/4550 family
Microchip seems to be very popular among hobbyist, but I haven’t very often seen it in professional use. I secretly suspect that the major reason for their success is that they offer so many of their devices in plastic DIL packages!The older PIC families had definitely weird and unfriendly architecture that made them unsuitable for other than assembly language programming or using their special version of BASIC. The later 18F series seems to be more main stream and SDCC supports both the 16 and 18 series of PICs.
PIC18F2550 supports USB, comes in a hobby-friendly 28 PDIP package (or even 40 pin!), requires an external crystal and has 32 kB of Flash 2048 bytes of RAM.
Unfortunately, this chip was not actually available when I made my decision and even samples were impossible to get, otherwise you might be reading my web page titled “Having fun with PIC18F2550”. I have actually put in an order for some samples, so maybe I’ll be reporting back on it in a year or two.
Since I’ve not used it I cannot say what it takes to program it in-circuit as I want to do, but surfing the web you see a lot of options software-wise.
Silicon Labs C8051F3XX family
Silion Labs has some very innovative chips and I actually have two chips on my table as I write this. The C8051F3XX family is based on the well-known and widely supported 8051 instruction set and besides USB it has 16 kB Flash and 1024 bytes of RAM and requires no, none, zero external components, not even a crystal. Even if Micochip datasheet explains that it is impossible to ‘do’ USB without a crystal oscillator, it seems that Silicon Labs can do it. Now that is what I call an integrated solution!Talking about crystal oscillators, while a crystal and the two caps plus resistor required by most chips are no big deal they can cost more than the microcontroller!
Unfortunately, it is not available in a DIL or PDIP package which means that I need to get an adapter board or design a PCB. No wonder those chips have been on the shelf for two years now.
Also, in-circuit programming via a C2 one wire interface sort of put me off because you cannot directly generate from a serial or parallel port the sort of signaling it requires.
By chance Motorola (now Freescale) MC6800 and its siblings and descendants were one of the first microcontrollers I learned and the 68HC08 family is a sort of upgraded/downgraded descendant of it. The evolution of the 68XX family would make for an interesting pondering but I won’t do it here, suffice to say that the 68HC11 that once looked like the future is now considered an evolutionary dead-end mutation.68HC908JB is available in a 18 pin PDIP package, which limits the number of I/O available and it does require an external crystal, but it supports USB and has 8 kB Flash and 256 bytes of RAM.
In-circuit programming is possible with the one wire serial communication based monitor mode which is easy to implement with standard serial port on any PC.
MC68HC908JB8 is also the cheapest of the lot at around 1.5 USD in quantities of one, the C8051F320 sell for around 6.5 USD and the PIC18F2550 is sold around 4.50 USD, not that it makes much of a difference.
For serious professional work the Silicon Labs chip seem to provide best bang for buck in terms of being the industry standard core with minimal external circuitry, fast clock speeds and more than adequate memory sizes. For the hobbyist the PIC18 family seems very attractive as does the Atmel ATmega48 family which is available in the hobby-friendly 28 PDIP package.
Worth noting is also Atmel AT90USB family with its 64/128 kB Flash memory, 2/4 kB EEPROM, 4/8 kB RAM , high performance RISC architecture with USB 2.0 support and really attractive feature: USB bootloading. Sadly, it does not come in a suitable package.
After saying that I think that PIC18F family seems ideal for a hobby project I guess it now seems logical that this page is about HC908JB!
BTW There is logic according to which Freescale name their parts. Take for example MC68HC908JB8, the MC is what Motorola has used for ages for designating their parts, the 68HC indicates that this is a descendant of the original 6800 microprocessor but in high speed CMOS, 9 indicates that this is a device with internal Flash ROM and 08 stands for cpu architecture variant as in 6808, JB is the family of USB oriented devices and 8 is the amout of Flash memory in the device in kilobytes. When ordering be careful the get the right part as this is the only variant available in PDIP package. Note that nowadays this is a lead-free product and there is a discontinued product that is just as good but impossible to get, so don’t be put off if a part is discontinued, there is usually a replacement.
Building the Hardware
To start having fun with HC08 you need just two micro chips, crystals and a dozen or so other components. The hardware setup presented here is very much like many others out there and not unlike those on the Application Notes for HC908. I’m mainly repeating the information here for my own reference, there is nothing special about this setup, but if you are new to this the notes may help you get going, after all the datasheet is almost 300 pages long, with a half a dozen application notes thrown in!The hardware is really rather simple, anyone should be able to solder it together on a Veroboard in one or two evenings. This may not be the ideal very first project in electronics as you need to be able to read schematics and hold the soldering iron from the cool end. That said, this is pretty forgiving and easy to get to work, unlike some of the analog transistor stuff I attempted and failed with 30 years ago. That’s because digital is simple, just two voltage levels and large tolerances.
The parts should not set you back more than 15 USD altogether. Some, like the not-so-easy-to-get USB Device connector can be ripped from an old PC. Speaking of the USB connector, a good solution is not to have a Device connector at all but to take a regular USB cable, cut away the Device end and solder the wires directly on to the Veroboard.
The bootload adapter converts the voltage levels of your serial port to match those required by the microcontroller and allows you to download software into the microcontroller as well as provides a handy way to debug and experiment with your system. It also provides the ‘high’ voltage that is required to force the microcontroller in the Monitor Mode in which the downloading can take place.
The bootload adapter is just a MAX232A and a diode that performs a logical OR function to combine the receive (RX) and transmit (TX) signals. This turns the serial communication into a party line where either end can talk to the other end but only in turns. The MAX232A is made for the very purpose of converting from and to the serial line -12/+12 Volt levels to the CMOS 0-5 V level logic. For this purpose it contains charge pumps that magically turn the +5 Volt feed into the -/+ 10 Volt required.
The HC908 requires that a ‘high’ voltage of about 8 Volts is presented in the *IRQ pin to force it into the Monitor Mode upon reset. The circuit presented here takes advantage of the MAX232A charge pumps to steal that voltage from there.
The adapter is re-usable in the sense that it is no longer required in this project once the project finished so you may want to construct it separately for easy re-use. To your left you cas see a picture of my adapter.
Below you’ll see the Vero-board layout I drafted for it (click the images for larger view). I usually don’t go to the trouble of drawing a layout but I wanted to make sure I did not run out of space as I wanted this to fit into my nice box.
The microcontroller I’ve used is pretty much self-contained. To function it needs just a few bypass capacitors on the power supply lines, an external crystal for the oscillator and one or two pull up/down resistors to configure the Monitor Mode entry.
It is also nice to have an LED so that there is something to experiment with as soon as the hardware is done. I also always include a LED that tells me that the power is on, which in this design is the red LED on the adapter. This LED also blinks as the serial communication takes place, which is both useful and comforting.
Below are the schematics for the microcontroller. I haven’t included any actual peripherals at this stage to keep the schematics simple, but you are sure to find plenty of online examples on how to connect LCD displays, stepper motors, relays or whatever you fancy to put there.
Setting up the Software Tool Chain
For the software you need a C-compiler to compile your programs into machine code and a bootloader to download and ‘burn’ the machine code into the microcontroller’s internal Flash memory. For anything larger than a single file test program I highly recommend using make to automate the process. You also need a text editor; any editor that can write plain text files will do, but I’ll show you how to set up a really streamlined development process and professional Integrated Development Enviroment with Eclipse.
And all this is free in every sense of the word.
For the compiler we use SDCC which you can download from their web site at http://sdcc.sourceforge.net . In addition to the source code, pre compiled binaries are available for all the major platforms, so there is no need to compile it from scratch. (I need to praise the SDCC project for making a good job on this, because building it from the source, when I tried it, went according to the book, something that does not often happen with your average Open Source project.)
So download the distribution (see the Snapshot section), unpack it and put it where you like, for this tutorial I use my own set up as an example and I keep SDCC in my home directory under the name
'sdcc'. The full path on my Mac is then
'/Users/nyholku/sdcc' and the actual compiler is at
For more detail: Having fun with HC08
Current Project / Post can also be found using:
- 8051 microcontroller simple game projects
- what is the work of microcontroller in games
- car game using pic microcontroller
- mc68hc908 datasheet pdf