November 7, 2009

Installing and Testing a Serial Interface


As I wandered through the farmer's market in Urbana this morning, instead of thinking about tasty dishes I could cook, I was wondering if I could use the the old potato-battery trick to power a micro. So that pretty much tells you where my head is these days. According to this guy, we'd get 0.5V with 0.2mA, so that means we'd need what? A bakers dozen to safely power our ATMega168? Good to know if I ever get stranded without power in Idaho! Maybe for fun I'll do this later on.

Now, down to the business at hand: Today I hacked a serial interface onto the ATMega168, using a MAX232 IC, and tested it using Linux and minicom. I was loosely following Sparkfun's "UART and Serial Communications" tutorial, but with the normal diversions to use tools commonly used with Linux. In this case, the tutorial would have us test out the rs232 interface by firing up Hyperterminal (infernal Windows term thing). Yeah, you know me. I was all "we're totally not doing that."

Here's where I started: I have a max232 IC which is my serial interface. I really don't have to know much about it, other than the fact that it converts +/-12V signals to usable 0-5V signals for our micro and other UART magic (the charge pump link in the tutorial is pretty interesting though). I noticed again no mention of static guarding IC's; Am I just too anal about this? I still don't have a grounding strap for my wrist, but I grounded myself to the leg of the table before I carefully bent the pins and installed it on my breadboard.

It was nice to know about the decoupling caps - I suppose I should plan on keeping a bucket of 22 and 10 uF ceramics around. I noticed also we weren't using electrolytics anymore for decoupling. We used them for the 5V power supply, but not for the oscillator nor for this max232. Can anyone tell me when one is preferable over the other? The datasheets of course specify ceramics, and that's fine. I was just wondering.

When installing the caps, one thing jumped out at me. Caps decoupled pins 1&3, 2&6, 4&5. Pins 7&8 are unused. Sound familiar? It did to me. That is the wiring order for a CAT5 UTP crossover cable! And it made sense: Back in the day, the first ethernet cards I remember had serial-looking interfaces on the back and often required tranceivers to accept CAT5 with an RJ45 end. I should've made the connection back then, but probably had my mind on other crap! Just an interesting note.

I put a loop on the TX and RX pins of the IC, so whatever input I give it will be puked right back out. In this state, we are able test the proper functioning if the max232 by hooking up a terminal emulator or heck, even a real vt100 terminal if we want!.

The tutorial tells us to use Windows' Hyperterminal, or maybe TeraTerm. Nonsense I say! Linux! I used minicom. Minicom on my Linux laptop had no pre-set config, and defaulted to some bad defaults which I needed to change.

Welcome to minicom 2.3

OPTIONS: I18n 
Compiled on Oct 24 2008, 06:37:44.
Port /dev/tty8

              Press CTRL-A Z for help on special keys

You'll notice it defaults to tty8. We need to change that and switch the port speed to 9600bps. The quickest way to do this is to hit "ctrl-A O" once minicom starts.

+-----[configuration]------+
| Filenames and paths      |
| File transfer protocols  |
| Serial port setup        |
| Modem and dialing        |
| Screen and keyboard      |
| Save setup as dfl        |
| Save setup as..          |
| Exit                     |
+--------------------------+

I selected "Serial port setup" and made the following changes:

+-----------------------------------------------------------------------+
| A -    Serial Device      : /dev/ttyS0                                |
| B - Lockfile Location     : /var/lock                                 |
| C -   Callin Program      :                                           |
| D -  Callout Program      :                                           |
| E -    Bps/Par/Bits       : 9600 8N1                                  |
| F - Hardware Flow Control : Yes                                       |
| G - Software Flow Control : No                                        |
|                                                                       |
|    Change which setting?                                              |
+-----------------------------------------------------------------------+

I ran into problems here. It's worth noting that I spent a while troubleshooting a seemingly non-working serial connection. I would type and type; nothing ever got puked back to the console. I noticed that the wires running to the DB9 interface were a little too loose for my comfort, so I got out the soldering iron and a new slice of bread and fixed that mess. Still no worky. After beating my head on it for a while I figured out that somehow changing minicom on the fly does not always work. When I saved the settings to a minicom config file and restarted, all of the sudden the thing worked fine. Yesssssss! I took the loopback plug out and connected the RX and TX pins if the max232 to the RX and TX pins of the ATMega. Now we're ready for some cool I/O fun!

So next time I will be doing the AVR-GCC lab. Not sure if it will be me just going off on my own, but in any event I will be using my new serial interface to debug stuff, which is cool! Later for now, all - I'm gonna go look at cool 19x2 serial displays and russet potatoes!

1 comment:

Anonymous said...

Would you mind giving us more of an idea of how to use the Serial communication in Linux? For those of us who have never used it before, how can you send text once you have it set up? Also, have you ever used 'moserial'? It seems to be a little more user friendly (IE has a GUI)

Thanks!

JR