My Tiva LaunchPad Powered Sine Player (Video Included)

A while back I mentioned I had been reading up on ARM Cortex-M microcontrollers and obtained a Texas Instruments Tiva LaunchPad (aka "Tiva Stellaris"). The Tiva LaunchPad has an ARM Cortex-M4 chip onboard. I've completed my first small project: An audible sine wave player whose tone is controlled by a potentiometer.

Checkout the brief video below to see it in action. The setup is straightforward; consisting of the following:
  • The Tiva LaunchPad, running a small (~120 line) C program I wrote.
  • A 5K potentiometer hooked up to one of the LaunchPad's GPIO pins.
  • A 3 bit DAC (digital to analog converter) I created using eleven 22K resistors and a breadboard.
  • A headphone jack connected to the output of the DAC.
  • Typical PC speakers to amplify and audibly play the generated sine wave.
The key things happening here:
  • The PE4 GPIO pin is configured for analog input and wired to the potentiometer.
  • The C program running on the ARM Cortex-M4 chip on the Tiva LaunchPad continually polls the PE4 pin value.
  • The value of the PE4 GPIO pin is used to calculate the duration before the next interrupt is to be triggered.
  • The interrupt routine outputs a 3 bit value to GPIO pins PB0, PB1 and PB2.
  • The value the interrupt routine outputs is one of 16 pre-created values making up a single cycle of a sine wave.
  • The current sent to the PB0, PB1 and PB2 pins is converted to an analog signal by the DAC.
  • The analog signal is amplified by the PC speaker; driving the speaker and creating the audbile tone.

Below is a close up picture of the Tiva LaunchPad and the breadboard DAC. Click on the picture to see a larger version.

Tiva LaunchPad

I don't really have a background in electronics, but I'm interested in learning more. Until doing this project, it had never occurred to me how simple a DAC can be: Just a few resistors on a breadboard. I got the design and some other helpful info from Jonathan Valvano's site here.

If you're interested in taking a look at my C code that makes this project work, feel free to download it here. I used TI's Code Composer Studio for development and debugging, but the code can be easily viewed with any text editor.

Standard disclaimer: In this code you'll see a lot of raw memory addresses for accessing necessary registers and other magic numbers. I wouldn't write normal production code like this. I did it in this case for the sake of my own learning benefit; just to see things as basic as possible to understand everything going on with the device.

The majority of the code is just setting values in registers. If you're curious how it works, the Texas Instruments Tiva TM4C123GH6PM Microcontroller Data Sheet PDF contains everything you need to know. Each bit of each register is described in this document.

Date: September 22nd, 2016 at 8:07pm
Author: Terence Darwen
Tags: Embedded, Tiva LaunchPad, TM4C123G, ARM-Cortex-M4, Sine Player, Sabbatical

Previous Next