October 24, 2009

Hexing up the ATMega168 Using Linux and Avrdude

NOTE: Since I am a serial tweeter anyway, I decided to tweetcast my progress live! Follow herdngelectrons to see my lab schedule, comment, or lurk from here on out. 

This week I built upon the 5 volt testbed I made last week, using the VCC rail to power the ATMega168. I also learned the basics involved in compiling the code with gcc-avr and using avrdude to move the code onto the micro. I followed Sparkfun's tutorial: Beginning Embedded Electronics - 2 Getting AVR HEX code loaded onto the ATmega168, well, not exacly followed; the thing was written for Windows users with parallel ports, neither of which I can lay claim to. So off the beaten path I went, and had a pretty fun time doing so.

So I started off the week inspecting the parts kit shipped by Sparkfun. All looked good at first until I stared at the parallel port programming dongle sent, realizing my laptop had no parallel port. My backup laptop had no parallel port. My wife's computer had no parallel port. My daughter's computer had no parallel port. I tried to think of the last computer I had with a parallel port - Holy cow, unless you have a desktop, you're pretty much out of luck. Oh well. Maybe to be a hardcore micro hobbyist, you need to hack a parallel port on whatever you use! I do have a USB port replicator, but read that these really don't work well for this sort of thing. USB programming dongles are out there, but whoa doggy are they pricey. I think $85 was the cheapest I saw. My lappy has a serial port though. Sparkfun did have a serial programming dongle. The spec page doesn't mention any support for ATMega168, but the tutorial refers to the thing (AVR-PG1) specifically at one point. What the heck, the thing was $12 so I ordered it.


To start off, I installed the AVR on the breadboard. I carefully and gradually bent the pins on the package to fit. It was at this point I realized the tutorial didn't really say anything about static guarding your micro. At first I was slightly annoyed that a beginner's tutorial wouldn't mention this, but then realized one important fact. This micro costs $4. I am used to handling VLSI chips worth $100 or more. I suppose at that cost I can leave the grounding wrist strap packed up and just touch the metal leg of the table and go on with life.


I ordered Sparkfun's cheapo-DVM-with-tone-function and decided it was very nice! Much better than my el cheapo and I enjoyed the alligator-clipped leads it included. I used the tone function to measure resistance on the legs of the 4-leg momentary switch, ensuring I didn't install the damn things sideways! @gevmage helped out by explaining the pitfalls of floating the RST pin, and why we need to pull the voltage high: "Two things in circuit bad: untied inputs, and outputs connected together (in slow logic; fast is different)." So there you have it! I also wired up VCC to pin 7, and GND to pins 8 and 22. I checked my voltage again, because I'm a paranoid wreck.


So I had successfully wired and powered my AVR. Next I needed to load some code on the thing. Time was nigh to see if I could get the serial programming dongle to work. I ran into an unexpected issue right off the bat: The dongle serial connector design assumed a serial port that pokes out of your comp. Of course, mine was recessed. The dongle connector case had protrusions that prevented me from seating it. I decided it was time to void the warranty and start cutting. Protrusions? Cut those bastards off. They were there to secure the package to the metal part of the interface, but as long as I'm careful to not rip the wires out when unplugging it, I'll be OK. The modified serial dongle plugged in nicely.




Following the datasheet for the ATMega168 I wired up the serial cable to the corresponding I/O pins. Looked like so much spaghetti. After wondering why I needed to wire up 4 GND connections (but doing so anyway), I reminded myself to order a 10-pin header for the next try. I still don't know what MOSI or MISO pins are for exactly, although I suspect the I and O may be (I)nput and (O)utput? Maybe that's my homework this week.

So at this point, the tutorial started going on about WinAVR and all the windows tools, what's a C compiler, what good code editors do, etc, etc. I kind of glazed over and took off on my own direction since I use Ubuntu (Intrepid Ibix) Linux, I use vim for coding, I know what make and gcc do, and well, why start using Windows now?. So I got my penguin on, downloaded the zip file containing the Makefile and the sourcecode and took off into the sunset. I figured out that all I needed to install in Linux was gcc-avr, avr-binutils, avrdude and avr-libc, all available in the Intrepid download channel (yesss) using apt-get or Synaptic package manger. I was happy to see the serial dongle was configurable in the Makefile, although it listed "COM1" as the serial port. A quick check in /var/log/dmesg confirmed my serial port was accessable through /dev/ttyS0, so the correct config for me was "AVRDUDE_PORT =/dev/ttyS0". I also needed to change the programmer var to: "AVRDUDE_PROGRAMMER = ponyser". To compile and transfer, "make all" and "make program" worked a-ok*, the code loaded onto the micro, avrdude posted happy messages, and the LED began to blink.

ZOMG it worked! I celebrated success with a cold Guiness. Maybe now we should start thinking about all the cool stuff we'd like our micro to do. Mmmm California dreamin :)


* I actually ran "sudo make program" because I as a regular user did not have enough permission to use the serial port. Details, details.



NEXT WEEK: Lecture 3, What is an Oscillator

4 comments:

Craig Steffen said...

As far as grounds, I assume the microcontroller-thingy that you're programming sends and receives signals slowly enough that it doesn't need multiple grounds for signal transmission reasons. I presume those are pins on the gadget that aren't being used currently, and so they're tied to ground to make sure they're in a known state? Maybe?

I didn't know MISO and MOSI either, but a quick Google search turned up the answer. I won't spoil it, but you were close.

Craig Steffen said...

[At the risk of being a serial commenter...]

Yeah, an untied reset pin will really make you hate life.

Similar problem, other end of the spectrum in complexity: Back in the day (late 1990s) when I was in graduate school in physics, one of the experimental end stations (not mine) was down for a couple of DAYS, I think, because the control equipment in the beam line kept having intermittent problems. They kept suspecting the power supply, but it was definitely operating.

It turns out that the power legs (it generated +12V, -12V, or maybe +24/-24V) were all fine, but the GROUND on the supply had become disconnected, so it was floating in bizarre ways and causing errors.


I checked my voltage again, because I'm a paranoid wreck.


It's astonishing how often that coincides with effectively keeping your butt out of trouble. To me, there's NOTHING more annoying* than having checked a basic paraameter of something, making a couple of dozen changes, discovering that the parameter changed, and then having to do a binary search backwards to figure out which thing you did that screwed it up.

* up to and including the Patriots beating the Colts for the AFC Championship, although that's close

Jeronimo said...

Nice Post.
Attention when you move to the ATMEGA328. You will need to upgrade your AVR-GCC to 4.3.3.
Take a look at these instructions at http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=42631

Jeronimo.
www.blogdoje.com.br
Avr, Arduino & ARM

--== chux0r ==-- said...

@Craig Steffen, funny you mention that, it should have occurred to me as I (and I know you too) have spent time chasing down automotive electrical problems sourced from floating ground connections. That always causes a real mess of issues that manifest themselves in the most interesting and infuriating ways (most common: you press on the brake pedal and instead of brake lights coming on, all of the tail lamps and running lights go dark). I am ordering a 10 pin header as I write - I'll just tie pins 4,6,8,& 10 together.

@Jeronimo, thanks for the heads-up! I went and checked my avr-gcc version, it's 4.3.0. So the upgrade is in order. Thanks also for the link to AVRFreaks - looks like my kind of place :) And I need all the help I can get my hands on