Atmel Xmega USB/Serial Arbitrary Waveform Generator

102,419

44

36

I grew up in upstate NY where I speant many hours with my brothers playing with legos (I once att...

Intro: Atmel Xmega USB/Serial Arbitrary Waveform Generator

This instructable walks you through programming and using the Boston Android Xmega evaluation board to work as a simple arbitrary waveform generator taking advantage of the integrated 12bit DAC and high speed DMA controller. I have provided precompiled firmware as well as source code which can be loaded onto the device and configured via a USB port on your PC or laptop.

You will need a computer (mac, pc, linux all supported) with an available USB port, free driver for the USB-Serial adapter, a Boston Android Xmega EVAL-USB-64 development board ($31.99) and an open source AVR chip programming utility called AVRDUDE.

Once set up, you can generate sine, ramp, triangle, square or any waveform you define from 5 to 20KHz with amplitude of up to 3.3Vp-p. It is possible to generate faster waveforms with the arbitrary waveform feature (up to 500KHz).

Step 1: What You Need

1) Boston Android Xmega EVAL-USB-64 (or comparable) Development Board
2) PC with USB port
3) AVRDUDE (free AVR programing software)

Also recommended:

Oscilloscope (to view waveforms)
Headphone or small speaker (to listen to waveforms)
WINAVR and AVRSTUDIO to modify the source code and built your own version of the waveform generator software

Although this example walks through programming and using the EVAL-USB-64 board, you can use any of the EVAL-USB boards from Boston Android. And if you're very experienced you can download the example source code and build for other target hardware such as the Atmel XPLAIN evaluation board, or the Xmega boards available from chips45 and Mattair, Megavore. The xmega family allows effortless migration of code from one chip to another.


Step 2: Solder Lead Wires (optional)

Although not necessary, it is helpful to solder lead wires to your board to make it easy to connect an oscilloscope or frequency counter to observe the waveform output. The waveform output is on PORTB pin 2 and pin1 is configured as GND.


Step 3: Installing AVRDUDE

AVRDUDE is available as a standalone source distribution which you can compile with the GCC compiler on a number of operating systems (namely Linux).

However, if you use a PC running Windows there is a complete binary distribution (no compiling needed) called WINAVR which includes AVRDUDE, along with the AVR-GCC compiler which will allow you to modify and compile your own version of the waveform generator.

Download WINAVR (includes AVRDUDE)

Then install it.
 

Step 4: Install USB/Serial Drivers

The EVAL-USB-64 board includes a built in usb/serial bridge IC which creates a virtual serial port on your PC. Most Windows, Linux (and probably Macs) include the necessary drivers and you don't need to do anything. However, if your system complains it needs drivers go here and download the VCP drivers for your platform:

Download VCP Drivers

Step 5: Download Waveform Generator Application

If you are using the EVAL-USB-64, you can download the waveform generator .hex file and move on to the next step

Alternative Path:

If you are using an alternative platform you will need to download and compile the source code. All the code is in a single .c file which is written to be compiled with AVR-GCC. If you are running on a Windows PC you can download the following two packages and compile and program the application on to your board.
1) Download WINAVR
2) Download AVR Studio IDE
3) Create new avr-gcc project in AVR Studio and paste the source code into the initial .c file
4) Compile (creates .hex file to load onto target device)

Step 6: Program Board

The EVAL-USB-64 board contains a serial bootloader which allows your board to be programmed with avrdude.
1) Plug your board into the usb port of your PC.
2) Open a command line window.
3) Locate the hex file you downloaded xmega-waveform-serial.hex
4) Within 10 seconds of plugging in your board, execute the following command to load the program image.

avrdude -p x64a3 -c avr911 -P COM5 -b 57600 -e -U flash:w:xmega-waveform-serial.hex

Your com port may be different. To be sure, in windows go to the Device Manager and check "com ports" to see what port your board is connected to. It is almost always the highest numbered com port on your system since the builtin com ports are assigned COM1-4 typically. On my laptop it comes up as COM5 but your computer will likely be on another numbered port.

Alternative Paths:
If you have an Atmel AVRISP mkii or JTAGICE mkii or compatible device you can use Atmel's AVRStudio to program the .hex file you downloaded in the previous step. See Atmel's documentation for using this utility.

Step 7: Connect to Waveform Generator Over Serial Port

Connect your board to your PC with a standard USB/miniB cable.

Open a serial terminal to the board. Under Windows XP you can use Hyperterm and open a connection to the COM port your device is on (you should know your COM port number from the previous step). Under Vista/7 you can use TeraTerm. The serial port configuration is 9600 Baud, 8N1, no flow control.

After you connect you should be able to type 'h' which will print out the usage instructions for the waveform generation utility.

Step 8: Generate 1KHz Sine Wave

In your serial terminal enter the following command:

s 1000

You should be able to measure 1KHz waveform on your oscilloscope or frequency counter. Alternatively you can hook up a speaker or headphone and hear a low constant tone.

Step 9: Generate 1KHz Ramp, Triangle and Square Waves

Okay, Sine wave is easy to generate without a fancy 12bit DAC. Let's generate some other waveforms including Ramp, Triangle and Square waves:

r 1000
t 1000
S 1000






Step 10: Frequency Limits

Although you can enter any number you want, for the standard waveforms (sine, square, etc) you should limit the frequency to 5Hz-20KHz. For example 20KHz sine waveform is shown below. To go beyond 20KHz you can use the arbitrary waveform function in the next step to create your own waveform with fewer datapoints (the dac is limited by how fast you copy data to it, the fewer datapoints in your waveform the faster you can go).

Step 11: Arbitrary Waveforms

Okay, let's generate our own waveform. To do this you use the "w" command and you will need to specify the number of datapoints, the frequency the waveform is output and then the data itself:

w [freq] [num data points] [data1] [data2] ... [dataN]

Here is an example arbitrary waveform which is a square wave with a trapezoidal shape repeated at 1KHz and 20 data points:

w 1000 20 0 0 0 0 0 0 0 0 4095 4050 4000 3950 3900 3850 3800 3750 3700 3650 3600 3550

Note the range of data point values is 0 - 4095 (12 bit resolution)

Once the arbitrary waveform is loaded you can change the frequency without re-entering all the data. To keep the same waveform as above change the frequency:

w 10000


Step 12: Number of Cycles

By default when you configure a waveform it is played back indefinitely. But you can also specify a small number of cycles. Here's how you would generate two cycles of a sine wave at 10KHz.

n 2
s 10000


This works for arbitrary waveforms as well. To go back to infinite cycles enter the following:

n 0



Step 13: Connecting a Speaker

You can connect a small speaker (or headphones) to the circuit in order to hear what your waveform sounds like (or determine your hearing range). You will want to put a blocking capacitor in series to prevent any DC current from the DAC and to create a bipolar (positive and negative swinging) signal.

1) Solder a 47uF(or whatever you have handy over 1uF) electrolytic capacitor between PORTB header pin 3 and one of the leads to your speaker.
2) Solder the other speaker lead to PORTB header pin 2 (the Gnd pin)

Electrolytic caps are polarized; make sure you connect the positive end of the cap toward the board (PORTB pin 3)

Now set some outputs and listen to them:

s 1000
s 2000
r 1000
S 100
s 100
t 100

s 16000
I can hear 16KHz sine wave, but above that it's silent..

USE EAR PLUGS WHEN YOU OPERATE HEAVY EQUIPMENT AND POWER TOOLS KIDS! :-)


Share

    Recommendations

    • Electronics Tips & Tricks Challenge

      Electronics Tips & Tricks Challenge
    • Audio Contest 2018

      Audio Contest 2018
    • Plastics Contest

      Plastics Contest

    36 Discussions

    Hi!

    (sorry for my english)
    It's a great site and instruction but c source file is missing. I will be very grateful for reload this file.
    Best regards - piotr

    1 reply
    0
    None
    arunp4

    3 years ago on Step 3

    My usbasp(atmega8) having an error,How can i solve it?
    Executing: "C:\Users\god\AppData\Local\Temp\RSIF5F2.tmp" -p m8 -c usbasp -C "C:\Users\god\AppData\Local\Temp\RSIF5F3.tmp" -P usb -U flash:w:"C:\Users\god\Desktop\TimerSchool.asm":i

    RSIF5F2.tmp: AVR device initialized and ready to accept instructions

    Reading | ################################################## | 100% 0.02s

    RSIF5F2.tmp: Device signature = 0x1eff1e
    RSIF5F2.tmp: Expected signature for ATMEGA8 is 1E 93 07
    Double check chip, or use -F to override this check

    hi, i'm from México. this instructable is very interesting for me, because i need to construct an ultrasonic frequency generator. i actually work in a project of the called smart windows, and i use a ultrasonic frequency generator and an atomizer nozzle in order to deposit thin films of NiO on ITO substrates. The generator that i'm utilizing is on the range of 0-130 kHz. so, i need to know if this instructable could be useful to me for construct my own generator with those characteristics. Thanks, César.

    0
    None
    Wuguisam

    4 years ago on Introduction

    Hi wandererwolf, Iare quieted to generate 20khz sine wave using xmega128a1 board. I read one of your post that your code might work with xmega128a1 board. Can I have your code and project files? I would like to give it a try. Thanks.

    0
    None
    numeratus

    5 years ago on Introduction

    This is an excellent instructable; the arbitrary waveform generator specifications are quite impressive for a such a low cost of implementation. I'm writing because Boston Android is no longer making development boards, and I'm not seeing alternatives via a Google search, other than a $199 Atmel development kit. Could you recommend a dev board?

    1 reply
    0
    None
    wandererwolfnumeratus

    Reply 5 years ago on Introduction

    Check out the boards from Mattair Tech (http://www.mattairtech.com/). They offer various Xmega development boards and the code should be portable to their boards with a little effort. Let me know if you need help; I can send you the source code and project files.

    0
    None
    catxld

    7 years ago on Introduction

    how would I boost the voltage amplitude of the frequency from 3V to 20V? if it uses opamps then what opamps would be best for boosting voltage which would cover all or most of its frequency ranges from 1hz ~ 500khz?

    5 replies
    0
    None
    wandererwolfcatxld

    Reply 7 years ago on Introduction

    The output of the Xmega DAC can generate 0-3.3V and a few mA of current. To get higher voltage you will need (as you suggest) an opamp (low current only) or a linear amplifier circuit capable of driving your load.

    What are you attempting to drive up to 20V? Is it a speaker? Any requirements for efficiency? Do you have a 20V supply to drive the output?


    0
    None
    catxldwandererwolf

    Reply 7 years ago on Introduction

    Basically I would be using it for all kinds of experiments, speaker, radio,EM , even experimental motor. but id be using the arbitrary waveform feature in most of these experiments, im wondering what is the range of the arbitrary function is it 140hz to 500KHz?

    0
    None
    legionlabscatxld

    Reply 5 years ago on Introduction

    An LM386 would be a cheap and easy way to amplify the lower end of that frequency spectrum, and it can output a reasonable amount of power (I seem to remember 0.5 watts for SMD and 1 watt for the DIP version).

    An OPA132 has a GBP of 20Mhz (gain of 40 at 500khz)... and if I recall can output 25 millamps, which is pretty good for an opamp of that bandwidth. They're nice and stable too, but like 6$ a pop.

    Those are the simplest solutions that come to mind.

    0
    None
    wandererwolfcatxld

    Reply 7 years ago on Introduction

    In theory the DAC can run up to 2MHz, but the example code here can only reach about 100KHz. For 500KHz you will be limited to square waves and need to write your own code to drive the DAC that fast.

    As for the output stage going up to 20V you should do a search for linear amplifiers. You could probably just use an Audio Amplifier circuit. They have some at http://www.sparkfun.com/ which can boost the signal output from this board to the power levels you're talking about. I've used an amplifier off ebay (search for TA2024 amplifier about $9) to generate high frequency signals to drive a small speaker. Don't ask why. ;-)

    0
    None
    wandererwolfcatxld

    Reply 7 years ago on Introduction

    You could definitely use an opamp amplifier circuit to boost the output voltage to 20V over that frequency range. The only limit of an opamp is that it generally is only used for low current applications. For example you wouldn't use an opamp circuit to generate a signal to drive a motor, etc.

    If you do a web search there are lots of example opamp circuits to choose from. I would breadboard them and try them out.

    Best of luck!

    0
    None
    wandererwolfkosmo!!

    Reply 7 years ago on Introduction

    Atmel worked very hard to make all the Xmega chips in the family derivatives of the Xmega128A1. So for example an Xmega128A3 just has fewer pins and fewer peripherals, but the ones it does have reside at the same addresses as the bigger Xmega128A1 chip. They also arranged memory at the same start addresses regardless of memory size. In theory all this means binary compatibility between chips (as long as the peripherals are available), so you MIGHT be able to load compiled code for the Xmega64A3 on to an Xmega128A1 and have it work, but there may be some catch. Certainly worth a try!

    0
    None
    kosmo!!wandererwolf

    Reply 7 years ago on Introduction

    Ok, thanks for fast support. But I have an another question. Im using an STK600 board and I have no serial terminal. Which parameters can change the frequency? I need 20 khz sinewave.

    0
    None
    gustep12

    7 years ago on Introduction

    Very nice, thank you. Do you think it would be possible to extend this design to use an external 16-bit DAC, like the TI DAC8830, for improved 16bit resolution and better drift stability? Most DACs use SPI as the interface, what would it take to hook these up?

    1 reply
    0
    None
    wandererwolfgustep12

    Reply 7 years ago on Introduction

    There are several dedicated SPI interfaces on the Xmega chip you could use to interface with an external DAC. You can also configure the USARTS on every port to master SPI mode.

    If you need more help let me know.