I’m learning a lot programming in AVR C. There’s are a few subtleties to watch out for, and some of them have had me banging my head against the wall for days. This post is an attempt to prevent anyone else playing with AVR outside of AVR Studio (I’m using Linux and the command line) from having the same issues.
Sections for avr-objcopy
When you compile an AVR C program, you get object code, which you have to convert into HEX format to flash onto the chip. You do this using avr-objcopy, and there are apparently several valid ways of doing it. I recommend learning about what the -j and -R flags are doing when you come across tutorials with this command in them. I was using one from a tutorial that looked like this:
avr-objcopy -j .text -O ihex some.o some.hex
That’s all fine and dandy except it only works for programs that don’t end up using the .data section of the object code! Now, this wouldn’t be a big deal if there were any warnings about this, but there aren’t – you just have to know to add a -j .data. A better solution, posted here by clawson, is to use the -R flag to remove parts you know you do not want. Here’s my current strategy:
avr-objcopy -R .fuse -R .lock -R .eeprom some.o some.hex
This fixed 2 separate problems when I did it, one with struct initialization and one with my LCD displaying block characters for strings but working for single characters.
I created an HD44780 LCD library for the AVR architecture today. There were a ton already, and mine is based off of one by Peter Fleury. I added a couple cool little features, and decided it needed to be on github to allow people to improve it and access it easier.
LCDiesel on Github
Here’s a quick code example using the library:
// Include the chars we want
/* initialize display, cursor off */
// Testing if x,y are set wrong
// Load character
// We better still be at 3, 1
See how easy it is to define custom characters? Cool, huh? I think so. The heart and rectangle bit vectors are in chars.h, and are only compiled in when they are defined, keeping code size down. Here’s how I have it hooked up on the breadboard (and the output of the program above):
Well, it’s a kit, so it’s kind of cheating. The other project was the Theramin, which I worked on for days. Anyway, this is a cute little kit you can buy for about $22 that, once assembled, can program pretty much any Atmel microcontroller IC. It’s pretty cool – for real. Anyway, it only took about 45 minutes (I was being careful soldering, could have had it done in 20 if I was hurrying). I’m very happy with the result – it lights up when I plug it into USB, and avrdude can talk to it. Now I just have to build a little header for my chip, and I can start breadboarding with it. The header pins are in the mail!