18 February 2011

Tutorial 13a: Combining Peripherals

The MSP430 peripherals are very useful tools in their own right, but when we're able to use two or more peripherals together, they become much more powerful for instrumentation.  This tutorial will go through one way that we can combine the Comparator_A+ and Timer_A peripherals.  In the next post, we'll use this combination to build a capacitance meter.

Background
Capacitors are one of the three fundamental, passive components that are used in nearly every electronic device out there.  When a voltage is applied to a capacitor, it will store a charge proportional to the applied voltage.  This proportion is called the capacitance.  These factors are all connected by the simple relationship Q = C V.  (Q is the charge, C the capacitance, and V the voltage.)  When the applied voltage is removed, the charge doesn't disappear immediately, but rather decays gradually by running a current through whatever resistance is present between the capacitor's two leads.

The Discharging Capacitor Equation
V_0 is the starting voltage.  In SI units, V is in volts, t in seconds,
R in ohms (Ω), and C in Farads. Equivalently, you can use t in μs and C in μF,
or t in ms, R  in kΩ, and C in μF. These are more useful in formulating
the calculation portion of the instrument. 
The way in which a capacitor's charge dissipates is simple to describe, but might not be what you expect if you haven't seen it before.  Right when we disconnect the charging voltage V, the voltage across the capacitor is still V because no charge has moved yet.  In that first moment, a little chunk of charge will flow as a current through the resistance, according to the well-known Ohm's Law: V = I R.  (Again, V is the voltage, I is the current, and R is the resistance.)  After that first moment, how much charge will move in the next moment?  A simple picture would be to guess that the same amount will move, however that's not what happens.  Since a little charge has already left the capacitor (Q is now smaller), the voltage across it now must be a little smaller too!  Since we have a slightly lower voltage, the next moment will have a little less current.  The following moment a little less still, and the next a little less, and so on.  This picture describes not a linear drop in the voltage, but rather an exponential change.  If you watch the voltage on the capacitor carefully (with an oscilloscope or some other device) you'll see that this is exactly what happens.

While it's not a linear change, it's still a simple description, and we can exploit this characteristic to find the value of the capacitance, C.  Assuming we know the resistance, all we need is to apply a voltage and time how long it takes for the voltage to decay to another known value.

Formulating the Capacitance Meter
The idea behind the instrument we are going to build is simple: we charge up a capacitor, then start the timer at the moment we start discharging.  We stop the timer when the capacitor has discharged to a known point, then plug in the values into the formula.

Formula needed to calculate C from the MSP430
C is in μF if t is in μs and R in Ω.  Alternatively, C is in nF if t is in μs and R is in kΩ  

The formula is easy to find.  When we invert the Discharging Capacitor Equation, we find that our measured value of C depends on the time for discharge, the resistance, and the fraction of the starting voltage to the ending voltage.  We can use Comparator_A+ to trigger the timer when we reach our chosen reference.  For this tutorial, we'll discharge the capacitor until it reaches 1/4 Vcc, so that we use f = 4.  This value is convenient, and minimizes any external components.

In the next tutorial, I'll show you how to set up the peripherals and suggest a few ways that you can read the obtained result with the MSP430.  You may wonder how accurate this setup will be.  The short answer is that it will be as accurate as you know the value of the resistor; if you use a 5% resistor, you'll be able to measure C to 5%.  A 1% resistor tolerance will give you close to 1% for C, but at that point some of the other factors become more important.  If you are interested, take a look at this description I've written on finding the error in the measurement.