I'm building a digital compass using an AVR microcontroller and an HMC5883L magnetometer. There's a whole series of individual articles about the development of this project:

  1. I2C with ATtiny85 on Adafruit Trinket -- This article includes: building for the Trinket using the GNU toolchain (rather than the Arduino stuff), using the I2C bus, and talking to the 8x8 LED backpack.
  2. Magnetometer Reading to Compass Heading -- This one is all theory. I talk about how the magnetometer works, and an approach to turning the raw sensor readings into compass headings. Warning: math is involved.
  3. Magnetometer II: Real Data -- I gather some actual data using the HMC5883L and a Raspberry Pi, then see what happens when it put it through the transforms I proposed in the previous article. Lots of pretty graphs.
  4. Magnetometer III: Working Prototype -- Finally, an actual compass (of sorts), running on the Raspberry Pi. There's some talk about debugging, but the real meat here is the source code (available via a link buried in the article).
  5. Magnetometer IV: Code Size -- How in the world am I going to fit this on a microcontroller with 5.5KB of program memory? In this article, I talk about measuring the code size (and how to know how much each function contributes to the overall size), what effect various compiler options have on size, and how I might go about making things smaller.
  6. Magnetometer V: Fixed-Point Math -- This article provides another working example on the Raspberry Pi, this time using fixed-point math instead of floating point.
  7. Magnetometer VI: Working AVR Example -- At long last, working code on the actual target hardware. Not useful yet, but working.
  8. Magnetometer VII: Usable Gadget -- Saves calibration in EEPROM. Has a usable (albeit gimmicky) user interface. For science!

If you're trying to do something similar and want to get started quickly without having to read all the intermediate steps, article #8 is (currently) your best bet.