Spare Time Labs 2.0 | |||||||||||||||||||||||||||||||||||||||||||||||
![]() Welcome ![]() EazyCNC ![]() jDraft 2.0 ![]() ![]() ![]() PureJavaComm ![]() PIC CDC ACM ![]() Weather ![]() Ten-Buck Furnace ![]() H8S Bootloader ![]() Camera Calibration ![]() Multitouch ![]() Myford VFD ![]() Fun with HC08 ![]() bl08 ![]() printf II ![]() Java Goes Native ![]() Densitometer ![]() printf ![]() jApp ![]() Igloo ![]() New Furnace ![]() New Furnace Part II ![]() Linux 101 ![]() H8S/gcc ![]() Quickie ![]() Gas Fired Furnace ![]() Down Memory Lane ![]() Exlibris ![]() Wheel Patterns ![]() Glitches ![]() CHIP-8 ![]() eDice ![]() Animato ![]() jDraft ![]() JNA Benchmark ![]() Contact Info |
Created 16.3.2008
![]() Having fun with HC08Here 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:
Table of ContentsEven though this is a rather long page it has a kind of 'linear' story to it and I've meant it to be read straight through. Having said that I realize that a table contents is handy, especially if later on you want to come back and look for something. Besides it allows you to skip the rant part and get down to the brass task, starting at 'Building the Hardware'.
Introduction IntroductionThis page is about getting started with having fun with microcontrollers, particularly with the Freescale MC68HC908 family of controllers.This page is aimed at beginners in microcontrollers. While a microcontroller project may not be suitable as a first project in electronics I hope to show that it is not that difficult either. The controller here can be put together and the development environment set up in a few evenings and for less than 15 USD, if you have the basic infrastructure, a personal computer and tools, in place. Lots of people are interested in microcontrollers, as witnessed by the countless number of hobby pages in the net. If you are new to this hobby it might make sense to pause for a moment and think about why you want to get involved. Motivation always concerns me a lot, in more than one sense of the word. While I'm not always forthcoming with my own motivations, at least I try to be honest with myself. A lot of people vaguely cite that they want to learn to 'do microcontrollers' or 'learn a new programming language'. These are as good reasons as any, but I suspect that behind these statements there are dreams or ambitions that aim further. Search within yourself and find out what makes you tick. So before I get to the actual technical substance, let me do some soul searching here with you, maybe it will provoke some thoughts that will help you to make up your mind.
Why microcontrollersI spend about two hours every day commuting and no sooner than I've started the car my thoughts start to wonder and I get these ideas. Hey, it would be cool to monitor the electric consumption of the house, just measure the pulse train on the meter, or how about creating that CNC rooter I've always dreamed of and I could build an induction furnace or how about a levitation device... What is common to these pipe dreams is that most of these would be best implemented with a microcontroller. A microcontroller is a sort mini computer with a central processing unit or CPU, some memory and some peripherals or input output devices. Let's face it, analog circuits and logic chips are just not the way to go today. A microcontroller can create pulses and frequencies, measure signals, do math, do logic and control them in a wonderfully precise and repeatable way. No tolerances to worry about, no trimming to do, no need for special measuring devices. Everything is going digital and that is the way it should be. It just makes sense. "Anything you can do I can do better" is the attitude of digital processing toward analog circuits.
Taking the long term viewI'm a professional programmer involved in designing embedded systems on a daily basis for the past quarter of a century, almost as long as they've existed. I also enjoy tinkering with these things in my spare time. Now how twisted is that! With this schizophrenic mind set I cannot help bringing some of my professional baggage to my spare time activities although the motivations, goals, requirements and resources of professionals and hobbyist differ considerably. Learning new stuff, like microcontrollers, is fun, but it's also an investment. An investment in yourself and your future. Like any investment it requires spending one thing to gain another. Time and money. Money is a secondary consideration; anyone can be rich, but no-one lives forever so we all must carefully consider what we do with our time. As with any investment, it is natural to expect from it something in return, be it in the form of skills, opportunities, gratification or just sheer fun. So what has all this got to do with microcontrollers? Learning to 'do' microcontrollers is a big investment in terms of spending precious spare time. Nobody wants to see their investement go bust so it pays to invest carefully. Concerns include, "what if the chip I'm using gets discontinued," "what if the tools I'm using get discontinued," "am I going to be left in a niche where other users are few and far between." This may sound a bit pompous, talking like that in the context of a hobby project, but as one lives one learns, and I've seen so many one-off temporary projects, both hardware but especially software, to live well beyond the wildest expectations of their creators. I'll bet you anything that the guys writing Unix code more than fourty years ago had no idea that a lot of this code would still be running and tooted as the most advanced operating system available today: Mac OS X! I've found playing with microcontrollers both rewarding and fun, and it has even paid my mortage, but surely it also has swallowed a big chunk of my life.
Ok, so let's see where this sort of thinking has lead me and why. I'm not putting this forward as the only right way of going about it, I'm just sharing my point of view. Corrections and revisions of my opinions are encouraged. If you're still reading, remember, that it sometimes pays off to listen to others who've 'been-there-done-that', but you should also study yourself and find out what it is that you really want to do. It is your project and your life.
Looking for a microcontrollerFor years I've been looking for a suitable microcontroller that would fulfil my self-imposed constraints. In the past, me and my friends have had great fun building things from the 68HC11 family of devices, but, although a very nice device, it leaves something to be desired when compared to more modern offerings with internal Flash and whatnot. Here is my list requirements for a microcontroller:
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.
C-programmableProgramming 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 CC 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.
DIL package![]() 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.
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.
Single ChipLike 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.In-circuit ProgrammingLong 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!
Free ToolsLet 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 ProfessionalsMaybe 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. Mac EvangelismNow, 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 EnviromentI'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 IssuesCost 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 itAt 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 familyMicrochip 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 familySilion 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. MC68HC908JB familyBy 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 HardwareTo 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 hardware consists of basically two parts: a serial bootload adapter and the actual microcontroller, see the illustration below.
There is no need for a power supply because for up to 100 mA we can draw the power from the USB port and for this set up we'll be using well below 30 mA here. The Bootload AdapterThe 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. Below are the schematics for the bootload adapter. For complete, printable schematics scroll to the bottom.
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.
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 MicrocontrollerThe 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. Note that I've not yet personally implemented/tested the USB interface part, but from documentation it appears that you should not need the pull up , nor should you need the serial resistors shown on my schematics.
I constructed mine on a Vero-board, see below, in about two hours with just the schematics as a reference. If you are inexperienced it might make sense to draft a layout plan first.
Setting up the Software Tool ChainFor 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
Scary stuff - the command lineNext we will venture to the dreaded world of the shell / comman line interface, but don't worry, I won't leav you hanging there, but I do think it is usefull to know this side of life too. If you are a complete novice to the command line I suggest you read my Linux 101 for Dummies which covers the basics.When reading through this stuff here you may get the impression that you are going to do a lot of command line typing stuff. Not so, don't sweat it. Even if you are not going to use an IDE but your favourite editor and work from the command line, your minute by minute routine is going to be something like 'edit code, type make' over and over again, not a lot of typing really. And even if you are using an IDE you need to be able to get grasp of the command line stuff to setup and trouble shoot the IDE stuff, when things don't go smoothly. A few tips that will make life easier on the command line. First of all you do not always need to type long lines, it is also possible to copy/paste stuff from for example this web page to the terminal so that you are spared from typing and typing errors. Pressing the cursor-up key in the terminal window will bring back your previously entered line, which saves you from a lot of typing when you repeatedly need to try things. Press the up key several times to go back even further back in history. This is universal, basic command line stuff that is worth learning. Every programmer should learn the basics of command line even if they spend all their life using an IDE. Note that this page and the examples are written with Mac OS X as a reference platform so some things maybe slightly different in Linux and Cygwin, but not much, and should work almost verbatim. Much of this applies to pure Windows as well, but I won't be covering all platforms here.
We are going to fire up the Terminal to get access to the command line but before we do that we do a litle tweak that will lessen the amount of typing we need to do. We will modify the we can just type
to invoke the C compiler.
This needs to be done before we start the Terminal for it to take effect. To modify the 'PATH' in Mac OS X you
open the file '.bash_profile' in your home directory and add the following line. Your home directory is the folder with
your username inside the 'Users' folder. So open the file and add the following line at the end:
If there is no such file, create it.
If you are using Mac's TextEdit as your editor always make sure that the "Hide Extension"
and "If no extension is provided, use .txt" check boxes are unchecked when you save a file. Note that the file is invisible in Finder because it starts with a period. Also worth noting is that you cannot open files whose name begin with a '.' from within TextEdit, you need to do this from the command line with something like Note that the 'shell' used by the Terminal was different (from bash) in versions before Mac OS Tiger so setting the path maybe a little different on older Macs. If you paid attention you noticed that while we were messing with the path we also added the path to 'bl08' which we will need later.
Next start a terminal session, on Mac you'll find the Terminal application under After firing up the terminal you should see something like:
Now to just see that the 'path' setup was succesfull type
and you should something like
and about a hundred lines of other helpfull text. If you get something like
you messed up the path setting or sdcc installation and need figure out what went wrong.
Next we install the bootloader software 'bl08' which will be used to download and program the
code into our microcontroller. To install it download the source code from here , unpack and
save into a directory named
To compile it you type the following:
This should complete silently in about a second and you should see, with 'ls' command, that
there is now a brand new file called 'bl08' in your current directory.
Your very first microcontroller software -- Blink LEDNow we are all set up to compile and execute our first program, the embeded world equivalent of the all time classic first program, "Hello World".Our first program will just blink a LED. Simple but oh so satisfying, I can still remember how it felt about 30 years ago when I saw my first microprosessor to blink that LED! Those were the days. This is also the way professional bring up prototype boards, just do something simple like blinking a LED to see that the system is at least alive -- you can't run until you can walk, so you need to take baby steps first. To save you from typing I put all sample code on-line and you can download it from here, but for, now lets pretend you type in the following program with your favourite editor: data at 0x00 volatile char PTA; data at 0x04 volatile char DDRA; data at 0x1F volatile char CONFIG; int main(int argc, char** argv) { CONFIG=0x01; // Disable COP DDRA=0x03; while (1) { unsigned int i; PTA ^= 0x03; for (i=0; i<50000; ++i) PTA; } } and save it to file 'blink.c' in the directory '~/hc08demos/blink'. Your editor does not support colors? Don't worry, them colors ain't supposed to be typed in , I just colored the listing to make it look pretty.
Next 'go to' the And finally we can compile it with:
This should silently produce, without any error messages, a bunch of files in the current directory, which you can verify
with ls command. If you made any typing errors, the compiler will output error messages on the console/terminal
along with the line numbers of the offending lines. Take your cue from there, go to your editor and fix them.
Among the files produced there should be one named Now all we need to do now is to download and program the code into our target microcontroller. To do that you need to have a serial port connected to the bootload adapter and the USB cable connected between the target and your PC to supply power to the device.
Before we can issue the download command we need to find out what is the name of your serial port. None of
the modern Macs come with a serial port, so if you are trying this out on a Mac you need to install a USB serial port adapter.
Don't forget to install the drives, see the adapter manual for that. The manual should also tell you the name of
the port but you can easily find it out yourself. This is a trick worth learning as this applies to Linux and Cygwin as well, just type:
This will produce a long list of devices, something like (I've shortened the listing from both ends to conserve some screen space):
Now we look for something with tty. and USB in it, and there it is, on the second column, last line but
one, 'tty.usbserial-FTOXM3NX' . In Linux the serial port is often called '/dev/ttys0' .
Now, the moment we have waited for, silence please, maestro drums.
Press the RESET switch to ensure that the target is in Monitor Mode, waiting for our stuff, and type:
If everything goes as planned we should see something like:
And the LED on our board should start blinking at the rate of about once a second!
What a feeling, time to celabrate, get me a tall decaf nonfat mocha latte with mapple syrup and foamy top , sprinkled with cinamon and chockolate shavings, I've deserved it. This was such a memorable occasion that I've even filmed it for posterity, don't miss it, click the picture above.
If, on the other hand nothing happens, it is time to trouble shoot so you need to read the trouble shooting section at the very end of this page. Before moving on, it might make sense to briefly consider what we have just done. To help to discuss this I've drawn the diagram below:
Reading from left to right things proceed pretty much in the same order as in reality.
First we have our program written in C in file called
All this takes place when we give the command Also worth noting is that SDCC is the 'one stop shop' for compiling programs, it is a C-compiler, assembler and linker, all in one package.
Next the loadable file is downloaded to the target microcontroller using the bootloader software Moving On - Hello WorldWhile a blinking a LED is a nice and necessary first step , what we realy need next is an ability to debug our software.Debugging is the art of finding out why the system doesn't do what we told it to do. To do that we need get information on what our code actual does and compare that information with our expectation of what it should be doing. I've done a fair amount of code writing on devices where the only output was a couple LEDs. Accomplising this does make you feel like a hero, but it doesn't make sense, so the first thing I always do in a new project is to set up some way to send text out of the device. When designing an embeded system it always pays to have serial port or some such on the device to get that text out when you are developing the software, and even later on, because when system device fails to perform as expected how are you going to find out what is the matter?
So, let's set up We are going to do the classic 'Hello World!' program, finally!
Doing that is pretty simple, create a directory named
In to this directory copy two files,
Now create and type in the following code and save it under the name
#include "printf.h" data at 0x00 volatile char PTA; data at 0x1F volatile char CONFIG; void putchar(char x) { (void)x; _asm bclr #0,_PTA jsr 0xFED6 _endasm; } int main(int argc, char** argv) { CONFIG=0x01; // Disable COP while (1) { printf("Hello World, this HC08 talking!\n"); } } Now you can compile this by typing the following three lines:
By the way, above is an example of how you compile and link a multi file project using SDCC, an example that I was unable to find anywhere when I started to work with SDCC. If you look carefully, and as a budding programmer you should always look carefully, you notice that the parameters passed to the SDCC are slightly different in this example than in the previous. Or actually they are the same but we are passing them slightly differently.
The first two lines are used to 'compile only' the files without linking them by
using the
The last line links the two previously compiled files, Note that it is always ok to pass both options to SDCC regardless weather you are just compiling or linking. Note also that with some different target architectures in SDCC the stack location is actually needed in the link phase, not in the compile phase, so maybe the best strategy is to always pass all parameters.
But all this typing is getting a bit tedious, so I think we are ready to introduce Make is traditional Unix tool that is used to automate the compiling of programs. Any non trivial C program consists of several source code files, each of which needs to be compiled separately and then combined into a single executable. That's what we did above, the two first lines in the example compile the two source code files and the last one links them together.
Now we use
Mac OS X supports
To use
Using
So create the following
Once you have the makefile in place you can 'make' it by just typing:
Which should result in the very same commands to be executed and displayed on the Terminal as you what you would have entered yourself manually. Now all that is left to do is to execute our litle Hello world.
So press RESET on the target device and type
the following command to download and execute the code. Note that this line is slightly different from
the previous command because we want invoke the
This should produce the following output on your terminal:
And so on and so. Bring another latte and this time make sure its hot enough and the crema gusto properly made.
To round up this 'Hello World' demo, let's review how we put together the
This is quite similar to what we did with the But there is one more thing to do before everything is set up the way I like. While using the command line is actually as simple as typing 'make', it is not the way I want to work all day long...because there is a better way... Real Programmers Do It with EclipseTo download Eclipse surf to http://www.eclipse.org
and go to Download page and select 'Eclipse IDE for C/C++ Developers', this is a nice little 67 MB download.
Double click on the
downloaded file and Mac will unarchieve it into a single
folder Launch Eclipse by double clicking the icon. Create a workspace by entering a path to where ever you want it to be generated. Close the wellcome tab/view by clicking the X -shaped icon. Eclipse is now installed and ready for action. Creating a Makefile based C project in EclipseCreate a new C project from the "File/New/C Project" menu and in the 'C Project' dialog that opens do the following:
Into the 'Project name' entry field enter a name for the project, call it Uncheck the 'Use default location' check box.
Into the 'Location' entry field enter
On the 'Project types list, expand the Makefile project and select 'Hello world C++ project'
It should look something like this (click image for larger view): Click 'Finish'. This will produce an error/warning dialog or two because the Makefile already exists, but just ignore them by cliking OK. Now the situation should look pretty much as follows: Make sure that the 'Console' tab (Eclipse calls them Views) is visible, if not, click on it. If you can't find it, go to "Window/Show View/Other..." menu and search for it by typing 'console' in the search box. This tab/view is the Eclipse equivalent of the terminal / command line we just left behind and you'll see the compiler print its error messages there and even the output from our target device. Remember the printf exercise we did above? You guessed it, even that output will appear in the console window!
Just for fun, you can now build the project, from the "Run" menu select "Build All" and you should see something like
this appear in the console view:
From now on, that is all it takes to build you project no matter how big it gets. Next we will introduce Eclipse and the bootloader to each other. From the "Run/External Tools/Open External Tools Dialog..." menu lets open the external tools dialog. In that dialog, on the left side panel, click on the row which says 'Program' and then just above it, click on the little document shaped icon with the plus sign on it. This will create a new tool with a default name. Into the Name entry field type 'run-hello' or what ever you fancy.
Into the 'Location' entry field enter the full path name to the bootloader, ie something like
Into the 'Working directory' entry field enter
Into the 'Arguments' entry field/box enter the same command line parameters we used when we did this from the terminal, i.e.
Now it should look like this: Click on the Apply button to save the changes. Now push the RESET button on the target device and click on the 'Run' button and you should see in the 'Console' view that first the project is quickly built and downloaded and then the familiar 'Hello World, this HC08 talking!' output should start ticking in the window. All you have to do now to build, download, program and execute you code is to select 'Run/External Tools/run-hello' from the menu. But wait, lets take it one step further. Select "Eclipse/Preferences..." from the menu and open the Preferences dialog. In the the search entry field type 'key'. From the short list that will appear select 'Keys'. On the panel that lists all the commands scroll until you find 'Run Last External Tool' command and select it. Click inside the 'Binding' entry field and then press the key on keyboard you want to use. Click OK. Now you can go through your whole development cycle of editing, compiling, linking, downloading, programming and executing by with a single key press. Now how cool is that!
Understanding the CodeThis page is not a tutorial on C programming, which you can easily find elsewhere, but I felt that a short line by line walk through the code might help you over some microcontroller related rough places not covered by standard C tutorials. Before I go through, line by line, the code I'll show you how basic input/output and peripheral control is typically handled with C in microcontrollers.
Brief Intro to Memory Mapped I/OSome processors have special instructions for doing output but much more typical is the use of so called memory mapping. Memory mapping simply means that the hardware is so wired that writing to a particular memory address will cause some action to take place in the hardware. Typically there is a hardware latch or register attached to that address and the outputs from that register control various things such as output port pins.
In this way controlling output devices or peripherals is as simple as assigning a variable! This concepts also applies to input and reading of peripherals. A few words of caution to store at the back of your mind though: memory mapped registers are actual hardware, not 'just' memory. Merely reading them may cause some special action and it is not always possible to read 'back' what you just wrote into a register, some registers are read only, sometimes the same address accesses different hardware depending on weather you read or write. Check your datasheets! In MC68HC908JB8 there are 64 of these memory mapped at addresses 0x0000-0x003F. Bits in these registers controls all the aspects of the microcontroller from simple pin output to clock frequencies and memory protection. Every byte in memory is made of eight bits each in either 1 or 0 state. This makes for 28 = 256 different combinations. When we are using these to do calculations this is typically treated as the numeric range -128..127, using what is called two's complement arithmetic.
But when we are talking about memory mapped registers or I/O ports, we are talking about individual bits which are indipendent of each other, yet we have to treat each byte which is a group of eight bits as single value. Each bit has some specific meaning in the hardware, this meaning being explained in the datasheet of the microcontroller. What we need to do is to be able to change those bits individually. Full discussion of binary arithmetic and bit manipulation is beyond the scope of this page, but here is a quick cheat sheet. It is convetional to visualize a byte as a box with eight cells numbered them from right to left as follows.
To SET, CLEAR, TOGGLE or TEST a bit in any memory mapped REGISTER look up the bitmask from the following table:
(If you want to set or clear more that one bit at a time, you can combine the masks with the or-operator '|') Once you have your bitmask you use the code from the following table:
There is a strong tradition to use hexadecimal numbers instead of decimal numbers when dealing with bits so I've used it here, just better get used to it. Walking through
What follows is a blow by blow treatise of the code.
|