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

October 17, 2009

5 Volts on a Saturday Afternoon

I have a streak I'm sure some of you out there can identify with. The streak goes like this: I start something, get bored with the basics, jump to the super-advanced part, build some monstrously awesomely advanced thing which then causes an unintended, yet epic conflagration or disaster. Seems crazy, but I have convinced myself the fun doesn't start until I'm knee-deep in something. After the fire department leaves, I always feel like maybe I should have maybe finished studying all that boring basic stuff. So this time no skipping through. I decided to go back and start my re-learning process with something we can all roll our eyes at: making a simple 5 volt DC power source. Yeah yeah, it doesn't get simpler, but hey later on we'll sure enjoy knowing that the problem were troubleshooting on our micro isn't rooted in the power supply. I can even tell the fire chief that my power supply was built with current-limiting awesomeness if he happens on by to see what the hell I'm doing this month.


I chose Sparkfun's "Beginning Embedded Electronics - 1" tutorial as it is nicely written and very easy to follow. It also contains some really nice supporting info, descriptions, and suggestions. I ordered all the parts from the convenient link included in the writeup. The kit arrived in my mailbox a couple days later, containing a breadboard, some 18ga. wire, a 9V wall wart, a switch, a barrel connector, 2 elecroltic capacitors, a 5V regulator, 2 LEDs, a diode, 2 resistors and an, ahm, a thingy? (We'll get to that thing later) So time to roll up the sleeves and put this together.


Note that I am selling my house and have already moved away. I am living in a small 2 bedroom apartment. At the house I had access to my workshop. Here I am working out of a "bare essentials" toolbox. So those of you out there doing this on your kitchen table - I'm right there with you. The tools used today are: needle nose pliers, an el cheapo DVM, wire strippers, and alligator clips. I don't have my nice soldering station (it's in a box floating around somewhere) so I made a trip to the local Radio Shack and picked up a 25W iron good for meatball joinery. Also grabbed flux and solder. Since we're not going to be soldering anything super sensitive, I'm not worried about my hot-ass iron. If you plan on soldering near or on sensitive stuff in the future however (ICs, micros, etc), you may want to splurge on a good digital temperature-controlled soldering station. I love mine (wherever it is).

Upon unpacking, I noticed the breadboard came "assembly required" which is no biggie, but I wish the adhesive for the rubber feet hadn't all dryed up and blown away in the packaging. I guess I'll take care of that with some spray adhesive later. There were no instructions, but I figured out how everything on the breadboard goes together just fine. If you don't, post or e-mail me and I'll help where I can.


Plugged in the wall wart and measured my voltage at around 9 volts. I don't have an o-scope to see how clean this is, but as the tutorial says, it's reasonable to assume a fair amount of ripple on the output of the wall wart. Who cares? We're going to fix it up with some caps later anyway like good boys and girls.



I like the idea of putting a jack on the board for nice power connect/disconnect. Soldering the barrel jack looked like it would be straightforward, but I encountered a few issues. Number one was keeping it still while soldering. If you have a set of clips or "helping hands", use 'em. I opted for something soft and pliable that I could smush the barrel into securely while soldering, yeah, that's a piece of bread. It's the end, which noone in this place will touch anyway. I imagine modeling clay, play-doh, plastic explosive, or other pliable and non-flammable material will work as well. I just won't eat the bread when I'm done - Not only are bread ends crusty and undesirable to most, on top of that solder for electronics is made with a host of nasty and/or poisonous stuff such as arsenic and lead. I have enough mutant powers alreadt, So I just tossed the bread.
Ran into a samll gotcha when assembling the barrel: the side lead on the barrel is not a ground lead like I thought it was. The end lead is 9V, the middle lead out the bottom is GND, and the side one is neither. Don't connect this one to GND like I did. I connected this to the breadboard afterwards and re-measured to ensure I saw 9V on the board.


After putting the 5V regulator on the breadboard, installing the switch and caps, and jumping the gaps in the GND and VCC rails, I took an extra step and verified 5 volts on the top rail. A small step, I realize but I crazy Ivan a lot when working to ensure all the details are in order. I could've just wired up the LED and resistor to verify (like the tutorial did), but then I couldn't be all paranoid. Just to mention, my paranoia is completely irrational; Sparkfun sent me an extra LED and resistor, probably on purpose.


Hey! it works!

The cathode of the LED is indicated on the schematic with a flat line and in real life manufacturers print a band or a cast a flat side to mark it. A casting flaw in my LED obscured the flat side, but I noted the cathode leg is usually shorter than the anode leg. This is minor stuff, as putting one in backwards usually doesn't damage much, as the tutorial reaffirms. It's still nice to get it in right on the first try (that's what SHE said!?)!

The regular diode was never really shown anywhere but on the schematic, I included a picture of it installed and working just because.

I encountered an interesting extra device I suspected was the PTC discussed in the tutorial. The labeling was a little cryptic but a little googling of the markings affirmed my suspicions, and a data sheet! Now I know it's what I think it is, I installed it in circuit.

Well that's it. 5 volts, VCC, whatever you want to call it. Next week I will hook it up to an ATMega168 and attempt to make something blink. Hopefully between now and then I can figure out who is telling the dev manufacturers that parallel cables are the way to go for uploading programs. Yeesh.

--chux0r

October 12, 2009

Pfft, pfft - Is This Thing On?

Every 8 months or so I decide to add a tool to my arsenal. See, I'm a big fan of knowing about shit (James Madison FTW!). I dig into something new on a regular basis. Usually it's a solitary pursuit involving me, some books, some gear, pen, paper, the intertubes, some conversation in my head, perhaps even an end in mind. The problem? Well my mind is filled with so much stuff, I fear over time I just lose things in there. I lose things I don't care to lose, like those great conversations I had with myself, when I figured out some shit on my own. What's left? An odd sensation that I once knew something really cool but have now forgotten. Frustrating. Not crying over spilt milk here, I'm just saying it won't happen this time or with any luck, ever again. I'm going to do it differently. I'm going to let everyone in the conversation this time. I may forget, but the cloud will remember. Maybe this way I can keep it straight? God I hope so.

So this blog is dedicated to documenting stuff I learn, including examples, fuckups, trials, conundrums, screaming frustration, and glorious triumphs. I will start with embedded electronics. I enjoyed reading through sparkfun.com's tutorials enough to document my progress through each one, hence "herding electrons". I hope to gain enough confidence to build some stuff I've been thinking about for a while - we'll get to that later.

Some background: I know a little, enough to be dangerous. I studied electronics while in the U.S. Navy's Basic Electronics & Electricity school. BE&E was 14 weeks of theory, practice, and sheer terror wherefrom I remember Ohm's law, the slutty ways of the infamous Violet*, hole flow, what an inductive-capacitive circuit does, semiconductor theory (AKA "as far as you're concerned it's all PFM kid"), basic troubleshooting, why I'd never care about MOSFETs in the field, and, according to my broadcast engineer father, not a %#$^@ thing about the most important and fabulous electronic things on earth: tubes. I ditched my test probes after graduating though, deciding to instead do something Dad knew nothing about. Years later when I started restoring old arcade machines I picked up a soldering iron again, mostly to shotgun malfunctioning stuff with new electrolytics and the occasional bridge rectifier. Mostly I know how to solder. Amazingly enough it's the thing I'm most proud of right now simply because it's the most practical thing I know out of all that crap. So there's where I start.

The goal is to use the knowledge to actually do shit. So here's the deal. I am going to ramble on about shit I learn, or break, or get stuck on. It will mostly be a conversation with myself about crap everybody else already knows. Feel free to post if you feel I've strayed so far afield the comedy of the situation moves you to point out my error merely as a point of mercy. But do remember I am not much interested in the theory unless I can apply it, get my hands dirty, and use it right now. Hey, maybe my crazy one-sided conversation turns to dialogue!

This thing is on, just don't tell my Dad.

* The old non-PC mnemonic for remembering resistor value color codes 0-9 was: Bad Boys Rape Our Young Girls But Violet Gives Willingly - nice, huh?