Introduction: Easy Atmel Xmega Sine Wave / Waveform Generator

About: I grew up in upstate NY where I speant many hours with my brothers playing with legos (I once attempted to test relativity by connecting a large number of gears to a small lego motor.. results inconclusive), m…

This is how you can use one of the BostonAndroid.com Xmega evaluation boards ($20)  to create a simple adjustable sine waveform generator with 12bit resolution and capable of producing frequencies from 125Hz - 20KHz using the integrated DAC. You can modify the code to produce any arbitrary waveform at up to 1MHz on 2 channels. You need only a PC, an eval board and a suitable Atmel programmer (AVRISP is under $30 new).

There are a LOT of features packed into the Xmega microcontrollers. This instructable is meant to demonstrate some of the really powerful ones like a 12bit DAC, 12bit ADC, DMA controller and open source compiler (avr-gcc).

Step 1: What You Need

You will need the following to complete this project:

Boston Android Xmega EVAL-01 Development Board ($20) or build your own
Murata PV36W103C01B00 Trimpot 10Kohm, 25turn (or equiv from Mouser)
PC with AVR Studio 4 (free)
AVRISP mkII programmer ($30)

Step 2: Theory of Operation

To create an adjustable waveform generator we will use a simple resistor voltage divider to be read by the Xmega on board 12bit ADC. This value will then be used to set the frequency of the DAC sine wave frequency but the following formula:

reading = (Vin/1.6V) * 256
freq = (reading * reading) + 1;

By adding an adjustable 3pin Potentiometer to PortA on the XMega-Eval01 we can create this simple voltage divider. You can also feed in your own voltage source between 0-1.6V to do the adjustment instead.

The waveform is output from the DAC with 50 samples per cycle at 12bit resolution from PORTB pin2. Pin1 is setup as ground reference.

Step 3: Schematic for EVAL-01 Board or Build Your Own

The instructions here are to simply buy the Boston Android EVAL-01 Xmega dev board. But you can also build your own by buying a TQFP-64 adapter board (check ebay ~$5) and buying an Xmega64a3 chip from Digikey or Mouser (~$8). Below is the schematic for the EVAL-01 board.




Step 4: Add Potentiometer

Solder the PV36W103C01B00 Trimpot to PortA pins 1-3 with the screw adjustment to the right.

Step 5: Add Test Wire Leads

Solder two wires to PORTB pins 1 and 2.

Step 6: Compile Firmware

Using Atmel AVR Studio 4:

1) Start Atmel AVR Studio 4
2) Select "New Project"
3) Select "avr-gcc" project
4) Give your project a name
5) Select "AVR Simulator 2" and target device: "Atmel AVR ATXmega64A3"
6) Get C source code file xmega-waveform.c from BostonAndroid website and save to your project directory and add to your project (or copy source code into the default C file created by AVR studio)
7) Go to "Build" menu and select "Build"


Step 7: Program Device

Connect 5VDC power supply to board. The easiest way is using a 5VDC wall adapter with center positive (you probably own one and don't know it; they are very common). If not, use an adjustable power supply or 3 AA batteries and connect to solder tabs at bottom of board. The contact close to the front of the connector is negative, the back one is positive.

Next connect the Atmel AVRISP mkii programmer to your PC USB port.

Next connect 6 pin ribbon cable between programmer and board as shown.

Open the software project in AVRstudio that you compiled in the previous step and load the software on the device by clicking on the "AVR" button in the toolbar (or also in drop down menu "program AVR device")

A dialog will appear which lists options like "Device" "Flash" & "eeprom". You want to program the flash memory on the target device. Select "..." under FLASH and locate a .hex file for your project, then click "Program". The .hex file name is .hex.

In a small box at the bottom of this dialog you should see lots of successful looking messages. :-)

Step 8: Use Device!

Connect an Oscilloscope (or freq counter, or high impedance speaker like a headphone speaker) across PORTB pins 1 & 2. You should see a sine wave of 3.3V p-p amplitude. Adjust the potentiometer clockwise to increase frequency from 125-20KHz. Turn counter clockwise to decrease frequency.

Step 9: What Next? What Else Can This Thing Do?

So, this is a simple example of how you can use the Atmel XMega microcontroller to generate sine waves. Here are some things you can do with minor changes to the firmware:

1) Go higher! The maximum frequency is limited by the 50 sample sine waveform and 1Msps DAC. You can modify the function LoadSineWave() and set the number of samples to be say 10: LoadSineWave(10). This will cause 5x more cycles. Now you can use SetWaveFreq() to go beyond 20KHz to 100KHz!

2) Arbitrary waveforms. LoadSineWave() loads a simple sine wave. But you can put any waveform you want into data12[] array. In C you can do this in the declaration:

    int data12[10] = {1,2,3,4,5,6,7,8,9,10};

3) Vary amplitude. The output waveform is 0-3.3V but you can make this smaller by scaling the data loaded into data12[].

4) Increase resolution of Freq adjustment potentiometer. The stock code uses the ADC in 8bit mode (256 discrete frequency settings). You can change this to 12bit and allow 2^12=4096 different frequencies