4-Button Piano

Here’s another cool Lab that I worked on as part of the edX MOOC I mentioned before using the Texas Instruments TM4C123G Launchpad Eval kit.  To learn more details about that course, please see the Lab 10 explanation.

This time in Lab 13 I was tasked with developing a 4-button piano that plays music using 4 momentary switches and a DAC (Digital to Analog Converter) circuit to generate the sound.

(So what happened to Labs 11-12 ?   Chapter 11 covered learning to use the UART and the associated lab involved using the UART to talk to the Nokia 5110 84×48 LCD, which was pretty cool.   It really was meant more as a stepping stone for later labs that would output to the LCD.  Here’s a video of the profs demoing the expectations for Lab 11.  Chapter 12 covered learning how interrupts work on the TM4C123GH6PM, and mainly how to use the Systick Timer based periodic interrupt.  Lab 12 involved making music using a DAC, similar as Lab 13 below.)

Lab 13 — Making Music

The purpose of this lab was to be able to play 4 different notes, or tones, on a speaker when each of 4 buttons are pushed.

To generate sound, a simple Sine wave was generated by the TI board and sent to a speaker.  Although an analog output on the microcontroller could have simply been used to generate this Sine wave, for the purposes of learning, digital outputs of the microcontroller were used instead.    These were then fed into a rudimentary DAC (digital-to-analog) circuit on a breadboard to generate the necessary analog Sine wave.

sinwave

6-bit Sine Wave in Excel

The Sine Wave above is of 6-bit resolution, i.e. it is represented by digital values from 0 to 63 (2^6) in amplitude. As a result, the period, or time scale, of the wave above is also divided to 6-bit resolution.

Basically, at each point in the wave above, a different 6-bit value (0-63) will be driven by the MCU fast enough so that the ear HEARS a continuous Sine wave of a single frequency.  Again, there are built-in functions to do this, but for the purposes of learning, this more low-level approach was used.

The Sine Wave above was actually defined first in Excel, just using math:

table

Partial 6-bit Sine Wave Table

Sure this could have been calculated in the code– but pre-calculating the values above and storing them in a table in RAM will save on execution cycles, which for this application is critical in not slowing down the generation of the Sine Wave in real time.

So how exactly do we go from a 6-bit digital signal generated by code to an analog Sine Wave that a speaker will use to generate sound??  We need to build a digital-to-analog converter, a DAC, circuit.

Well, there a few different DAC circuits we could use, and Chapter 13 showed 2 types:  2-bit weighted binary and R2R ladder.   Both have the digital outputs drive nodes of a resistor-based circuit organized in such a way so that different digital values driven on these nodes result in different voltages corresponding (in this case) to one of 64 voltage levels generated on an ‘output’ node.

The 2-bit weighted binary resistor circuit looks like this:

2_19_6_2

4-bit DAC

The problem with this is that the more bits you add the greater the difference between the resistance on the least significant bit 0 (LSB) and most significant bit 3 (MSB).  Resistors have % tolerances so the larger they get the more inaccurate they get.  It’s also very difficult to find resistors that match all the values I need (or you have to use resistors in series).

So I decided to use the R2R resistor tree method:

R2r-ladder

R2R ladder of n-bits

In this DAC circuit, the largest resistor is only 2x that of the smallest one so it’s easier to implement with resistors at hand.  Of course, resistance errors still add up along the various current paths, but for me this was the easier circuit to build.

Now the final step is that we need to drive each of the 6 digital outputs fast enough so that it SOUNDS like notes are being played at a particular frequency.

Interrupts

In order to make the DAC sound like it’s playing a single note, the processor has to drive the 6 digital outputs at a frequency 64 times that of the note.  Or saying it another way, the period of time between changes on the 6-bit digital output must be 64 times smaller than the period of the note.

The table below shows the required time delay for each digital update in the Period/64 column.

Piano Key #NoteFrequencyPeriodPeriod/64Systick RELOAD
value (80MHz)
0C523.251 Hz1.911 mS29.861 uS2388
1D587.330 Hz1.703 mS26.603 uS2127
2E659.255 Hz1.517 mS23.701 uS1895
3G783.991 Hz1.276 mS19.930 uS1593

To achieve this, I used the Systick Timer to drive a periodic interrupt in the microcontroller.   Chapter 10 of the edX class describes the use of this Systick Timer, which is a counter in the system that counts down from a predetermined value down to 0.  This value is called the RELOAD value.   The speed at which this counter counts is the system clock frequency, which in this case is set to 80MHz (using the PLL as a reference).   Chapter 12 describes how you can have this timer fire a periodic interrupt every time the counter rolls down to 0.   Whenever this interrupt ‘fires’, the Interrupt Service Routine that gets executed can then update the digital outputs.

Basically, by modifying the start RELOAD value used in the counter, the Systick can be used to generate a sound wave of the desired frequency!

Final step then is to have 4 momentary switches connected to 4 digital inputs to determine which Note to play.

https://www.youtube.com/watch?v=ExHPVHXh8Ts

Ok, so it is basically, this:

But just a bit cooler.

Stop-Go-Walk!

MOOC

Well, I thought I’d let you all know why I’ve been a bit behind on my blog posting.  One of the 2 reasons…

Early March, I decided to take an online MOOC course related to learning embedded microcontroller programming.  That stands for Massive Open Online Course given online by an organization called edX that hosts various online courses given by various brick-and-mortar Universities.   The course I’m taking is called Embedded Systems: Shape the World, UT.6.0.2x, hosted by the University of Texas at Austin, and is the second year of an online version of UT’s EE319K: Intro to Embedded Systems.  The course is taught by Dr. Jonathan Valvano, who has several books on embedded microcontrollers, and Dr. Ramesh Yerraballi, both of whom continue to teach classes at UT Austin in the Electrical and Computer Engineering Department.

I did this to sharpen my embedded microcontroller programming skills, since when I was last in school doing this stuff the internet was still being invented (ok, made generally popular, I’m not THAT old).   Anyway, I thought that this would be a great way to learn something new at night in an inexpensive way.   You can take the courses for free, but you can get verified with edX for a donation (minimum $50) after which you can get course credit if you complete it in the allotted time.

The course uses the Texas Instruments TM4C123G Launchpad Eval kit, which you need to purchase to complete the course, but thankfully is relatively inexpensive at $13.  I decided to go ahead and get the optional hardware for Lab 15 (Nokia 5110 84×48 LCD) and Lab 16 (CC3100 BoosterBack), in case I had time left in the ‘semester’ to do them.

I started the course a bit late so had to play a lot of late-night catch-up.   The course had 16 chapter each of which generally include an online graded quiz and lab.  The labs each have 2 parts:  a simulated-graded portion and a real-board-graded portion.  The only real deadline was that the course would close on May 13, at which point you couldn’t get additional credit for the labs or quizzes.  To get course credit and certification for this particular class, you have to achieve a grade of 50%.

So I made myself a schedule for completing the labs on time that was about 4 days per chapter (vs. a week as suggested by the course profs).  Most of the early chapters went quickly, however as expected the later chapters took longer.    I thought I would document here some of my progress on some of the more interesting stuff I did in the labs.

Lab 10

Lab 10, shown below, is the culmination of Chapter 10 of the edX class, which focuses on learning:

  • Finite State Machines, specifically a Moore FSM, which is a way to describe a system that depends on inputs and determines outputs
  • Using the PLL, which is an accurate system clock with changeable frequency
  • Accurate time delays using the Systick timer

The objective of the lab was to implement a traffic light intersection with cars going either Southward or Westward:

Lab10image001

The control system looks for either a car on the Southward or Westward roads and changes the lights appropriately if a car happens to be present to let it through.   Real roads have inductive loops installed under the road as shown below to detect cars, trucks and sometimes even bikes at an intersection:

220px-Inductance_detectors

The system also has a Walk/Pedestrian sensor which simulates the switch commonly seen on intersections that pedestrians push when they want to cross.   For the purposes of this lab, the traffic light system does not auto-cycle the lights after a certain time as real traffic lights typically do.

As mentioned above, each lab includes a simulated and real-board grader.  What this means is that the provided Lab Starter software includes code that runs to check the inputs/outputs and other various setting on the real board or on a simulated version of the board included in the software.  The simulated board is very handy as it permits debugging software “offline” to validate that it works ok before trying on real hardware.   In the Semiconductor world, we call this simulation-type checking work Design Verification.   The same checking on the real hardware/silicon we call Design Validation. 

After quite a bit of work in designing my theoretical traffic light state machine to determine the proper states and inputs and outputs, here is the final result:

https://www.youtube.com/watch?v=_mHofGXF0_o

Incidentally, the is the SECOND traffic light circuit I have built.  The first I did one as a class demo for a prof at Trinity University the summer of 1993 (!), not using a microcontroller but logic gates to implement the state machine.  I actually made a mock-up of a real intersection with real traffic lights.  There were reed switches under the “road” that would be activated by strong magnets in toy cars.    Unfortunately, I don’t have a picture so you’ll have to use your imagination.  Needless to say it was pretty cool too.

In any case, I really am enjoying this online class and highly recommend it!  If you’re interested, check the edX site in the Fall (Sept. 2015) as I believe the professors will give it again!   They have also talked about giving another, more advanced class.

Stay tuned for more edX UT.6.0.2x Lab completion videos!