C programming and microcontrollers are two big topics, practically continental in size, and like continents, are easy to get lost in. Combining the two is a little like traipsing from Alaska to Tierra del Fuego. Chances are you’ll get totally lost and if the natives don’t eat you, your infected blisters will make you want to sit and pout. I’ve been down this road so many times that I probably have my own personal rut etched in the metaphorical soil... and I can point to the sharp rocks I’ve stepped on, the branches that have whacked me in the face, and the bushes from which the predators leapt. If you get the image of a raggedy bum stumbling through the jungle, you’ve got me right. Consider these workshops a combination roadmap, guidebook, and emergency first aid kit for your journey into this fascinating, but sometimes dangerous world.
|FIGURE 1. C being invented.
I highly recommend that you get the book The C Programming Language — Second Edition by Kernighan and Ritchie, here after referred to as K&R. Dennis Ritchie (Figure 1) wrote C, and his book is the definitive source on all things C.
In Figure 1, Ritchie (inventor of the C programming language) stands next to Ken Thompson, original inventor of Unix, designing the original Unix operating system at Bell Labs on a PDP-11.
I have chosen to follow that book’s organization structure in this series of workshops. The main difference is that their book is machine independent and gives lots of examples based on manipulating text, while these workshops are machine dependent, specifically based on the AVR microcontroller, and the examples are as microcontroller oriented as I can make them.
Back in the dark ages of microprocessors, software development was done exclusively in the specific assembly language of the specific device. These assembly languages were character based ‘mnemonic’ substitutions for the numerical machine language codes. Instead of writing something like: 0x12 0x07 0xA4 0x8F to get the device to load a value into a memory location, you could write something like: MOV 22, MYBUFFER+7. The assembler would translate that statement into the machine language for you.
I’ve written code in machine language (as a learning experiment) and believe me when I tell you that assembly language is a major step up in productivity. But a device’s assembly language is tied to the device and the way the device works. Assembly languages are hard to master and become obsolete for you the moment you change microcontroller families. They are specific-purpose languages that work only on specific microprocessors. C is a general-purpose programming language that can work on any microprocessor that has a C compiler written for it.
C abstracts the concepts of what a computer does and provides a text based logical and readable way to get computers to do what computers do. Once you learn C, you can move easily between microcontroller families, write software much faster, and create code that is much easier to understand and maintain.
There are many excellent microcontroller families out there, but I chose the AVR because — among many reasons — it was designed with the C programming language in mind, has Flash memory that can be configured with a bootloader (more on all this later), and has the best user forum I’ve ever used: www.avrfreaks.net.
The AVR is fast, cheap, in-circuit programmable, and development software can be had for FREE (really free, not crippled or limited in any way). I’ve paid thousands of dollars for development boards, programming devices, and C compilers for the other micros, but never again — I like free. The hardware used in these workshops — the Atmel AVR Butterfly — can be modified with a few components to turn it into a decent learning system and the needed components can be had for less than $40. You can’t get a better learning system for 10 times this price and you can pay 100 times this and not get as good.
You will often stumble across arguments on the Internet as to which microcontroller or programming language is the ‘best.’ People take sides and soon a religious war breaks out. I don’t want to enter that fray, so I’ll just say that the AVR is the best microcontroller and C is the best programming language and if you don’t believe me, you are bound for perdition — hallelujah and amen. (Agreed! -Ed.)
What I hope to accomplish with this series is to help you learn SOME C programming on A SPECIFIC microcontroller and provide you with enough foundation knowledge that you can go off on your own somewhat prepared to tackle the many kinds of microcontrollers and C programming systems that infest the planet.
Both C programming and microcontrollers are best learned while doing projects. I’ve tried to provide projects that enhance the learning process, but I’ve got to admit that some of the projects are pretty lame and are put in mainly to help you learn C syntax and methods.
I know how easy it is to get bogged down in all the detail and lose momentum on this journey, so we’ll begin with a quick start guide and learn just enough to get our learning platform tools working — kind of a jet plane ride over the territory.
AVR Studio — FREE and darn well worth it.
AVR Studio is provided free by the good folks at Atmel Corporation, who seem to understand that the more helpful free stuff they give developers, the more they will sell their microcontrollers. Actually, this software could cost hundreds and still be darn well worth it, but unless you just really like Norway, don’t send them any money; they’ll get theirs on the backend when you start buying thousands of AVRs for your next great invention. The workshop is based on version 4.14 and if you use a newer version you may find differences in it and our discussions. You can find AVRStudio at www.atmel.com under the AVR Tools & Software section: http://atmel.com/dyn/products/tools_card.asp?tool_id=2725).
WinAVR — Oh, Whenever ...
WinAVR is a GCC compiler toolset for Windows that we will use in AVR Studio. We will use this package under the AVR Studio IDE that has a GCC plug-in that finds the WinAVR installation and adapts it to the IDE. You can find WinAVR at: http://sourceforge.net/projects/winavr/.
Figure 2 shows the PC terminal we will use to communicate with the AVR in our workshop. You can get the terminal installer and related documents from www.smileymicros.com. The source code in C# and Visual Basic .NET for this terminal is discussed in detail in the book Virtual Serial Port Cookbook (by me) and is available from the Nuts & Volts webstore and www.smileymicros.com.
|FIGURE 2. Developer Terminal.
It is simply amazing what the Butterfly has built in:
|FIGURE 3. The Butterfly front.
I mean this thing has everything (except a kitchen sink ... sorry). If anyone can find a learning platform with anywhere near this much for this price, I want to hear about it. If I seem to be raving a bit, get used to it. I do that a lot.
The AVR Butterfly box has instructions to show you how to use the built-in functions. Play with it now before you risk destroying it in the next step. I shudder to think how many of these things will get burned up, blown up, stepped on, and drenched in coffee. (And that’s just me this morning.) After you’ve seen how it works out of the box, remove the battery and prepare to add components for our learning platform.
|FIGURE 4. Butterfly++ Mini-Kit.
The Butterfly provides an excellent learning platform, but it can be even better with a few extra parts that Smiley Micros supplies in the Butterfly++ Mini-Kit. This kit includes a CdS light sensor, a DB-9 female connector and wires, and a two AA battery holder with power on LED and resistor.
Atmel’s AVR Butterfly no longer has a light sensor due to European RoHS compliancy considerations (don’t get me started ...). The mini-kit provides a suitable substitute component that the user must solder to the Butterfly to use the light sensor function. This sensor works with the existing Butterfly software.
The CdS light sensor is a device that has resistance proportional to the incident light. As a resistor, the device has no polarity so either leg can be inserted in the pads shown circled in red (see Figure 5). Seat the sensor snug to the top of the Butterfly, then solder the legs to the bottom and trim them just above the solder meniscus.
|FIGURE 5. Butterfly CdS light meter.
In order to communicate with a PC, the Butterfly must connect to a serial cable. The mini-kit provides the connector and wire to make the connection to a serial cable.
The Butterfly has built-in RS-232 converters for serial communication with a PC. Most serial cables will have a DB-9 male connector on the device side that will mate with the provided DB-9 female connector (calm down — it’s technical).
|FIGURE 6. RS-232 DB9 connection.
Strip about 1/8 inch from the ends of each wire and then carefully solder them to the Butterfly and the DB-9 connector as shown in Figure 6. Notice that the upper wires in the picture cross. Be very careful to get this exactly according to Figure 6; about half the emails I get for problems turn out to be related to either incorrect wiring or poor soldering of this component.
|FIGURE 7. AVR Workshop Learning Platform.
Refer to Figure 7 to see the location of the ADC, USI, PORTB, and PORTD pads. Solder the two-pin header to the ADC pads; the four-pin header to the USI pads; and 2x5 headers to the PORTB and PORTD pads. Notice that Figure 7 shows a male header on the ISP pad; this is not included in the kit and won’t be used for our work (maybe later).
The AVR Workshop Learning Platform is built on a foamcore board that lives in a protective foamcore box. (Details for this construction can be found in Smiley’s Workshop 1 Supplement: AVR Learning Platform Foamcore Base and Box at www.nutsvolts.com.)
You will need to carefully twist the ends of the battery box wires until they are straight, then soak them with solder so that it runs up under the insulation to make these wires strong enough to insert into the breadboard power bus. The red wire goes to the + red bus and the black wire goes to the – blue bus. Connect the two power busses with red and green wire and then put an LED with a 2.2K resistor on the breadboard. The resistor goes to the + power; the LED short leg goes to the – power. The LED and resistor are then connected on a breadboard strip.
|FIGURE 8. Close-up of power connections.
The Butterfly receives power from the bus as shown, the + red wire goes to the rightmost top of the header, and the – green wire goes to the rightmost bottom of the header.
Hook your Butterfly DB-9 connection to an RS-232 cable from a PC. If you use a USB-to-serial converter cable, you may have problems if the voltage levels are not robust. I’ve used several and have not had a problem.
Open Developer Terminal. You can read the user manual (I recommend: RTFM) by clicking the ‘Help/Manual.’ Click the ‘Settings/Port’ menu item to open the settings window. Select the RS-232 COM port that the Butterfly is connected to. Set the baud rate to 19200, data bits to 8, parity to None, stop bits to 1, and handshaking to none. You can test that your learning platform is working okay by two methods. A simple test is to turn the power off on the Butterfly and then WITH THE JOYSTICK BUTTON PRESSED TO THE CENTER, turn the power back on. Now each time you press the joystick button to the center, you should see a series of question marks (???????) in the Simple Terminal receive window. This is the Butterfly bootloader wondering what the heck is going on.
Another test is to turn the Butterfly on and click the joystick up to get the LCD scrolling. Move the joystick straight down three times till you see ‘Name,’ then move the joystick to the right twice till you see ‘Enter name.’ Move the joystick straight down once and you will see ‘Download name,’ then push down the joystick center for a moment until you see ‘Waiting for input.’
In Developer Terminal, make sure the ‘Send Text: Immediate’ radio button is checked. Type in your name, then in the ‘Send HEX Immediate’ dropdown box, select and click on 0x0D that tells the Butterfly you are finished sending characters. Your name should appear on the LCD.
This isn’t easy and there are many opportunities to mess up along the way. Many folks get this going right away, but others seem to have fits getting over this hurdle, so I’ve provided a Workshop 1 Supplement: Problems Communicating with the Butterfly, in the downloads section of this article's page at www.nutsvolts.com to help you get past this point. Trust me, if you see the ????? or get your name scrolling on the LCD, you are over a major hump and subsequent workshops will be easier to get going than this step.
Well, that was a lot and we haven’t really gotten started yet. Next month, we will look at the software side of the AVR Learning Platform and learn about the AVRStudio IDE and the WinAVR C compiler toolset. We’ll write our first C program and create a set of Cylon Eyes in hardware. NV
|3” Wire AWG 22
|Two 2x5-pin header
|Two-AA Bat. Box
|DB-9 Female Ctr.
Joe Pardue has a BSEE and operates www.smileymicros.com from the shadows of the Great Smokey Mountains in Tennessee. He is author of Virtual Serial Port Cookbook and C Programming for Microcontrollers
|WORKSHOP BOOKS & KITS
If you’re following this series and want to read further or jump in and try the experiments, click this banner to go to the Nuts & Volts webstore where you’ll find all the hardware kits being used throughout, as well as Joe’s books. It’s a great way to learn microcontroller programming with C and get the kind of hands on experience, as you go, that brings the lessons to life.
Smileys Workshop 200808 (AVR Workshop Supplements)