Are you prepared for Wrath of the Lich King? WoW Insider has you covered!

Make a talking MSP430 microcontroller

talking msp430This article continues a series about building a DIY digital audio recorder. Inspired by this microcontroller audio project [via], I set out to build a simple digital recording device. I chose Texas Instrument's MSP430 microcontroller for this project because it's fast (16 MHz), it's cheap ($1), and it's very low power. Read the first part, and the second part.

This week we'll progress towards a digital audio recorder by playing audio files from a SD memory card. First, we'll convert an audio file to a raw format and image it directly to a SD card. Then, we'll interface the SD card with the MSP430 and play an audio file. See it in the video:


Next time we'll extend this basic circuit to include a microphone and audio recording capabilities.

Read on to learn more about generating audio with a microcontroller.

next page

Gallery: MSP430 audio output

MSP430 audio prototypeProject overviewSD card SPI interfaceSD card data layoutPlaying a raw audio file

Make a talking MSP430 microcontroller - part 2


Overview

Read the previous article in this series for the fundamentals of microcontroller audio. Learn about basic MSP430 connections and programming in the MSP430 primer. This week we'll use the tiny, inexpensive MSP430 microcontroller to play audio from a SD card.



Playing an audio file
In the previous project, I used the MSP430 pulse-width modulator (PWM) to generate simple tones. The PWM duty cycle changed the frequency, and thus tone, of the generated signal. The signal is cleaned by a simple audio filter, and amplified by powered PC speakers. Hear it in this video clip from the previous article:




Each value generates a different frequency. If we change the values about 8000 times a second (8KHz) we can reproduce telephone quality audio.

8 bit audio (each sample requires one byte) at 8KHz requires 8000 bytes of data per second. 16 bit audio, the quality associated with CD players and PCs, requires twice as much data per sample (16 bits or 2 bytes) -- 16 kilobytes/second.

With 2K of internal flash, the MSP430 can store about one-quarter second of audio. External memory is needed to store any meaningful amount of audio. There's no better source of cheap storage than flash memory chips. A raw flash memory chip would be a special order item, but you probably already have some flash memory -- SD cards! These cards are ubiquitous in digital cameras and low-end MP3 players.
next page

Gallery: MSP430 audio output

MSP430 audio prototypeProject overviewSD card SPI interfaceSD card data layoutPlaying a raw audio file

Make a talking MSP430 microcontroller - part 3


Working with SD cards

NOTE!!! We are going to use the SD card as flash memory. It will not have a full (FAT) file system, and can not be read on a PC by the usual methods. It will not be possible to drag and drop a raw audio file to the card and read it with the MSP430, nor will it be possible see recordings from the file browser on a Mac or PC. Special disk tools provide access to the raw disk, but a full FAT file system implementation is too big for the tiny microcontroller. The F2013 has 128 bytes of RAM and 2 kilobytes of flash. A workable FAT implementation requires 1024 bytes of RAM and several kilobytes of flash program space.

The prototype design accesses the SD card using the simple SPI protocol. SPI is a simple three wire interface that allows a microcontroller to read and write data from external chips. If you want the dirty details on how the SD card works in SPI mode, read some of these tutorials [Tutorial 1, Tutorial 2]. A source code library from TI provides easy access to the disk without writing our own initialization and read/write routines (mmc.c/mmc.h in the project archive).



SD card storage is divided into 512 byte chunks called sectors. A full 512 byte sector must be read or written at once. The MSP430F2013 has only 128 bytes of RAM -- not enough to hold a whole sector for random access. We can still read/write each byte in order, as it's needed. This method will suffice as long as each byte is read or written sequentially.

Playing an audio file is as simple as reading a byte from the SD card and writing it to the PWM duty cycle register.

Converting audio to a raw format
My 'hello world' audio clip comes from the intro to my videos. I converted the clip to an 8KHz .wav file with Audacity and then saved it to raw text file with the free version of Switch, as described here.
  1. First, install Switch.
  2. Start Switch, and add your desired files to the queue by clicking "add files".
  3. Now set the output format to ".raw", and then click "Encoder Options". Set the encoder format to "8 bit unsigned", sample rate to "8000", and channels to "mono".
  4. Now click convert and the raw version of the audio files are created.
The raw text version of the audio file is included in the project archive.
next page

Gallery: MSP430 audio output

MSP430 audio prototypeProject overviewSD card SPI interfaceSD card data layoutPlaying a raw audio file

Make a talking MSP430 microcontroller - part 4


Imaging an SD card

The MSP430 isn't powerful enough to deal with a file system, so we need the audio to start exactly at the first sector the of SD card. A simple copy->paste operation would put the file on the SD card according to the rules of the PC file system. To avoid this, we image the file directly onto the card. This will be familiar if you've ever 'imaged' a CD .iso file, say, for a bootable Linux live CD. I used DiskImage 0.9 from Durban.

Imaging the audio file onto a flash card was pretty easy with DiskImage. I put the SD card in a USB flash card reader and attached it to the USB port. I opened DiskImage and it detected the card as a logical and physical volume.
  1. It's crucial to deal with the disk directly, rather than through the file system. All operations should be done to the listings under "Physical drives - raw drive, independent of partitions" box.
  2. Find the physical partition that represents your SD card - it's easy to identify the correct physical drive because it should be type "Removable" and the correct size (in my case 14MB). Be sure you don't overwrite your actual hard drive, this utility can easily do that!!!
  3. Click on the correct drive to highlight it.
  4. In the "With selected item do" box, click the "Import from file" button. Click yes, and type "i agree" after you verify that the correct disk is selected. Click yes again.
  5. Next, DiskImage prompts for the file that will be imaged onto the disk. Choose the raw audio .txt file outputted earlier.
With the raw audio imaged onto the SD card, we're ready to play it from the MSP430. Take the SD card out of the computer and put it into the card holder on the prototype.

next page

Gallery: MSP430 audio output

MSP430 audio prototypeProject overviewSD card SPI interfaceSD card data layoutPlaying a raw audio file

Make a talking MSP430 microcontroller - part 5


Firmware
The example program is included in the project archive. The example software is written using the demo TI/IAR Kickstart C compiler.



SD card access

The example firmware starts reading the SD card at the first sector. It reads one byte at a time over the SPI interface and places the byte in the PWM duty cycle register. When the end of a sector (byte 511) is reached, the next sector is immediately loaded and initialized so that the next byte is always ready when needed.

The example firmware will copy bytes from the SD card to the PWM register until it reaches the sector defined by the variable flashDisk.lastSector. At the end of this sector, the program begins again at the first sector.

The value to use here is determined by the number of sectors consumed by the audio file. The example audio file consumes 58,447 bytes. The SD card is arranged into 512 byte sectors, so the file ends at sector 115 (sector numbering starts with 0). Update this value if you are working with a custom audio file:

flashDisk.lastSector=115; //last sector (512byte block) where file is stored on flash...

Playback rate
The playback must match the sampling rate of the audio file, or the audio will sound too fast or too slow. Since I sampled audio at 8KHz, the PWM duty cycle register should be updated with a new value 8000 times each second. I appropriated the watchdog timer (WDT) from the MSP430 to sound an alarm (an interrupt) 8000 times per second. When the timer interrupts, a bit of code copies the next byte from the SD card to the PWM duty cycle register.

The timer runs on the calibrated 8 MHz internal clock. The WDT is set to trigger every 512 counts of the internal clock (8MHZ/512), or 15,625 times per second. This is about twice as fast as we need, so the interrupt routine uses a switch that updates the audio only once every other interrupt, or 7,812.5 times per second. Not exactly 8000 samples-a-second, but the internal oscillator will vary with temperature and age anyway. Using the internal crystal keeps the design simple and the part-count low.

If you're after tighter tolerances, consider using a watch crystal on the P2.6/7 pins of the MSP430 as the timer clock source.

When the MSP430 is not copying audio to the PWM, it enters a loop that continually checks if a new audio byte should be loaded into a single byte buffer. The buffer ensures that data is available when it's needed, and that audio quality isn't effected by delays in reading data from the SD card. A ton of power could be saved by entering sleep mode between interrupts, we'll look at this again in a future article.
next page

Fix locked iPod hold button with tin foil

My sister sent me her iPod telling me it's locked up: the hold button switch stopped working. It was stiff and felt like a piece of grit was in the switch. With the hold switch broken, all the other buttons stopped working as well, even while the screen indicated that the device was on.

I first tried the farmer method of fixing things, by adding a micro-drop of mineral oil to the switch....wrong, that didn't do anything. I tinkered a little more, and found out the actual switch on the circuit board was busted.

Hit the continue for more on this.

Gallery: iPod Fixing

Hold SwitchPry Open CaseRemove ScrewsPull Back Circuit BoardConnect Points

Continue reading Fix locked iPod hold button with tin foil

Make a singing MSP430 microcontroller



This is my second article about building a DIY digital audio recorder. Inspired by this microcontroller audio project [via], I set out to build a simple digital recording device. I chose Texas Instrument's MSP430 microcontroller for this project because it's fast (16 MHz), it's cheap ($1), and it's very low power.
Read the first part here.

This week we'll progress towards a digital audio recorder by generating simple tones with the MSP430 microcontroller. We'll use the MSP430's pulse-width modulator to generate an audio waveform, and clean it up with a simple low-pass filter. The signal won't be strong enough to drive a speaker directly, but it'll work great with a cheap set of powered PC speakers.

Next week we'll expand on this basic circuit to play audio recordings from SD memory cards.

Read on to learn more about generating audio with a microcontroller.

next page

Gallery: MSP430 audio prototype

Audio filter exampleMSP430 audio prototype circuitMSP430 audio prototype renderingMSP430 audio prototype PCB placementMSP430 audio prototype bare PCB

Make a singing MSP430 microcontroller - part 2

Microcontroller audio

There some great resources on the web to get us started with microcontroller audio.

This series of articles on Arduino audio (part 1, part 2, DAC options) gives a fantastic introduction to the theory side. There's tons of great stuff in these pages that I won't duplicate here.

Another project feeds audio samples into the microcontroller from a PC serial port. The microcontroller is like a simple PC sound card, it's not capable of independent operation. This project, and the great video, inspired me to design this digital audio recorder.

TI and ATMEL have application notes detailing designs for simple digital audio recorders. TI's design[pdf] records 12 seconds of low quality audio to the flash program memory of an MSP430. It uses a specialized chip to create the audio signal, complicating the design. A great TI app note on speech compression[pdf] also has some interesting support circuitry for digital audio. The ATMEL digital sound recorder[pdf] uses a small external memory chip to store a few seconds of audio.

Pulse-width modulated audio synthesis

The cheapest, easiest way to generate audio on a modern mirocontroller is to use a hardware pulse-width modulator (PWM). A PWM is a circuit that generates a repeating time period (called the period), and turns on a switch during a percentage of that time period (called the duty cycle). This happens so fast that only the average value of the on and off periods is measurable. Different audio tones can be generated by varying the duty cycle.

For more about pulse-width modulation, see my previous projects: Make a USB color changing light, and Show PC stats on analog gauges.

This diagram gives an overview of the design.


next page

Gallery: MSP430 audio prototype

Audio filter exampleMSP430 audio prototype circuitMSP430 audio prototype renderingMSP430 audio prototype PCB placementMSP430 audio prototype bare PCB

Make a singing MSP430 microcontroller - part 3



MSP430

The digital audio recorder will use a MSP430 F2012 or F2013. Both have useful hardware modules, like a pulse-width modulator (PWM) to generate sound, an analog to digital converter (ADC) to capture audio from a microphone, and a hardware communication module (SPI) to communicate with external memory chips. They're also fast (16 MHz), cheap ($1), and very low power.

F2012 and F2013 target boards are available for the ez430 development kit. You can get started on this project with a full programmer, debugger, and development board for $20. You'll still need the extra parts described in this article, but the development kit can make life easier if you're worried about working with surface mount components. The both MSP430 models are also available as a through-hole parts.

If you want to know more about basic MSP430 connections and programming, read last week's primer on the MSP430.

The MSP430 pulse-width modulator

The MSP430 has a 16 bit PWM. This could be used to play 16 bit resolution audio, a vast improvement over most previous 8 bit microcontroller audio projects.

The MSP430 PWM has a ton of modes. I'm sure they're all super useful, but we just want a standard pulse without the fancy stuff. To do this on the MSP430, I set the following registers:

CCTL1 = OUTMOD_6; // Mode 6 is toggle/set
CCR0 = 0xFE; // 16 bit PWM Period, use period-1 for MC_1(up counter)
TACTL = MC_1; // Timer A MC_1 mode counts up to the value in CCR0, resets

The PWM output signal can be enabled on three different pins. This is handy, but it can also be really confusing. I designated pin P2.6 for the audio PWM output. P2.6 is also used for an optional 32.768khz watch crystal. The crystal oscillator circuitry on the pins must be disabled before using the PWM output. This isn't documented very well in the MSP430 datasheets, read more in this forum post.

BCSCTL3|=LFXT1S1+LFXT1S0; //enable PWM output on P2.6, disable oscillator.
//.........................................................//now there are limitations on VLO....

This was a real pain to figure out. I don't usually include these details in an article, but I hope this info will now be easily accessible to anyone facing a similar problem in the future.

next page

Gallery: MSP430 audio prototype

Audio filter exampleMSP430 audio prototype circuitMSP430 audio prototype renderingMSP430 audio prototype PCB placementMSP430 audio prototype bare PCB

Make a singing MSP430 microcontroller - part 4

Audio hardware

The signal from MSP430 PWM pin isn't quite ready to drive a speaker or amplifier. First, we can clean up the signal a bit by running it through a low pass filter. This clips the sharp edges from the PWM waveform to make it sound a bit more natural.

You can calculate the ideal low pass filter for a given frequency with a calculator like this. I used an 8000Hz frequency because I eventually plan to play and record about 8000 samples-per-second with the digital voice recorder. Note that this is just a fraction of the 48000 samples-per-second used by CD players and PCs. At 8000Hz my ideal low-pass filter has a 0.01uF capacitor (C1) and a2K ohms resistor (R1).



Finally, it's proper form to block any DC voltage from the signal path using an electrolytic capacitor (POL-C1). The capacitor allows only the AC waveform to pass out of the circuit. A value between 4.7uF and 47uF seems to work fine. I didn't notice a difference among the range of values that I tried for the quality of audio produced. This capacitor is usually not necessary, as most amplifiers have a similar capacitor at the audio input connection.

Generating an audio signal in software

Now that the signal is conditioned, we can connect it to an audio amplifier. A cheap set of powered PC speakers work great.

Test program 1 (test1 in the project archive) creates a 50% 'on' signal with the PWM. This generates a continuous tone from the speakers. See it in this short video clip.


next page

Gallery: MSP430 audio prototype

Audio filter exampleMSP430 audio prototype circuitMSP430 audio prototype renderingMSP430 audio prototype PCB placementMSP430 audio prototype bare PCB

Make a singing MSP430 microcontroller - part 5

The next step...

Complex waveforms are generated by placing a series of values on the PWM in the right order, at the right time. Test program 2 (test2 in the project archive) is a simple program that plays different tones. I re-appropriated the watchdog timer for a twice-per-second interval alarm. When the alarm sounds (interrupts), the PWM duty cycle is changed to the next value (tone). This generates alternating tones as can be heard in this video.




Next week we'll expand on this basic circuit to play complete audio recordings from SD memory cards. A sneak peak of the next week's project is shown in the full project video.

The Prototype Circuit



The prototype circuit is not intended to be a final project – it's just an aid to understanding the article. It has three basic parts:

1. An MSP430 and support circuitry as described in the project last week.
2. An audio filter circuit attached to the PWM as described this week.
3. An SD memory card connected to the MSP430's SPI interface, which I'll introduce next week.

We'll design two final projects in a few weeks, but this prototype will demonstrate the important concepts along the way. There are several problems with this design that I know of, and probably several more that I'll find in the next few weeks.

All the code and design files for the prototype are included in the project archive. The circuit and PCB were made using the freeware version of Cadsoft Eagle. You can download it here. The test firmware is written in C and compiled with the free/demo IAR Kickstart compiler.

Prototype PCB


Part list -- Name, value (size)
The parts are specified by value and size. I used mostly surface mount components in this design. SMD parts help keep the design as small as possible, and save a ton of time on drilling. The audio coupling capacitor is a through-hole part because large value electrolytic capapacitors have resisted miniturization and remain quite large.

Nearly all the parts specified are also available in a through-hole version. I know of no through-hole SD card holders, but the large soldering tabs on this part aren't at all intimidating.

The most obscure part is probably the SD card holder. I used ALPS part number SCDA1A0901, purchased at Mouser.com (Mouser number 688-SCDA1A0901). This model is a push in, push out model with a spring. I'd much prefer a simple push in/pull out type, but I've yet to find one. Watch out when you pick a holder, pin placement and measurements vary wildly.

Misc
IC1, MSP430F20X2 or F20x3 (PW14)
LED1, SMD LED (0805)
ALPS-SCDA1A0901, Alps SD Card holder (n/a)

Capacitors

C1, 0.1uF (0805)
C2, 0.1uF (0805)
C3, 0.01uF (0805)
C4, 47uF (0805)

Resistors
R1, 47K (0805)
R3, 330R (0805)
R4, 2K (1206)

Related link

Program a MSP430 microcontroller

Gallery: MSP430 audio prototype

Audio filter exampleMSP430 audio prototype circuitMSP430 audio prototype renderingMSP430 audio prototype PCB placementMSP430 audio prototype bare PCB

Program a MSP430 microcontroller

Inspired by this microcontroller audio project [via], I set out to build a 100% DIY digital audio recorder. I chose Texas Instrument's MSP430 microcontroller for this project because it's fast (16 MHz), it's cheap ($1), and it's very low power.

This week we'll look at some MSP430 basics -- power requirements, programming connections, and development tools. In the coming weeks we'll make the MSP430 record audio.

The MSP430 has been around for ages, but the $20 eZ-430 USB development tool has really brought it to the attention of DIY'ers. This is a programmer, debugger, and development board in the shape of a USB flash-drive. If you're lucky, you can get one free at a MSP430 Day in your area.

Read on to learn about basic connections and programming options for the MSP430.

next page
Or jump to a section:
Basic connections, overview
Programming connections
Programmers and programming tools

Program a MSP430 microcontroller pt. 4

Programmers
A programmer is the device that physically connects a PC to the MSP430. The PC sends data to the programmer, and the programmer copies it into the flash memory of the MSP430 microchip. I know of no DIY MSP430 programmers. Fortunately, TI sells a complete programmer, debugger, and development board for only $20 (see below).

Newer MSP430s are programmed with the two wire SPY-BI-WIRE protocol, but older versions were programmed with a large JTAG interface. Some new, high pin count MSP430s support both the old JTAG interface and the new 2 wire protocol. This tutorial only applies to the new SPY-BI-WIRE devices.

TI's ez430
The ez430 is a $20 debugger and development board that looks like a USB flash disk. It works with TI's IAR Kickstart C compiler and development environment demo. The programmer connects to a tiny circuit board module with a real MSP430F2013 processor and LED. The standard board sports a F2013 with 16 bit ADC, but TI sells 3 packs of F2012 "target boards" for a few bucks. The ez430 will program any MSP430 chip that supports SPY-BI-WIRE.

You can get a free ez430 at TI's MSP430 days. Check this website to check for one near you. I got mine at one of these events. Watch out though, mine was ruined when IAR Kickstart accidentally updated the firmware in the programmer - something that should never be done to the eZ430. It was irreparably ruined and I had to order a new programmer.

TI's MSP-FET430UIF
This is an expensive ($100) programmer and debugger for the MSP430, but you usually get a code for 50% off at MSP430 day. This will also program the older JTAG chips, but we're not likely to encounter any of these.

Third-party
There are a few of third party programmers for the MSP430. Most of them are really expensive, but the line of programmers from Olimex is geared towards DIY'ers. The Olimex MSP430-JTAG -Tiny is an inexpensive alternate to TI's MSP-FET430UIF (above) -- with the same features and compatible with the same applications.

Gallery: MSP430 Programming

TI's MSP430 Programmersez430 MSP430 programmerMSP430 ProgrammerPreview of next week's project





Programming software/compilers
A compilers and integrated development environments (IDEs) is simply an application that is used to write software. I'm familiar with three IDEs that can be used to write software for the MSP430 and program code into the chip. There are several third party compilers, but those listed below have free or limited versions that work great for DIY projects. The IAR compiler, for example, is limited to 4K -- but that's twice the program space of the F201X chips!

IAR/Kickstart
This is the demo compiler, debugger, and development environment that comes with the ez430 programmer. It's a self contained environment for writing code in C, compiling it, and programming it to a MSP430. It can also control code execution in a prototype chip - this helps hunt down problems in complicated programs. This is a fairly nice and reliable way to work with the MSP430. Watch out, though, if it asks you to update the firmware on your ez430 USB stick, DON'T LET IT!

The IAR compiler is quite expensive, and the demo is limited to 4K. If you run into that limit, you'll probably turn to the next program.

MSP430/GCC
This is an open source compiler for the MSP430 based on the famous GCC compiler. It's accompanied by a complete tool chain that includes a programming and debugging application that works with the MSPFET and the ez430.

These tools can all be combined, with heroic effort, into the Eclipse development environment. This is a bit like bolting a text editor onto a compiler and debugger yourself, rather than giving IAR the privilege. I successfully followed these directions to install MSP/GCC and integrate the tool chain into Eclipse under Cygwin on MS Windows. It worked great for standard equipment, but lacked support for some modern MSP430s. That isn't quite true - the changes were present in the code but not the compiled version. It should be significantly easier to setup and compile under Linux.

Code Composer Essentials
CCE is TI's own compiler and debugging environment for the MSP430. The demo version will compile unlimited assembly code, but only 8K of C code. This is the MSP430 equivalent of Microchip's MPLAB. Historically, CCE has been fairly unloved, but it looks like a new version is available.

Taking it further
What do you do with a 16 bit low-power microcontroller? The MSP430 is popular among educational institutions because of the inexpensive development kit. Power conservation features also make it quite popular in the field of wireless sensor networks.

Next week, I'll show you how to use the MSP430's 16 bit pulse width modulator (PWM) to play audio files. The following week we'll record audio with the 12/16 bit analog to digital converter (ADC) and complete the digital audio recorder prototype. Finally, we'll look at how to turn the prototype circuit into a talking picture frame or stealth digital recorder.



Resources
TI has a host of training materials for the MSP430.
Probably the best source for help is the archive at the Yahoo MSP430 group.
TI maintains a list of discussion groups.

Program a PIC microcontroller


There are lots of cool hardware projects on the web. Many require you to program a microcontroller. Programming, or burning, happens when we copy software from a computer into the flash memory of a microchip. This is just like copying something to a USB flash drive, but it requires a special connection. Without the ability to burn firmware you can't build that awesome open source project -- and you can't develop your own.

Today we'll burn a PIC microcontroller from Microchip - in this case Microchip is a proper noun referring to this company. "PICs" are the brains in tons of projects -- this USB color changing light, or these analog gauges, for example.

Check out the video to see different ways to program a PIC, and read on to build your own simple JDM2 style programmer.

Continue reading Program a PIC microcontroller

Show PC stats on analog gauges

These old analog gauges were in a one-dollar junk box at the market. Before there were LCD screens in everything, before LEDs, data was shown on these.

In a sort of retro mash-up, we'll make a USB device that displays PC status info on these gauges. The gauges can show CPU and memory usage, processor voltage -- just about any numerical data typically displayed on small HD44780 based LCD character displays commonly used in PC case mods.

You'll find all the details and project files after the fold. Check out the podcast for an overview of the project.



Gallery: USB analog gauge overview

Analog gaugesPulse-width modulation on an analog gaugeDriver and analog gaugesRendering of the circuit board

Continue reading Show PC stats on analog gauges

Next Page >

About DIY Life

Do Life! DIY Life highlights the best in "do-it-yourself" projects.

Here you'll find all types of projects, from hobbies and crafts to home improvement and tech.

Featured Projects


Powered by Blogsmith

DIY Life Exclusives

scentuallife kiddie crafts avant-yard

Sponsored Links

Featured Galleries

An easy way to insulate and skirt an elevated structure
USB analog gauge overview
USB analog gauge circuit
Making and using a facial mask
Hot Sprinklers
Homemade lava lamp for kids
Create a Celtic pendant for St. Patrick's Day
Easy no-sew jeans messenger bag
Bathroom tile makeover - fish
Hinamatsuri doll examples
Poisonous Plants 101
Playground 4x4s
Upholstered nightstand makeover
iPod+Nike DIY duct tape pocket
cootie catcher
10 ways (OK, maybe a couple more) to increase your vehicle's fuel economy
Nike+iPod hacks and mods
Tile Floors
Valentine's Day Scentual Oils
Building the JDM2 PIC programmer
Hanging sheet rock overhead

 

Weblogs, Inc. Network