Monthly Archives: February 2011

Learn from my AVR mistakes

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.


LCDiesel: Yet another AVR HD44780 LCD library

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 <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "lcd.h"

// Include the chars we want
#define CHAR_USE_OPEN_RECTANGLE
#define CHAR_USE_HEART
#include "chars.h"

int main(void)
{
    /* initialize display, cursor off */
    lcd_init(LCD_DISP_ON);
    lcd_command(LCD_FUNCTION_4BIT_2LINES );
    lcd_clrscr();

    // Testing if x,y are set wrong
    lcd_gotoxy(3, 1);

    // Load character
    lcd_custom_char_p(0x00, _char_open_rectangle);
    lcd_custom_char_p(0x01, _char_heart);

    // We better still be at 3, 1
    lcd_putc(0);
    lcd_putc(1);
    lcd_putc(255);

    for(;;);
}

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):

Wiring

Parsing WFM Oscilloscope files in C

So, I got this Rigol Oscilloscope – I wrote a little instructable about it. One cool feature is the ability to save waveforms to a USB stick. There are many options for the file format, but the default is a WFM file. Now, a true minimalist could just use the CSV option, but the WFM is a binary format that contains a lot more information directly from the scope. I wrote a little C program hosted on github that allows you to convert those pesky proprietary files into something useful – namely a gnuplot image!

This is super alpha, and I’ll happily take patches. Cheers!

Apple Style Light Pulsing using ATTiny and PWM

I had quite a bit of trouble getting PWM to work on the ATTiny85 for some reason, but after many trials, I finally got it working and tweaked it so it acts a lot like the sleep light that you see on so many Apple notebooks. This chip costs about $1.50, and even less if you get it in SOIC rather than 8-DIP.

You can write code in C or Assembly – I prefer C – you can make cool macros, there’s a really nice libc for AVR chips, and it’s just plain easier.  Supposedly you could code in BASIC as well, but that’s so 1985.

Anyway, I hooked up this little chip to my breadboard and USBTinyISP programmer using some wires.  It was actually really simple.  I highly recommend the programmer – it was fun to make, works perfectly, and is really convenient in Linux – very plug and play with avrdude.

The code for my program follows:

This also shows how to do some debugging with SimAVR (all that commented stuff at the top). I couldn’t quite get it to work right, but there’s some great active development going on, and help available on Freenode#simavr. I highly recommend you try it out if you experiment with raw AVR code.

Overall, I’m really happy with the AVR MCU. I think I’ll be sticking with them as time goes on.

Switching to an energy-efficient desktop computer

Since 1990, I’ve had IBM-compatible desktop computers in my life. Before that, I had Tandy computers beginning at age 2. At first, they were the only computers I had, so I did everything with them – gaming, BBS surfing and eventually the internet, word processing, etc. In college, I got really into video encoding and I played all the newest games, which required quite a bit of PC hardware. Since then, I’ve done less and less encoding and gaming, and have settled into a state where all I do with my desktop machine is surf the web and watch HD movies and TV shows.

Now, decoding HD video isn’t exactly the easiest task ever. Routinely, playing 1080p video would peg one of my processor cores at 100%, and I felt that justified keeping the extra horsepower around. About a year ago, I started experimenting with VDPAU, which allows UNIX machines to offload video decoding tasks to the GPU. I bought a new video card basically just to mess around with this feature, and eventually I got it working very well. I’ve been using it for all my video decoding needs. Recently, I found out that YouTube and a couple other sites started supporting hardware video acceleration in their Flash movie players. Since I do actually watch a lot of video online, this was pretty much the last thing keeping me away from low-power CPUs. I found out about the Nvidia ION chipset, which supports VDPAU, and happens to come on a lot of Mini-ITX motherboards. Now, if I could find the right combination of hardware, it would finally make sense to try and trim down my desktop.

My system was a X3220 Xeon Quad core processor. I had 4GB of RAM, but I never touched most of it. I had 3 2TB hard disks in a RAID5, and a 120GB Raptor root drive. I also had a Blu-Ray burner, which brings us to a total of 5 SATA devices. I ended up buying a ZOTAC IONITX-A-U Atom 330 1.6GHz Dual-Core 441 NVIDIA ION Mini ITX Motherboard/CPU Combo for $179, one day before they lowered the price and the rebate came out, which makes it $155 today (lucky you if you choose to follow in my footsteps). It has 3 SATA connections, and 1 eSATA on the back, and about a million USB ports. I’m going to hook up one of my RAID HDs with the eSATA, all the other HDs with the regular SATA, and I bought a $10 converter for the Blu-Ray drive from SATA to USB.

This is where the fun begins. The day I received the board, I quickly stripped the RAM out of my desktop and put it into the little Zotac. Once I got everything hooked up (at this point, just the Blu-Ray and my root disk, along with my TV and monitor). I booted it, and it performed beautifully. There were exactly zero snags getting it to boot into Ubuntu. Once in, I got to work making everything work correctly with the Nvidia ION and VDPAU and Flash. I managed to get it all working with the Nvidia 260.19.06 driver and Flash 10.2 d151 32-bit. This is a bit of a complicated process, and will be much easier once Adobe makes 64-bit Flash support VDPAU. This is only available in the 32-bit beta at the moment, and all this went down around November 2010.

The process for getting all this working is just barely documented here and here. You basically have to install getlibs to download and install a 32-bit version of libvdpau, then download npwrapper, which wraps 32-bit plugins with 64-bit bindings to make it work in all your favorite 64-bit browsers. Yeah, it sucks, but hopefully you’ll only have to do it once.

I can play Big Buck Bunny in 1080p completely smoothly (as long as my connection can keep up) with only about 30% CPU usage! I can also play pretty much everything in mplayer with VDPAU smoothly. But how has my power consumption been affected?

Before the changeover, I measured my computer’s consumption with a Kill-a-watt meter for several days. Idling with everything else off, the computer consumed around 160W. My old processor’s max TDP was 105W, and the idle wasn’t pretty either. The new Atom 330‘s max TDP is only 8W! Just changing this one component brought my current idle power consumption down to about 60W according to the meter, a 100W savings. Since power costs around 10 cents per KWh, and my old computer was using 3.84KWh/day, it cost around $12/month to keep it on all the time. Now, it’s only about $4.50/month. Obviously, this isn’t a HUGE savings, but considering I don’t have a giant loud computer next to me taking up space for no reason, I think it’s kinda neat. Also, this brings my computer well within the realm of being able to run exclusively on solar power, once I get my panels and inverter installed.

Since I bought a Mini-ITX motherboard, I could complete one of the projects I’ve had in mind for about 3 years now – an old-school toaster machine. I’ve had this toaster body for several years, and I really like it. I cut out the back panel with a plasma torch – that thing cuts this sheet metal like butter. Mounting the board inside was difficult – I tried welding the standoffs to the frame, only to realize they were aluminum and promptly melted when exposed to my steel weld wire. Kelsey and I pondered for a bit, and came up with hot glue as a solution. We went to Michael’s (eww), and got supplies. I hot glued all the standoffs to the inside of the toaster and let it set up for 10 minutes or so. This worked very nicely. After checking about 5 times to make sure nothing was shorted, I hooked up everything, and hit the power pins with a screwdriver blade. It powered up! I’m typing on the system right now, and it’s purring quietly. I can feel the very slightest bit of heat coming out of the top. I’ll post pictures as soon as I figure out how I can hook up my USB card reader.

2 Projects completed within 24 hours: USBtinyISP Kit

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!

Flickr Tag Error: Bad call to display set '72157625847794027'

Error state follows:

  • stat: fail
  • code: 95
  • message: SSL is required

PetSafe Batteries: What’s really inside?

Well, I got an invisible fence kit for my dog, and found out the battery in the collar was dead. It has big letters on it that say DISPOSABLE, which was interesting – why is there no recharger for the batteries on this thing? That would seem to be logical. After checking online, I found out that the batteries were little 6-volt guys that cost about $3/each! That’s a little pricey – I wondered what was inside. Maybe it was one of those new-fangled fancy LIPO (Lithium Polymer) batteries, which would explain the high price. I was shocked:

Flickr Tag Error: Call to display photo '5413467671' failed.

Error state follows:

  • stat: fail
  • code: 95
  • message: SSL is required

So, you can find these coin cells on eBay for, oh, 14 cents each shipped to your door. That’s a 92% savings per cell.

So, assuming you can carefully (I was not careful above, but I will be next time) cut open the housing with your rotary tool (I used my new Proxxcon IB/E precision tool), and tape/glue it back together for insertion into the unit, you can make the cells yourself for 28 cents each instead of $3. Not too shabby. I think a coin cell adapter completely separate is the next step. I’m sure I can find something on the market that I could adapt to fit into the collar housing.

I got it back together, and even though I did a great job tearing the plastic apart completely, not being careful whatsoever, I was still able to snap it back into the housing. I tested the battery with a multitester and it read the same as it did yesterday. So, with a rotary tool, time, coin cells and glue you can reconstruct your own PetSafe batteries for 28 cents or less.

Flickr Tag Error: Call to display photo '5414122726' failed.

Error state follows:

  • stat: fail
  • code: 95
  • message: SSL is required

Pocket Theramin: Project Complete!

I finally finished my second real electronic project: The Pocket Theramin. I made a few changes/improvements to the design found here, and I really like my result. The basic circuit is exactly the same, but I added an LED and a toggle switch so I can keep it together as one unit all the time.

Also, I discovered that MAKE: Electronics is an awesome book. I highly recommend purchasing yourself a copy.

Flickr Tag Error: Bad call to display set '72157625843821033'

Error state follows:

  • stat: fail
  • code: 95
  • message: SSL is required