November 15, 2009

Mucking About with Code and UART I/O

While attending my daughter's dance recital Saturday, my mind stuck on the word "recital". The kids weren't reciting anything, they were dancing. Neither do kids at a piano recital actually recite anything, and so on*. "Recital" I suspect is one of those words originating out of a specific common event, which eventually broadened in definition to mean any event where a pupil demonstrates his progress and/or achievement in a very formal and public way. Once I sorted it out in my head, I realized I am performing sort of an electronics recital here, am I not? Only difference is, when I'm done I intend to launch killer flying killer robots instead of playing "Mary had a Little Lamb". Heck, I might even make flying killer robots that play "Mary had a Little Lamb"! Onward with the recital then...

I did Sparkfun's Beginning Embedded Electronics tutorial #5. It was labeled "AVR GCC Compiling", but I like my title better, since we really do just muck with some code and our serial interface some more. And that's cool with me, I have a little 16x2 serial display on the way; I need to get a handle on this stuff. I realized late last week that the subtext to last week's lab said we'd printf some foo to our monitor via a running program on the AVR, and somehow that never happened. Until now, of course.

I started by compiling and writing the "basic-out-atmega168" program to my micro. Of course I noticed right away that something wasn't quite right - The code was supposed run on the micro and output something like "Test it! x = 1Test it! x = 2Test it! x = 3Test it! x = 4Test it! x = 5" to my minicom (terminal emulator) screen. What I got was a bunch of garbled crap. Looked like the inventor of ASCII threw up in my term session. Good time to recap what we learned last week. I did a successful loopback test on the UART. Looking at the schematic for the max3232 indicated I tie RX and TX on the micro somehow to "R1OUT and T1IN".  A break in the drawing doesn't show which goes to which though. I guessed RX went to the transmit pin and TX to the receive pin. A basic crossover connection, right? Looking at the resultant garbled crap made me think about what the RS232 controller really does - it does some voltage foowankery, but basically it's a traffic cop, a pass through. That means the TX on the micro goes to T1IN and RX goes to R1OUT, and all would be fine. It was. Program now works. Just a note, Homework for this week is to see if I can manage 2 serial streams with this guy, cause I noticed a set of unused pins labeled R2xxx, T2xxx.

Can you spot my rookie screw up here? This setup can't work.

The tutorial goes over some basic methods used in C to twiddle bits, toggle GPIO pins, and set other options. After a while, it'd probably be good to spend some time rooting around header file avr/io.h to see what interesting AVR defines and directives exist. Even though the tutorial skirts around the subject, now is probably a real good time for me and anybody else who hasn't used a bitwise C operator since college to get a good refresher. They are of course used to enable things that could make life sweet, like the TX and RX pins (RXEN0 & TXEN0). More homework for lucky me. The author states that he never starts any code from scratch - he always modifies existing code. If I actually trusted other programmers, I might use this method. But since I trust neither the coders out there, nor coding contexts as germane to my efforts, I think I may spend more time getting familiar with the coding end of this whole thing. I can't recommend my way over Sparkfun Nate's, as it will be considerably more work, I imagine it just depends on whether you trust people or not. Damn, trusting people is a lot less work than my way. *sigh*

I won't discuss compiling and loading the second program, since it was uneventful, so that's it for now. Time to think about using the interface for more useful stuff. Like I said earlier, I have a 16x2 serial display en route. I'll see if I can get the micro to display to it next week.

The next 2 Sparkfun tutorials deal with soldering. I am probably going to skip these. This does not diminish the importance of good soldering technique, I am just already pretty good at this. Not sure about the CAD ones either. I have downloaded the free Eagle CAD software (good stuff! they have great Linux support!) and will use it to design my stuff, but I want to keep focusing on getting the micro to bend to my will, not peripheral stuff. So I guess that means I am pretty much done with the Basic Embedded tutorials at Sparkfun! Woooo! Be sure to check out their last one, which is basically things to do to prevent frustration and embarrassment. I should formally thank them for sharing all the learning material. I had fun and now know a little something about something.

*If one was performing an original oratory for the very first time, would that just be a "cital"? Or does one "cite" when first putting pen to paper? No, I would think it drafting or composing. And then everyone repeating it would then re-cite it? No, that would be speech, or speaking. But there is no "respeeching" or "respeaking", only recital. Strange word. Calling all linguists! Attack!

No comments: