What Is It?

Alright, quiz time.  Can you identify the source of following picture?  Hint: it is the inside of a common electronic device (measuring 3″ x 4″):

Base Layer

No luck yet?  How about we add the second layer of circuitry:

Bottom Two Layers

Still uncertain?  Those are circuit traces you see, although not ones from a traditional circuit board.  These traces are printed on thin, clear plastic.  Oh, and you can ignore the black plastic bit in the lower right – that’s just a point through which a screw connects.  Alright, one final layer, then I’ll reveal the final image:

Almost Complete...

That’s a layer of semi-transparent rubbery material.  You’ve probably figured it out now, right?  Would it help if I told you those domes you see are buttons?  Well, try this:

It's a Keyboard!

Yep, it’s a keyboard.  To be specific, it’s my Logitech LX710 wireless keyboard.  Pretty exciting, huh?  It works quite simply: whenever you press a key, those rubber domes, which have a small layer of carbon on their undersides, contact the two layers of circuitry, which are separated by another thin layer of clear plastic.  These two layers, when pressed together by the rubber dome, complete an electrical circuit which tells the keyboard’s microcontroller unit (MCU) which key has been pressed.  The MCU then processes and transmits this data back to the computer.  End result?  This article.

Logitech LX710 Keyboard

Explaining the XMega XPlained (Dev. Board)

About two months ago, Atmel announced a smart new set of AVR development boards, the XPlained series.  One of these boards (which I’ve just recently purchased for $30) boasts a shiny new AVR XMega microcontroller.  What?  An XMega you say?  Why yes, haven’t you heard?  Come now, they’ve been around for fully three years at this point.  Well, don’t worry if this is fresh news, you’re not alone.  For some reason, adoption of the powerful new XMega MCU has been slow amongst hobbyists.

The XMega Xplained (click for high-res goodness)

When Atmel introduced their new XMega series of AVR microcontrollers back in 2008, I was pretty sure they’d be a quick sell.  Even in spite of their unavailability in DIP packaging, and this cheesy marketing video.  But sadly, a quick search of Hack a Day currently yields only four articles containing the term “XMega” (versus more than three hundred articles for “ATMega”).  I guess change is hard.  And yet, it’s really not.

The XMegas utilize the same AVR core as the ATMegas, and are fully supported by the free AVR-GCC compiler and AVR Studio (obviously, I guess).  Plus, you can still use your trusty AVRISP mkII programmer to load code onto an XMega.  In other words, the tool-chain is unchanged.  So, with a few register name changes, code from an ATMega will run perfectly well on an XMega.  Sadly though, the XMegas and ATMegas are not pin compatible, so you won’t be able to solder a new XMega onto your Arduino.

Alright, so for those of you keeping track, I guess I’ve now listed three strikes against the XMega which may help to explain its present unpopularity: no DIP packaging, no pin compatibility with the ATMegas, and changes to register names (plus the addition of new registers for new features).  Oh, and odd marketing tactics.  By the way, as a side note, it could be argued that the XMega register scheme is better than that of the old ATMegas.  They’re making good use of structs, so that you now address a port’s direction using PORTX.DIR, rather than DDRX.  And you output to that port using PORTX.OUT.

But let’s now take a look at the data in favor of the XMegas, yes?  To do that, we’ll compare the ATMega1280 (utilized on the Arduino MEGA) against the ATXMega128A1 (utilized on Atmel’s new XPlained development board):

Feature ATXmega128A1 ATMega1280
FLASH Memory 128KB 128KB
Max CLK Speed 32Mhz (PLL) 16Mhz
RAM Size 8KB 8KB
Voltage Range 1.6 – 3.6V 2.7V – 5.5V
ADC 16, 12-bit, 2Msps 16, 10-bit, 76.9Ksps
DAC 4, 12-bit, 1Msps N/A
USARTs 8 (one supports IrDA) 4
Hardware Encryption 128-bit AES, DES N/A
Timers 8, 16-bit 2, 8-bit and 4, 16-bit
Current Draw, 1Mhz, 1.8V 365uA 500uA
Event System Yes No
Price $10.20 $16.13

Not bad, right?  I’d say the XMega wins this round.  It’s faster, provides substantially better analog to digital conversion, offers digital to analog conversion (in other words, analog outputs – a feature not available on any ATMega), hardware-based encryption (again, not found on any ATMega), lower power consumption, and, wonder of wonders, a lower price.  I’m quite impressed (for what that’s worth) and am particularly excited about putting these new analog features to the test.  In fact, as I mentioned, I’ve just received my XPlained development board and have already written a quick test program to do ADC → DAC pass-through.  But I’ll describe my experiences with the XPlained board a bit later (spoiler alert: they weren’t all pleasant).

So what precisely does the new XMega series offer that makes it, in my opinion, such a substantial improvement over the ATMega series?  Let’s talk speed for a moment.  The old ATMega topped out at 20Mhz, at least officially (though overclocking is possible, as seen in the Uzebox gaming system).  But furthermore, there is no way to adjust the system clock on the fly (although you can, of course, adjust peripheral clocks).  You’d have to make fuse adjustments with an external programmer.  With the new XMega series, you can adjust the system’s clock frequency at run-time.  Both a 2Mhz and a 32Mhz internal RC oscillator are provided, plus a PLL which allows for clock multiplication (1x, 2x, 3x, …, 31x).  According to application note AVR1005, you can even use the PLL to increase the clock speed of your peripherals to a maximum of 128Mhz.  This might be useful for generating high-resolution PWM signals, for computing precise time intervals (think range-finders), or for just blinking an LED really really fast (although perhaps not this fast).  Man, I could’ve used this on my MS thesis

Another neat feature of the XMega series is the brand new event system which allows for high-speed signaling between peripherals.  This is not a communications bus in the traditional sense.  It’s actually more like a set of, shall we say, “personalized” interrupts sent between features.  Event signals can be sent quite rapidly – in no more than two clock cycles – and don’t require the CPU to be active.

The XMega Event System

The XMega’s event system opens up a whole new world of possibilities.  With it, you could tie a set of 16-bit counters together to form one highly-accurate 32-bit counter.  Or, how about this application note example:

You could use one event to synchronize two modules. For instance, you could use a pin change event to do an ADC conversion and an input capture on the Timer/Counter to get exact time-stamps for each conversion.

For more details on the event system, see the “Getting Started with XMega” application note AVR1005 or the “Getting Started with the XMega Event System” note AVR1001.

First Impressions of the XMega XPlained Dev Board

Alright, well let’s get down to business here.  As I mentioned earlier, I’m now the proud owner of a brand new Atmel XMega XPlained development board.  I was putting in an order with Digi-Key the other night when I thought to search for XMega products (yes, shame on me, I haven’t tried them out until now).  I found just one, but it was precisely what I was looking for: low cost ($30.16), USB-powered, and covered in blinking lights (well, nine of them anyways).  So of course I bought it.  I mean, it’s not quite as much of a steal as the $4.30 TI Launchpad, but even at $30, I didn’t even bother to do research before adding it to my cart.   I just figured it would work.  🙂  Bad idea, I know…

So what did I get for my money?  A pretty padded box and the board itself.  Nothing more.  No documentation whatsoever, only a printed messages on the outside of the box requesting I go online for any required drivers and data.

XMega Xplained Unboxing

Now the lack of paper is fine with me; if Atmel wants to save some trees, good for them.  I’d have gone online for datasheets and schematics anyway.  My only gripe here is that Atmel’s site isn’t all that easy to navigate.  In fact I don’t think I ever located a link to the ZIP file associated with AVR1927 (instead I just crossed my fingers, manually typed in the assumed link, and bingo).  But maybe I’m just bad at the internet.  Well for the sake of centralization, here are a few URLs I found helpful when getting started:

The XMega XPlained comes pre-programmed with a cute little application that blinks its nine LEDs and plays different sounds (drums, trumpets, etc. – just one- or two-second clips) when each of the eight different buttons are pressed. And I’ve got to say, the little speaker actually impressed me with its sound quality.  It’s not exactly a M-Audio studio monitor, but it’ll probably hold its own against a speakerphone.  And it’s certainly not being driven by square waves; they’re making good use of the XMega’s analog outputs (DACs).  So what else does the XPlained offer?  Well, here’s the official list:

  • External memory (8MB SDRAM, MT48LC16M4A2TG)
  • Atmel AT32UC3B1256
    • Communication gateway
    • Programmer for Atmel AVR XMEGA
  • Analog input (to ADC)
  • Analog output (from DAC)
    • Mono speaker via audio amplifier
  • Digital I/O
    • UART communication through USB gateway
    • 8 mechanical button switches
    • 8 LEDs (plus one bi-color LED)
    • 8 spare analog pins
    • 24 spare digital pins

Programming the XPlained using FLIP

So the pre-loaded software entertained me for about sixty seconds, after which my desire to reprogram the board overcame my fascination with lights and sound effects.  I didn’t have my AVRISP mkII handy (left it at work again), so I started by looking into reprogramming via the board’s USB connection.  The first thing I needed was a driver for the virtual COM port (Windows 7 did not recognize the XPlained), a single INF file:

USB CDC Driver (Virtual COM Port) – required for USART communications.

I was pleased to find that the instructions provided in the “Getting Started” guide (AVR1927) for using the Flexible In-System Programmer (FLIP) for RS232 programming were quite simple.  I downloaded and installed FLIP via this link.  I also had to import an XML configuration file (provided here), although it sounded like this file should have been included in the latest FLIP installer.  But before adding this file to <Install Directory>Flip 3.4.xbinPartDescriptionFiles, I received an error stating that “the device does not exist” when using BatchISP (the FLIP command line utility).  I also attempted to use the FLIP GUI directly, but for some reason the RS232 communication option was greyed out.  No problem though, I simply threw the programming commands given in the instructions into a simple batch file:

batchisp -device ATXMEGA128A1 -hardware RS232 -port COM25 -baudrate 115200 -operation onfail abort memory flash erase f blankcheck loadbuffer c:xmegatestdefaultxmegatest.hex program verify start reset 0


I’ve highlighted the elements you’ll want to change when using this file.  Including that pause command causes the command window to wait for you to press a key before closing, that way you can take a look at the results of your programming attempt:

FLIP (BatchISP) Command Line Programmer

So using BatchISP (FLIP) worked just fine for me.  The whole programming process took a bit longer than I would have expected (maybe 20 seconds), but it’s not terrible.  The one catch is that you have to unplug the XPlained board, and then plug it back in while holding down switch SW0, every time you want to reprogram.  This is required in order to activate the bootloader.  Doing this gets old fast, and it didn’t seem to please my computer (it would occasionally freeze for a few seconds when the device was quickly plugged back in).  But there is an easier way; keep reading…

Programming the XPlained using the AVRISP mkII

So based on the literature I’ve run across, it seems the preferred means of reprogramming (and debugging) an XMega is via JTAG using either the AVR Dragon ($50), the AVR JTAGICE mkII ($300), or the Cadillac of debugger/programmers, the AVR ONE! ($600 – perhaps this is the reason for the exclamation point).  The new XMega series uses the PDI (Program and Debug Interface) programming interface (as opposed to ISP).  However, it is possible to use the AVRISP mkII programmer (though you cannot use it for debugging), which costs just $35.  And if you’ve done anything with AVRs in the past, you’ve probably got one of these hanging around (I should note that the original AVRISP, the one with the DB9 port, will not work with the XMega series).

Now, to get your AVRISP working with the XMega Xplained, you’ll need to create a simple pin adapter.  You cannot connect directly to the board as the pins are arranged for a 10-pin JTAG connection (I guess Atmel really wants you to use JTAG).  However, you’ll only need to connect four of the six pins present on your AVRISP, as shown in this diagram (found on page 9 of the XMega “Getting Started” guide, AVR1005):

AVRISP Pinout Comparison

These pins can be found on the XPlained’s JTAG connector, as shown in the schematics:

XMega XPlained JTAG Connector

Once you’ve made these connections, you can use AVR Studio to reprogram your device as usual.  Well, almost.  First, you need to make sure it’s fairly up-to-date (I used version 4.18, build 700 with success, but you might go straight to AVR Studio 5, which I’ve also tried with success).  You’ll then need to manually specify the ATXMega128A1 before programming or adjusting fuses.  Plus, and this is key, you’ll want to disable the JTAG interface by using AVR Studio to clear the JTAGEN bit on the fuses tab.  If you don’t, your programming may or may not be successful.  I actually got into an interesting cycle where alternate programming/read events would fail.  I’d perform one operation successfully, but on the next I’d see “Entering programming mode…FAILED.”  But disabling the JTAG interface took care of this issue.  And doing so does not prevent you from re-enabling JTAG later, or from using BatchISP (though you may need to reload the bootloader if you’ve erased it, which may be found in this ZIP).

For more details on programming and debugging an XMega, see this article (and actually, check out all of these “Getting Started with XMega” articles, they’re quite good).

A First Test of the Analog I/O

Now I think I’ve stated this already, but again, I’m pretty excited about the new analog I/O offered on the XMega series.  In particular, the 12-bit digital to analog converters (DACs) open up a whole new world of options.  I mean, there are all sorts of applications out there that might benefit from an on-chip DAC: function generators, analog power supplies, audio processors, lighting controls, you name it!

So the first bit of code I wrote for my XMega is a simple ADC → DAC pass-through.  It’s a touch long to include in this post (because of all the comments), but please feel free to download it here.  The code takes an analog input on ADCA1 (PORTA1, pin 96) in the 0-2.1V range and outputs a proportional analog signal on DAC0 (PORTA2, pin 97) in the 0-3.0V range.  Here’s a screenshot of the results taken using my RPI IOBoard and LabVIEW interfaces.  The bottom graph (red line) shows the sine wave signal being generated by the IOBoard and connected to the XMega’s ADC input.  The top graph (white line) shows the scaled sine wave being measured at the XMega’s DAC output:

XMEGA ADC-DAC Pass-through Test

You may be wondering: why the difference in input and output voltage ranges?  Well, here’s one additional problem I see with the XMega: Vcc (max 3.6V) is not directly available for use as a reference for the A to D conversion, only Vcc/1.6V (which is 3.3/1.6 = 2.0625V, in this case).  Now you can select AVcc (typically tied together with Vcc, perhaps via a filter) as a reference for the DACs, although according to the hardware datasheet, both ADC and DAC reference sources are limited to AVcc-0.6V, or 2.7V on a 3.3V source (which is what you get on the XPlained development board).

Now in testing these specifications, I have found the ADC limit to be fixed as stated, although when I’ve selected AVcc as the reference for the DACs I’ve seen max outputs reach just over 3V.  Honestly, I don’t know what prevents the XMegas from using Vcc as a reference, as this is commonly done with ATMegas.  Oh well!  You just may need to throw in a voltage divider and/or op-amp to compensate.

One other issue I noticed was noise in the ADC signal when measuring values near 0V.  This could be an issue with how I’ve setup my code, or with some other aspect of my hardware.  But you can see this effect in the slightly garbled low points of the sine wave shown in white in the above screenshot.  I guess the bottom line is that I’ll need to play around with this a bit more.  (NOTE: I believe we have solved this issue; it is a problem with the XMega chip itself.  The solution is to use a lower (e.g. 1V) reference for the DAC.  See comments section below.)  Apparently there are also calibration registers for the ADC, and some pretty advanced tweaks you can make.  Take a look at this page on configuring and tuning the XMega ADCs – it’s been a great help to me already.


Overall, I’m cautiously optimistic about the XMega and the XPlained development board.  I’ve encountered a couple of minor issues, and the list of problems in the errata section of the datasheet is frighteningly long.  I should also point out that a previous version of the XPlained, the XPlain, apparently had quite a few more serious issues.  You’ll find references and pictures of the XPlain if you do a bit of Googling.  I’m not sure who’s still selling it, but I can tell you that despite the picture and the name, Digi-Key is shipping the newer XPlained, not the old XPlain (this is where I got mine).

So I still say that the XMega a great leap forward by comparison with the ATMega series.  The only question left is what to do next?

I’ve been thinking about going further with this ADC-DAC application and creating an audio compressor and volume control.  You see, I’ve got this cheap portable speaker that I use with my Blackberry for listening to MP3s.  The trouble is, there’s no remote, and each track seems to play at a slightly different volume.  So I’m thinking of using the XMega to receive IR signals from a remote and then adjust the volume accordingly (by scaling the ADC result before sending it to the DAC).  And at the same time, it could automatically adjust volume, based on the incoming audio signal, within a certain range.  This is called compression.  My setup would require a bit of analog work to get the signals into the correct voltage ranges before and after processing, but a couple of op-amps would likely do the trick without much work.

That said, I’m open to other ideas.  Has anybody out there got suggestions for projects?

The EEWeb Forums

EEWeb LogoSo I don’t often plug websites, but lately I’ve been browsing EEWeb and have found it to be quite a useful and even entertaining resource.  You’ll find a wealth of informative articles on topics ranging from power management to PCB design, plus current news items.  Perhaps most interesting, however, is the EEWeb Forum, where anyone can pose queries related to electrical engineering (example).  Ask a question, and you might get responses from practicing engineers, hobbyists, or even professors.

Plus, as I mentioned, EEWeb even offers a bit of amusement to liven up your day. Check out their web comic, “Return to Zero.”  If you’re an EE, I think you’ll get a laugh or two.

LabVIEW: Storing Diverse Data

Well it’s hard for me to believe, but I’ve now been using National Instruments LabVIEW for six years.  I started off with LabVIEW v7.1 and have used every version since (it’s now called LabVIEW 2010).  So I’m definitely a fan.  And yet, I’m still discovering new and more useful features on a fairly routine basis.

Just the other day I was looking for a convenient means to save a ton of front panel control values to a file (to allow the user to save their current program setup for later use).  In the past, when saving program settings, I’ve used the Configuration File VIs, which make saving simple parameters very easy and neat (think header-based *.ini files).  However, in this case I needed to save an obscenely large quantity of very variable data (numerics, strings, booleans, arrays, clusters, clusters of arrays, arrays of clusters, etc…).  After a bit of Googling I found my answer, the magical Datalog VIs (located on the Programming → File I/O → Adv File Funcs → Datalog palette):

The Datalog Palette

The Datalog VIs allow you to save clusters of any sort to a compact, record-based file.  Of course, you aren’t required to make use of the record functionality, you can just store everything in a single record and then recall that record later.  For instance, I packed the following mass of controls into a single datalog file with just a few simple blocks:

Datalog VIs In Action

Reading from a datalog file is just as simple, you need only swap in a read block in place of the write block.  Here’s a bit more on the Datalog blocks, taken from NI’s website:

A datalog file stores data as a sequence of identically structured records, similar to a spreadsheet, where each row represents a record. Each record in a datalog file must have the same data types associated with it. LabVIEW writes each record to the file as a cluster containing the data to store. However, the components of a datalog record can be any data type, which you determine when you create the file.

For example, you can create a datalog whose record data type is a cluster of a string and a number. Then, each record of the datalog is a cluster of a string and a number. However, the first record could be (“abc”,1), while the second record could be (“xyz”,7).

Using datalog files requires little manipulation, which makes writing and reading much faster. It also simplifies data retrieval because you can read the original blocks of data back as a record without having to read all records that precede it in the file. Random access is fast and easy with datalog files because all you need to access the record is the record number. LabVIEW sequentially assigns the record number to each record when it creates the datalog file.

So there you have it!  If you’re looking for an easy way to store diverse data, the Datalog VIs may be your answer.  Oh, and if you’ve got no clue what graphical programming with LabVIEW is all about, check out NI’s free 30-Day LabVIEW Trial.  The trial is full-featured; the only catch is a small watermark placed in the lower right-hand corner of your VIs.  But give it a shot!  It takes a little getting used to if you’re a die-hard C programmer, but once you understand the basics, you’ll be churning out sophisticated programs in a fraction of the time required to type code.

Hard Drive Failure…

So just FYI, my server’s hard drive partially corrupted last week.  I have no idea why.  I was able to peer through my log files and saw nothing unusual.  So, my apologies for the downtime Wednesday through today.  But, obviously, we’re back up and running now, albeit on a slower, less reliable connection (boo-hiss Mediacom).  Still, props to StorageCraft ShadowProtect for the quick recovery.

Update (7/17/2011): So apparently it wasn’t just software; my drive began to fail again yesterday evening.  It has now been replaced with a nearly identical model which, hopefully, will last longer than ten months.  By the way, has anyone else pondered just how much time they’ve spent fixing computers?  I mean, if you sum it all up, it’s got to be solid months of time.  Not that it’s time wasted, but, well, it sure feels like a waste…