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.

13 comments:

Mandeep said...

I'm so glad to see a new post from you! I was afraid you'd gotten too busy to continue your blog. Your posts have been a great resource for the stuff I've done with my msp430 chips.

David said...

Busy, yes... but determined. =)

Adam said...

Great to see another tutorial in the making. This blog has been by far the most helpful resource in teaching me how to work with the launchpad.

Anirban said...

Hi,

I find this blog useful. I have a question about the Launchpad kit: do you know if the CCS-4 debugger dumps the bootloader code along with the user program onto the MSP430xxxxx at debug time? I am running out of program space and I need every bit of space available.

Thanks.
AB

David said...

I'm glad it's been helpful. I'm sorry it's been slow in progressing recently, and will start adding more soon.

I'm not exactly certain about boot-loader code or not, but there are options that you can play with in the CCS compiler. I know, for example, that it optimizes libraries like stdio to save room (an example I'll share when I do a post on UART etc.), and I would imagine it's possible to optimize the compiler further.

Another option is to get one of the newer G2xx devices that has more memory and similar capability to the chip you're using now- you might be able to snag a couple of free samples to try out!

sanju said...

hi David, ur blog has been really helpful. i am totally new to microcontroller.. anyway could please explain on ur next lesson how to use Timer to generate PWM..


thank u very much

Jorge Garcia said...

Hi David,

Thanks for the excellent blog, this post has inspired to make a nice LC meter with my launchpad.

I wanted to point out what I believe to be an error in your error calculation. Specifically the dC/dF calculation. Here's how I think you obtained the derivative.

C=t/R * 1/ln(1/F)
x=1/F

For this derivative t/R is a constant so we are only interested in the second term. Using the reciprical derivative rule

Dx 1/ln(x)= -(1/x)/(ln(x))^2

Substituting x=1/F back our final result would be

dC/dF=-t/(R(ln(1/F))^2) * 1/(1/F)

Which is the result you obtained. The problem is that as soon as you make the substitution x becomes a function of F, and since you're looking for the derivative of C with respect to F and not x the chain rule applies. The general derivative of the ln function is

Dx ln(x(F))=x'(F)/x(F)

Applying this definition we find

Dx ln(1/F)= -F^-2/F^-1= -F/F^2= -1/F

With this in tow, using the reciprical rule again we find that dC/dF is

dC/dF= t/R * (1/F)/(ln(1/F))^2

Which is what I believe to be the correct answer. I could very well be wrong but I thought I would bring it up and see what you thought.

With the corrected derivative, you'll find the effect of F on the error is greater then you originally thought.

I confirmed the general derivative of the ln function in my edwards & penney textbook.

Thanks again, for the inspiration.

Jorge

Jon Thornham said...

Any plans to continue the blog? This is a great resource I am really missing.

David said...

Goodness, it has been a while, hasn't it...

Yes, I do intend to continue, though it's been put on hold for a while as I am buried in writing my Dissertation. Thank you, everyone who has commented, for your encouragement. Thanks for being patient as I work things out on this end.

Jon said...

Get your Dissertation finished. That's a tough piece or work. Thanks for taking the time to respond.

Take care,

Jon

PC said...

Hi David,

Thank you very much for this resource. It has been really useful for me. I wanted to let you know about the following

TI has released the MSP-EXP430FR5739 FRAM Experimenter’s Board (http://focus.ti.com/docs/toolsw/folders/print/msp-exp430fr5739.html?DCMP=FRAM&HQS=Other+PR+fr57xx-pr-tf#Technical%20Documents) aat $29 and they currently have a 50% sale on it (http://e2e.ti.com/support/microcontrollers/msp43016-bit_ultra-low_power_mcus/f/166/p/117134/416050.aspx#416050).


Due to the FRAM the read/write speeds are significantly faster and would definitely be a benefit when data-logging from sensors. Also it further reduces power consumptions

David said...

That sounds like a great product-- I'll have to look at it. You're right in your assessment of its usefulness in data logging.

General comment to everyone: I know a lot of people have written asking if I'm going to continue. The current plan is to do so, but I have a lot of things changing in my life right now that take priority. I do hope to get to the point where I'm able to return; thanks for your interest and patience!

George said...

Beretta & David!!

Great job on the MSP430 blog, excellent detail and I followed everyone of post!

please dont leave us hanging because I am down to this very last tutorial(so far) and it was posted back to Feb 2011, and it definitely looks like does not end there!

you can just have a short update on your current events happening in your lives and let us know you still care about the MSP430 community =D