In order to do an actual scientific experiment using the MSP430, we need one more tool. To be fair, we could do with what we’ve covered so far, but it requires constant (or at least regular periodic) monitoring of the equipment, and manual recording of the data displayed on the LCM. No, what we need is a way to automatically record the data when it is taken.

There are two different paths open to us at this point: the MSP430 has on-board flash memory. We could use it to record multiple measurements. The other, more complicated path is to learn how to communicate between the LaunchPad and a computer via USB. There’s some elegance in starting with the former, as our focus to this point has been on the LaunchPad itself, but unfortunately we’d need a way to transfer the data from the flash memory to a useable location anyway, which more or less requires connection to a computer. So even though it will delay getting to some of the cooler things we can do with the MSP430, it’s time we tackle serial communication. Once we have this piece mastered, we’ll start a little science experiment that will take me a few days/weeks to complete. During that time, we’ll begin looking at recording to flash, communicating with external peripherals, and how to put all the pieces together for remote data collection. We’ll also start looking at alternative power systems, system control, and other great things that will completely open the field of what’s possible with a microcontroller. The future looks bright; but first we’ll have to tackle this difficult task.

Well, things aren’t really so bleak… serial communication isn’t that complicated. In fact, most MSP430 devices have peripherals built in already for that very purpose, making it simple to do. However, of the two devices that come with the LaunchPad, only the G2231 has one of these peripherals, and it only has two modes of operation, conspicuously missing the one we really need first: the Universal Asynchronous Reciever/Transmitter, or UART. So, instead, we are going to turn to learning to implement this functionality in software.

Fortunately, there’s some real advantage to this; a solid understanding of how serial communication works helps us understand how to process and record scientific data. In fact, when we get to the USI/USCI peripherals, looking at other modes of communication such as SPI and I2C, we’ll take the time to understand how these methods send data. (The particular implementation of a serial communication system is called a protocol. There are even more protocols available, including Bluetooth, Wi-Fi, and ZigBee, which are cool things we’ll tackle some day!)

You might be asking, “Why are we going to rehash software UART? Lots of people have published articles about it already, and lots of code and examples are available.” Well, I’d respond that there are two reasons. The more philosophical reason is that you become a better scientist when you understand how the tools you’re using work; Einstein once said you don’t really understand anything until you can explain it to your Grandmother. The more practical reason is that none of the articles I’ve perused give much explanation to why the code is set up the way it is. That’s our goal here: by completely dissecting the software UART, we learn how serial communication works, and get a thorough example of using the MSP430 peripherals to our advantage in getting jobs done. We’ll also do a very thorough job, starting with just transmission (I guess technically it would be UAT), then moving to just reception (likewise UAR), then designing a full-on UART transceiver. Along the way, we’ll talk a bit about crystals as well as learn about calibrating our DCO. We’ll even talk about saving DCO calibration to the flash memory, and introduce the concept of a checksum. (So we’ll see a little bit about writing to flash memory soon after all!)

If that sounds like a lot to cover, it is. I’ll do my best to keep the posts coming regularly and quickly, so that we can move on to more advanced ideas soon. There is motivation for approaching this topic in this way at this time, however. These tutorials have always been designed as notes from my own learning. As a result, sometimes the methods/styles have been a little disjointed, but one of the goals of this blog was to put together a curriculum that could be used to teach science students in a one-semester course on microcontrollers. (After graduation, I’ll gather, edit, and format these tutorials into a book that can be downloaded for just such a purpose.) I think the material we’ve covered to this point fits about a one semester course very well, so think of this tutorial as the final project for the course. It’s a bigger concept that will take a while, but will draw on our knowledge from the other peripherals and skills we’ve learned. The fact that we’ll introduce some new ideas along the way will add to the sum total of knowledge taken away from this course. So strap in; we’re going to start the final for MSP430 101!

[via: http://mspsci.blogspot.com]

 

As we’ve seen, microcontrollers have great usefulness in digital electronics.In the real world, and for scientific applications, however, digital is not always adequate.In this tutorial, we begin working with analog signals using a 1 bit analog to digital converter: the comparator.

Some MSP430 devices have a built-in comparator (called the Comparator_A+ module in the technical documentation, I’ll refer to it as CA+ here), including the MSP430G2211 that comes with the LaunchPad kit.(See TI’s website for other LaunchPad-compatible devices that include the CA+ module.)Up to this point, all of the tutorials could be used on both the G2211 and the G2231 chips in the LaunchPad kit by changing the

#include

header appropriately.For this tutorial, you will need to use the G2211.

Overview of the Comparator_A+ Module

The CA+ module is very flexible, allowing multiple GIO pins to be connected to its inputs and including a variety of useful internal voltage references.As you may recall, a comparator has two inputs, called inverting and non-inverting (often labeled V+ and V- respectively).The V+ input can be connected to three different pins for external signals, or any of three different internal reference signals.The V- input can be connected to seven different pins (two overlap with the V+ inputs) and any of the internal references.CA+ has an output that can be read in software, trigger the CA+ interrupt, and trigger a timer interrupt.All of these options give the CA+ module its flexibility and usefulness to us in developing instrumentation.

Take a look at the datasheet for the G2211, and find the page that shows the pinout diagram for the chip.The pins that can be connected to the CA+ inputs are labeled as CAx.Note that CA0-2 can be connected to V+, while CA1-7 can be connected to V-.Note also that in addition to providing the CA7 input, P1.7 can be configured to provide the output value CAOUT externally.

Configuring the Comparator_A+ module

The CA+ module has three different registers for its configuration: CACTL1, CACTL2, and CAPD.These registers are described on pages 19-10 to 19-13 of the x2xx Family Guide.We’ll look at the basic pieces here.

CACTL1

  • The CAREFx and CARSEL bits (4-5 and 6) control the internal references for the comparator.Bits 4 and 5 select the reference value, while bit 6 selects which input is connected to the reference.There are three references provided in the module.Two of them are a fractional value of the voltage powering the MSP430: 1/2 Vcc and 1/4 Vcc.If you are powered from the USB (3.6 V), these references are 1.8 V and 0.9 V respectively.(These are accurate to about 1%.)A third reference is provided by the forward voltage of a transistor.This voltage is typically 0.55 V, but is less accurate and susceptible to temperature changes.
  • The CAIFG, CAIE, and CAIES bits (0-2) control the interrupt for the comparator.Bit 0 is the interrupt flag, which clears automatically when the interrupt is serviced.Bit 1 turns on the interrupt, and bit 2 selects whether an interrupt is triggered on a rising edge (low to high transition) or a falling edge (high to low transition).
  • The CAON bit (3) turns the comparator on when set, and off when cleared.
  • The CAEX bit (7) is used to exchange the two inputs; ie. the pins/references connected to V+ and V- are swapped.(In addition, the output is inverted to compensate for the change.)This ability is useful in examining values close together, but for basic functionality is not necessary.

CACTL2

  • The P2CAx bits (2-6) are used to select the pins connected to the comparator inputs.V+ is selected with P2CA0 and P2CA4.V- is selected with P2CA1-P2CA3.The x2xx Family Guide provides a table on page 19-12 that explains which bit configuration selects the various pins.
  • The CAF bit (1) puts the output through an RC filter to smooth out any rapid oscillations that might occur if the comparator inputs are very close together.It’s not always necessary, but is a good idea to enable if you’re using slowly-varying signals.
  • The CAOUT bit (0) is the output value only; you cannot write to this bit, but reading it will give you the current value of the comparator output.
  • The CASHORT bit (7) shorts the two comparator inputs together.While this might sound like an odd thing to do, it is useful under certain circumstances.Basic use of CA+ will not use this function.

CAPD

  • The CAPD register is analogous to the Port registers; each bit correspondsto the input pins CA0-CA7.The purpose of this register is to disconnect the digital circuitry for the GIO ports from the pins that are being used with analog signals.If an analog signal close to the transition voltage for the digital circuit is applied to the pin, the digital portion could oscillate between high and low fairly rapidly.This effect can cause degradation of the circuitry in the chip, and so it’s best to disconnect it completely when analog signals are used.In fact, when a pin is connected to the comparator, it is automatically disconnected.The purpose of this register is to manually control the disconnect.Sometimes you may want to use the comparator on multiple signals; only one pin can be connected to the comparator at a time, but you can switch the input connection in the software.While the CAPD method of disconnecting the digital circuit is redundant when only one pin is used, enabling the CAPD bit for a pin connected to an analog signal prevents it from being reconnected to the digital circuit when the software causes the comparator to switch input pins.

Programming Example

To demonstrate the CA+ module, look at a basic comparison program in

bcompG221.c

.The idea in this program is to measure an analog voltage, and flash an LED if the voltage is above a particular reference, in this case 1/2 Vcc (1.8 V when running off the USB power).To do this, the program uses the Timer_A module to flash the LED, while the Comparator_A+ uses an interrupt to turn the flashing on and off.

The portion of the code that configures the CA+ module is as follows:

CACTL1 = CAREF1 + CARSEL + CAIE;// 0.5 Vcc ref on – pin, enable
// interrupts on rising edge.

CACTL2 = P2CA4 + CAF;// Input CA1 on + pin, filter output.

CAPD = AIN1;// disable digital I/O on P1.1 (technically

// this step is redundant)

The values used here can be found in the header file for the G2211.Setting CAREF1 alone selects 1/2 Vcc as the reference voltage and setting CARSEL connects the reference to the V- input.Setting P2CA4 alone selects CA1 (same pin as P1.1 for the G2211) as the V+ input.AIN1 was defined to be BIT1, so we permanently disable the digital circuit on P1.1 for this program.This step wasn’t essential, since we don’t change the inputs on the comparator in this program.

After all the peripherals are configured, the comparator is turned on with CACTL1 |= CAON(the |= operator is used to prevent changing any of the other configurations we put in at first) and the chip enters LPM0.

TimerA periodically triggers an interrupt and toggles the P1.0 output with the value stored in flash.When CAOUT is 0, flash is 0 and so the LED never turns on.When CAOUT is 1, however, flash is set to LED1 (defined to be BIT0) and the LED toggles on and off.

Now let’s look at the Interrupt Service Routine for CA+:

#pragma vector = COMPARATORA_VECTOR

__interrupt void COMPA_ISR(void) {

if ((CACTL2 & CAOUT)==0×01) {

CACTL1 |= CAIES;// value high, so watch for

// falling edge

flash = LED1;// let LED flash

}

else {

CACTL1 &= ~CAIES;// value low, so watch for

// rising edge

flash = 0;// turn LED off

P1OUT = 0;

}

} // COMPA_ISR

None of the examples that TI provides on their website demonstrate how to use the Comparator_A+ interrupt.To find the proper syntax, I located the vector name in the msp430g221.h header file.(Remember, the vector name must be what the compiler expects (in this case, COMPARATORA_VECTOR).The header file is the safest place to locate the proper name.)The interrupt routine name, of course, can be named anything.Something descriptive is always helpful, though.For this program, I chose COMPA_ISR().The service routine first examines the value of CAOUT.If it is 1, then the voltage on V+ is higher than the reference on V-.The next interrupt should be when the value drops below the reference again, so it sets the edge select to a falling edge.It then sets flash to a non-zero value so the LED will blink.If CAOUT is 0, then V+ is lower than the reference.The edge select is set to a rising edge, flash is set to zero so the LED won’t blink, and the P1OUT is cleared to ensure the LED is off.

To test the program, you can connect a potentiometer (anything above 1 kΩ should work) between Vcc and ground, and connect the wiper to the P1.1 pin on the LaunchPad.Adjust the potentiometer to see the LED start blinking (indicating the voltage on the pin is above 1.8 V) and back again to see it turn off (indicating the voltage is below 1.8 V).If you don’t have a potentiometer, you can try two resistors of different values.Connect one resistor between Vcc and P1.1, and the other between P1.1 and ground.If the smaller resistor is between Vcc and P1.1, the voltage should be more than 1/2 Vcc, and the LED blinks.If the larger is in that place, the voltage is less than 1/2 Vcc, and the LED turns off.(Caution: If you use two resistors to try this, be wary of removing the resistors while the LaunchPad is powered.You might not do any damage, but it’s safer to power off a circuit before pulling pieces out.)

Reader Exercise:Change the program so that the LED doesn’t blink, but is set on/off constantly for high/low values of CAOUT.Then use TimerA to periodically change the reference voltage between 1/2 Vcc and 1/4 Vcc.When comparing to 1/2 Vcc, it should turn the red LED on P1.0 on if the analog signal is above the reference.When comparing to 1/4 Vcc, it should turn the green LED on P1.6 on if the signal is above the reference.Turning the knob on the potentiometer, you should see the green LED light when the signal is above 1/4 Vcc, then the red LED light when above 1/2 Vcc.

[via: http://mspsci.blogspot.com]

 

GPIO Interrupt Example

The first example we’ll do uses the Port 1 interrupts; this code is easily changed for any port number used in your particular device.This code will show how to do the DCO example we did in

Tutorial08-b

, which demonstrates changing the DCO, using interrupts.

First, we need to configure the port to use interrupts.The LaunchPad button is on P1.3, so all of our concern will be on that pin.The pin needs to be configured as an input.Since the default state of the pin is logic 1, a high-low transition (a drop from 1 to 0) should be used to signal the interrupt.We can code this using:

P1IES |= BIT3;// high -> low is selected with IES.x = 1.
P1IFG &= ~BIT3;// To prevent an immediate interrupt, clear the flag for
// P1.3 before enabling the interrupt.
P1IE |= BIT3;// Enable interrupts for P1.3

We’ve enabled the interrupt for P1 now, and a high-low transition caused by the button press will set the interrupt flag in P1IFG.The processor, however, isn’t set to recognize maskable interrupts like P1IFG.We can turn on the interrupts with:

_BIS_SR(GIE);

or equivalently:

_enable_interrupt();// Note the singular name here!It’s not interrupts.

The first method parallels the

bis.w

instruction in assembly used for this command, and can be used to configure the SR in one step with other options, such as low power modes.The second method is a little more human readable, and works well enough if you don’t need to enable any LPM’s.

Next we need to write the ISR.Something like this will work:

#pragma vector = PORT1_VECTOR
__interrupt void P1_ISR(void) {
switch(P1IFG&BIT3) {
case BIT3:
P1IFG &= ~BIT3;// clear the interrupt flag
BCSCTL1 = bcs_vals[i];
DCOCTL = dco_vals[i];
if (++i == 3)
i = 0;
return;
default:
P1IFG = 0;// probably unnecessary, but if another flag occurs
// in P1, this will clear it.No error handling is
// provided this way, though.
return;
}
} // P1_ISR

The port interrupts can be sourced from 8 port pins, and so the ISR needs to decide what to do with each possible interrupt.The switch statement in C is ideal for this task, and is used here to single out a BIT3 flag.If, for some reason, the MSP430 is flagged with a different Port 1 interrupt, it simply clears the flag and moves on.Since none of the other flags are enabled, this should never happen, but it’s good coding practice to include something like this.Error handling would be ideal, but it’s not done here.

Note that since the P1 interrupt has 8 sources and individual flags, you need to clear the flag manually.Don’t forget to do this, or your code will continually keep interrupting!This code makes use of three pre-stored values for BCSCTL1 and DCOCTL and increments through them each time the interrupt is called.These values, as well as the counter

i

, need to be global for the ISR to see them, and so they are declared outside of the

main()

function.See the whole code put together in

interrupted-1_G2211.c

.

Timer_A Example

As a second example, we’ll do something similar to a project I’m working on and turn a light on and off using a relay switch.I’ve coded this using a switch on P1.6 so that you can observe its behavior on a LaunchPad using the green LED instead of the actual relay.If you have a relay, be sure to

connect it correctly

to prevent too much current draw from your MSP430.Using the 1 MHz calibrated DCO divided by 8, Timer_A will count up to 62,500 in 0.5 s.In my actual application, I’d like to turn the light on for a few hours, then turn it off for a few hours.That’s easier to do with a slower clock, and an ideal application for the LFXT1 clock source.But, since we haven’t covered that yet and many people likely do not have the crystal soldered to their LaunchPad’s, we’ll do more frequent on/off cycles to simulate the actual process.We’ll use the DCO to turn the light on and off in 1 minute intervals.To do this, we’ll make use of the Timer_A up mode and interrupt using the CCR0 registers.(Yes, this is essentially a fancy version of blinky.Turns out “Hello, world!” is actually useful in microcontrollers!)

We configure the timer with the following:

TACCR0 = 62500 – 1;// a period of 62,500 cycles is 0 to 62,499.
TACCTL0 = CCIE;// Enable interrupts for CCR0.
TACTL = TASSEL_2 + ID_3 + MC_1 + TACLR;

The long string of assignments to TACTL are, in order, select SMCLK as the clock for Timer_A (runs off the DCO by default), select input divider of 8 (1 MHz SMCLK becomes 125 kHz), select up mode, and clear the count in TAR.These values are defined in the device header file so that you don’t have to constantly manipulate bits by hand.The mnemonics are easier to use most of the time.(For other configuration options, see the Timer_A section in your device’s header file.)

The ISR may look something like this:

#pragma vector = TIMERA0_VECTOR
__interrupt void CCR0_ISR(void) {
// no flag clearing necessary; CCR0 has only one source, so it’s automatic.
if (++i == 120) {
P1OUT ^= RLY1;
i = 0;
}
} // CCR0_ISR

Again, we’re using a global variable

i

as a counter, and presumably have defined

RLY1

to be

BIT6

in the program header.Every time a flag occurs (twice a second) the counter is incremented.If we’ve reached 120 (60 s at 2 Hz) the relay control is toggled and the counter resets.

The complete code for this example is in

interrupted-2_G2211.c

.For you impatient folks, feel free to adjust the timer period to whatever value preserves your sanity.

These two examples should get you started with interrupts.As always, feel free to ask questions or suggest other helpful tips!

[via: http://mspsci.blogspot.com]

 

If you did the exercise at the end of the last tutorial, you probably noticed that the program stopped working if you tried using any of the LPMs other than LPM0 or LPM1.The reason for this has to do with the clock that is chosen to power the timer.So let’s take a careful look at just what’s happening in the

limboG2211.c

program.

First of all, we set the DCO to the calibrated 1 MHz frequency.Pretty straight-forward here; the MSP430 MCLK timing the CPU will operate at very close to 1 MHz.What isn’t as obvious is that the sub-main clock (SMCLK) is also driven by the DCO, and so also operates at 1 MHz.

The timer is then configured to run with the SMCLK divided by 8, or at 125 kHz.The timer counts to TACCR0 and then triggers an interrupt, wherein the current through the speaker (or current to the LEDs if you changed it to that mode) is switched.The frequency of the switching drives the speaker to create a particular tone.

In between switches, the MSP430 enters LPM0.We can see what happens to the chip when entering this mode by looking again at the diagram and table on page 2-15 of the x2xx Family Guide: the CPUOFF bit in the Status Register is set to 1.The CPU turns off, as does MCLK, leaving SMCLK and ACLK running.If we run LPM1, then the CPUOFF bit is set as in LPM0, and in addition SCG0 is set to 1.We can see that this mode has the added benefit of turning off the DCO

if the SMCLK is not being used

.In our case, we’re using SMCLK to drive Timer A, so the DCO is

not

turned off for LPM1.Essentially, for this program, there is no difference between LPMs 0 and 1.

If we move to LPM2, however, we set CPUOFF and SCG1, which has a much different effect: in addition to the CPU and MCLK, SMCLK is disabled, allowing the DCO to also be disabled.In our program, then, when we enter LPM2 we turn off the clock driving Timer A; Timer A never reaches TACCR0, and an interrupt is never triggered to cause the MSP430 to exit the LPM.In LPM2, however, ACLK is allowed to continue running.If we drive Timer A with ACLK, then, we could use deeper modes and consume less power overall!Less power means more battery life and more efficient use of the resources in the MSP430 design.

ACLK is typically driven at low frequencies (comparatively), and so draws less power than does DCO, further improving our goal of reducing power consumption.By default, it is set to use a 32,768 Hz quartz crystal.Your LaunchPad kit came with a crystal that you can solder onto the board to use here.Alternatively, you can configure the LFXT1 clock source to run from an internal oscillator, similar to the DCO, that operates at a very low frequency (typically 12 kHz).This oscillator is referred to simply as the VLO.

Configuring LFXT1 and ACLK

If you’re planning to use the crystal oscillator on LFXT1, there’s likely little that needs to be done.The crystal that comes with the LaunchPad needs 12.5 pF capacitors for stability, and you might notice the SMD solder pads near the crystal location for capacitors.You can use discrete capacitors if you like, but the MSP430 is also configured with a few capacitances internally, and 12.5 pF is one of the choices.Using the crystal is a simple matter of soldering it onto the board and configuring the BCS+ module correctly.There are a lot of switches and controls involved here, so we’ll save crystal control for a later tutorial, and assume for now that we’re going to use the VLO to source the Auxiliary Clock.

Section 5.3 in the x2xx Family Guide (starting on page 5-13) holds the necessary information for configuring the BCS+ module.The registers we need for now are BCSCTL1 and BCSCTL3.In terms of setting up the VLO, we need the XTS bit in BCSCTL1 (bit 6) and the LFXT1Sx bits in BCSCTL3 (bits 5 and 4).The XTS bit sets the LFXT1 clock source to low or high frequency.Many MSP430s in the x2xx family let you use a high frequency crystal for this source; the value line devices, however, only support low frequency (ie. 32,768 Hz) crystals.When this bit is cleared to

0

 we have configured the chip for low frequency ranges.Some devices, including the value line chips that come with the LaunchPad, allow us to use the VLO.This mode is selected by setting the LFXT1Sx bits to

0b10

, as described on page 5-16.To do this in C, we again make use of a header file definition:

BCSCTL3 |= LFXT1S1;// sets LFXT1Sx to 0b10, VLO mode

We don’t need to explicitly change the XTS bit, as it defaults to the low frequency setting.This line is sufficient for setting up the VLO.

With LFXT1 now oscillating at 12 kHz, ACLK can be used for the peripherals.You may recall from Tutorial 08-a that ACLK only sources from LFXT1, using either a crystal or the VLO.We can, however, alter the frequency for ACLK by dividing the VLO by 2, 4, or 8.This is done in BCSCTL1 with the DIVAx bits (5 and 4) with the configurations given on page 5-14.For our purposes, we’ll set ACLK to run at about 3 kHz by dividing the VLO by 4.We do this with the following code:

BCSCTL1 |= DIVA_2;// ACLK divide by 4

Keep in mind that you can also divide the clock in the peripheral itself, or further divide inside the peripheral.For example, we can run Timer A at 750 Hz by dividing by an additional factor of 4:

TACTL = TASSEL_1 + ID_2 + MC_1 + TACLR;// use ACLK, div 4, up mode, clear

To summarize, we can modify the limboG2211.c code to use the following:

BCSCTL1 = CALBC1_1MHZ;// Running at 1 MHz
DCOCTL = CALDCO_1MHZ;
BCSCTL3 |= LFXT1S1;// VLO mode


TACCR0 = 14;// With the Timer using ACLK (12 kHz), this
// value gives a frequency of 12000/(TACCR0+1) Hz.
// For TACCR0 = 14, that’s 800 Hz.

TACCTL0 = CCIE;// Enable interrupts for CCR0.
TACTL = TASSEL_1 + MC_1 + TACLR;// ACLK, up mode, clear timer.

_BIS_SR(LPM3_bits + GIE);// Enter LPM3 and enable interrupts

This code sets ACLK to run from the VLO (at ~12 kHz) and then uses Timer A to switch the speaker at ~800 Hz.Most importantly, since we are now sourcing the timer which causes interrupts from ACLK, we can use LPM2 and 3 in our program.Note that LPM4 would turn off the ACLK; using LPM4 is only feasible if we have some external interrupt source, such as watching for a signal on one of the GIO pins.

We’ll return to looking at the low power modes soon; the first real scientific project we’ll do will analyze and compare the power consumption of a simple MSP430 design using the various LPM settings.To do this, however, we’re going to need to measure an analog signal and record data.The next tutorial will start us in this direction by looking at analog to digital conversion.

Reader Exercise:Suppose your LaunchPad has the crystal soldered onto it, and is being used in a project that does not use ACLK at all.The crystal will continue to oscillate, and use power (albeit a very small amount) that is not contributing to the overall design.Note that none of the standard LPMs disable ACLK and the crystal oscillator except LPM4.Write up the needed line(s) of code that would create a new LPM that keeps MCLK, SMCLK, and the DCO running, but disables ACLK and the crystal oscillator.

[via: http://mspsci.blogspot.com]

 

We’re almost at a point where we have built a complete scientific instrument. The one thing the capacitance meter lacks is a way to provide the measurement outside of the debugging environment– not very convenient for working in the field. There are a number of ways we can transfer the data somewhere usable. One way that is useful for single measurements is by displaying the result on an LCD display, much as consumer meters do. We’re now going to look at using a standard alphanumeric LCD module with the LaunchPad. This project will also introduce the concept of building a custom library; by the end of this tutorial, you will have a library that you can import into future projects to add an LCD display without having to re-code the entire thing.

The LCD Module (LCM)

One disadvantage to this tutorial is that it require having an LCD display, which you might not have. If not, and are willing to purchase one, I would recommend doing so. These displays can be very useful, and are not very expensive. They come in a variety of sizes and styles– for this tutorial we will use a standard 16×2 character alphanumeric display modules, such as those found at

SparkFun.com

. SparkFun carries a variety of these modules; feel free to pick one to your liking, but be certain of a few things: it needs to be configured to run on 3.3 V and should not have been modified to accept serial input. If you’d like a larger module (such as a 20×4 display), it’s up to you, as they all work the same way. Get whatever color you’d like.

The SparkFun LCM’s utilize an ST7066 chip as the interface to the display, which is based on the ubiquitous HD44780 interface. (If the names make this sound technical, don’t worry too much about them– the important thing here is that we have an interface that translates data from the MSP430 into the commands and characters needed to control the LCM.) This interface uses an 8-bit parallel transmission for sending data to/from the display. As you can imagine, with 8 bits for data, plus another 3 bits for control, you can very quickly run out of GIO pins on your MSP430. In fact, even if we use the G2211 device without a crystal so that P2.5 and P2.6 are available, we only have 10 GIO pins available in total, so we’re short 3 pins to control the LCM (needing 11 pins) and the comparator interface (needing 2 pins) for our meter. Fortunately, the HD44780 interface (and thus the ST7066) provides a means of sending data in two 4-bit chunks, and as long as we have no need for reading data from the LCM, we can get by with 6 pins for LCM control, allowing us to just fit the comparator and the LCM into the 8 GIO pins for P1 on our G2211. We’re stretching the limits of this chip at this point, which is excellent motivation for expanding to other MSP430 devices in the future!

Connecting to the LCM

The standard LCM module has 16 pin connections (14 without a backlight). The first few connections are for power (possibly in two places, if the LCD is also backlit) and contrast adjustment. Pin 1 (labeled as Vss) should be connected to your LaunchPad ground. Pin 2 (Vdd) is connected to the LaunchPad Vcc. If you are using the backlight, pin 15 (LED+) is also connected to Vcc and pin 16 (LED-) to ground. (This is most easily done on a breadboard. If you find yourself losing track of this description, a good guide to follow is one written by

LadyAda

.) Pin 3 (Vo) controls the contrast of the screen. If you have a 10k potentiometer available, tie it to the wiper and tie the two ends to Vcc and ground for an adjustable contrast. If not, you can just ground this pin; it probably won’t look as good as it could, but it will work.

That leaves 11 pins for the control and data lines. The three control lines are pins 4 (RS), 5 (R/W), and 6 (E). The read/write (R/W) pin is not necessary here, and by tying it to ground we keep the LCM always in write mode. We won’t be able to read anything from the LCM (such as the address of the cursor, the busy state flag, etc.), but it saves us a pin on the MSP430. The Register Select (RS) and Enable (E) pins are what we’ll use to control the LCM. Finally, pins 7-14 are the data lines D0-D7 respectively. You can consider these pins much like the P1 pins on the MSP430– D0 is the first bit, D1 the second, and so on. If we used an entire GIO port on an MSP430 to control the data lines, we could connect Px.n to Dn, and by writing a value to Px, we can write the same value to D (conveniently saving us from any strange coding to accommodate changing the order). Since the G2211 doesn’t provide enough ports to do this, we’ll use the 4-bit mode instead. This mode uses only D4-D7. Leave D0-D3 unconnected for now. (Doing so prevents accidentally writing commands we don’t intend.)

For the capacitance meter, we’re going to change some of the pin arrangements to accommodate the LCM. We’ll use P1.1 as TA0.0 rather than CA1, and use P1.2/CA2 as the input to V+ on the comparator. P1.0 will control RS, P1.3 will control E, and P1.4-7 will control D4-7.

Note: P1.3 is also connected to the button– this shouldn’t affect the code, but since there is a pull-up resistor on P1.3, there will be excess current whenever we drive E low. Unfortunately, the LaunchPad is not designed with a jumper like on P1.0 and P1.6 for the LEDs, so we’ll just have to live with this. While we’re on the topic, be sure to remove the jumpers for the two LEDs and on the TXD/RXD pins.Sending Commands to the LCM

Once we’re wired up, sending commands or characters to the LCM is a straightforward task. In fact, it can even be done by hand, without a microcontroller at all! (If you’re interested in this, or would like to know more about what’s going on, see the Reader Exercise below.) The basic idea is that we write an instruction to the data pins and pulse E. The instruction is sent on the falling edge of E, which is why it’s pulsed. The instruction issues a command if RS is low, and sends a character if RS is high.

As an example, let’s look at the commands we need to set the LCM in 4-bit mode. The 8-bit binary instruction

0b001nnnxx

is the “Function Set” instruction. (Here, the n’s represent values we choose for the configuration, and the x’s imply an unused bit– these can have either 0′s or 1′s and not affect the instruction.) Bit 4 in this instruction sets the interface mode: a 1 sets the LCM to accept 8-bit instructions, and a 0 sets it to accept 4-bit instructions. So by sending the instruction

0b00100000

(or

0×20

), we configure the LCM to accept commands and characters in two 4-bit chunks instead of one 8-bit chunk. This command must be issued first in order to do anything with our 6-wire setup. We first set the data lines with

P1OUT |= 0×20;

(which also sets RS low (command mode) and E low in this wiring scheme) and then send the command by pulsing E.

The LCM does not respond instantly to the command, and there are some strict timing requirements in order for it to work correctly. Specifically, RS must be set low a certain amount of time before beginning the pulse on E. The data lines must be set a certain amount of time before the falling edge on the pulse, and must be held at that value a certain amount of time after the pulse. Then a certain amount of time must elapse before we can pulse E again. Fortunately for us, the only timing value of major concern is the time between command pulses. The other times are on the order of a few hundred nanoseconds, and at the processing speeds of the MSP430 there is enough latency to accommodate them. The amount of time needed to complete a command before accepting another can be on the order of 150 milliseconds, however, and so delays must be incorporated to handle them.

So, to recap, here’s the set of instructions needed to set the LCM in 4-bit instruction mode:

__delay_cycles(10000);// wait for the LCM to settle on power-up
P1OUT |= 0×20;// set to 4-bit instructions
P1OUT |= BIT3;// E high
__delay_cycles(200);
P1OUT &= ~BIT3;// E low
__delay_cycles(200);
P1OUT &= 0x0F;// clear the upper 4 bits

Though E can be set high before setting the data lines, it’s convenient to switch the order to prevent any timing mismatches. Note that if you use different pin connections, or especially if you use multiple GIO ports, this code won’t work exactly as is; it’s convenient to use P1.4-7 for D4-7 to be able to assign directly to P1OUT, but this is not general. If we were to swap the order, for example, to P1.4-7 as D7-4, then we would be writing

0b0100

instead of

0b0010

on the data line with this code. So be careful; either use the pin connections I’ve suggested here, or assign the data line bits individually as needed. The final line clears the data line bits to make it easier to set the next command properly.

Other Initializations: Sending Commands in 4-bit Mode

Now we have our LCM ready to accept 4-bit commands. This mode works by sending the upper 4-bits (or nibble) with a pulse on E, and then sending the lower nibble with a second pulse. With our wiring scheme, we can do this easily by the following code:

P1OUT |= ( & 0xF0);// send upper nibble
pulse();
P1OUT &= 0x0F;// clear
P1OUT |= (( & 0x0F;) << 4);// send lower nibble
pulse();
P1OUT &= 0x0F;// clear

I’m assuming here that I’ve lumped the commands to pulse E with the incorporated delays into a function

void pulse(void)

.

refers, of course, to whatever 8-bit command or character we’re sending to the LCM. If we encapsulate this set of commands into a function

void SendByte(char)

,then we can issue the next initialization commands as follows:

SendByte(0×28);// Function Set 4-bit, 2-line mode (for 2-line displays, of course)
SendByte(0x0E);// Display on, underline cursor on, non-blinking
SendByte(0×06);// Character entry mode: increment address, no display shift

After sending these commands, our LCM is ready to display whatever characters/text we want to send it. Note that to send characters, the commands are similar to above, but P1OUT must also set BIT0 (RS) to tell the LCM to receive character instructions rather than commands. In

lcddemoG2211.c

, I demonstrate this with a more generalized version of SendByte that lets you send commands and characters. It also demonstrates other commands, such as clearing the display and moving the cursor. If you have an LCM, try out the code yourself. I wouldn’t use a DCO faster than about 2 MHz with the selected delays, so if you play around with the code keep that in mind. In the next tutorial, we’ll look at how to encapsulate all of this into a custom library that we can keep on hand and how to import it into our capacitance meter project.

Reader Exercise: Not really an exercise– more suggested reading. If you’d like to know more about how the modules work and the specific commands and characters that can be sent, I suggest reading the following two articles from Everyday Practical Electronics:
How To Use Intelligent LCDs: Part One
How To Use Intelligent LCDs: Part Two
These articles are very easy to understand, and do a great job of explaining how to use the LCM.

[via: http://mspsci.blogspot.com]

 

Before I start this tutorial, let me add a caveat: I have a feeling this is not the best way to build a library in CCS. It is, however, the only way I could get it to work reliably short of copying the code into every project I use it in. If anyone has some experience with this in CCS, please send a comment and let me know!

We have code that will let us easily send text to the LCM, which would be very useful to have in a library that can be called up as needed, without having to rewrite (or copy-paste) the code every time. The C language makes doing this fairly easy, and so we’ll look now at moving the LCM code into a library and go through how to configure a project in CCS to use the library. You should be able to add any code you’d like to reuse to this library and be able to call it up whenever needed.

First: choose a location to keep your library. It’s not important where this library resides (from the compiler’s point of view), but it’s best to have it somewhere easy to get to when you add/change code in your library. At the same time, it should be somewhere safe, where it won’t be accidentally deleted, moved, or changed in any unintentional way. I chose to create a folder in my workspace directory called ‘library’.

Second: copy any

#include

,

#define

, function prototypes and global variables into a new header file. For this library, I’ve called it

simple_LCM.h

. If you’re going to use definitions specific to the MSP430, you will need to include the MSP430 header as well. To keep your library general, rather than including the header file for a specific device, just

#include <msp430.h>

.

Third: copy the remaining code (the encapsulated functions) into a new .c file with the same name (ie.

simple_LCM.c

in this case). At the top of the file, you should add

#include <filename.h>

 (replacing filename with the name of your library file). Note that this file

should not have

a main function in it.

Fourth: in your new project, right click the project folder and select new → folder. Click the [Advanced >>] button, and select “Link to folder in the filesystem”. You can then browse to your library folder and finish adding the folder.

Any files in your library directory are now available for use in your code; the compiler, however, needs to be aware of the path to this folder to find it. (This is the part I don’t like; this has to be done for every project, and I’m unable to find a way to make this path be a default in CCS for every new project.)

Fifth: right click your project folder and select properties. Open the C/C++ Build window, and in the Tool Settings, look for MSP430 Compiler → Include Options as well as MSP430 Linker → File Search Path. Both of these need to have your library folder added to the list in order to compile your code.

One shortcut I’ve found: In CCS, go to the menu Window → Preferences, then navigate to General → Workspace → Linked Resources. Here you can define a path variable (eg. My_Library) that links to your library directory. When you add a new folder to a project, instead of browsing to the folder location, you can click [Variables...] and select it from the list; it’s much quicker that way. Unfortunately, I can’t seem to get the project properties changes to recognize the path variable, though it seems it’s supposed to.

Now we should be ready to build our capacitance meter using the LCM. The code I’ve written in

CMeterLCMG2211.c

demonstrates a number of new ideas using the LCM. Browse the code and examine the comments to see how it works. Note the use of

MoveCursor(row,col);

and the particular commands sent to configure the LCM.

While the simple_LCM library has a routine for printing strings, what happens when we want to print an integer value like the recorded value in the 

time

 variable? One intuitive option (at least if you’re accustomed to programming in C) would be to use the stdio library and the function

sprintf();

. All we would need to do is set up a character array such as print_time[10], and use sprintf(print_time, “%d”, time); to put the integer into the print_time string and pass it to PrintStr(). Unfortunately, this method has some serious problems for microcontroller use. First of all, even with the heavy streamlining done in CCS to reduce its size, any code using a printf function will be large. In this program, it would exceed the 2 kB of size available in this device. Second, the streamlining makes it difficult to format correctly; ideally, we’d use a %10d format specifier to put time into exactly 10 places to fit the print_time size. We can’t do this with the streamlining implemented. We can change the printf assumptions in the project properties, but that makes the function use even more of our severely limited code space.

Fortunately, there are some ways around this problem. For an integer, we can pick off the individual digits by using the mod operator and integer division.

x%10;

will return the last digit of the number stored in x.

x/=10;

will remove the last digit and leave up to the second to last. By running a loop over the number until we reach a condition of

x == 0

(no more digits), we can pick off each digit to print one by one. The ASCII codes (and the codes for the LCM) are arranged in a way such that the lower nibble corresponds exactly to the digit’s value, so

0×30 + 0

is “0″,

0×30 + 7

is “7″, and so on.

The disadvantage to this loop technique is that the digits are picked off in reverse order–from right to left. The LCM has a mode that allows you to decrement the cursor position when you send characters, however, so it’s possible to print from right to left in this way. (In fact, this ability is used in many hand-held calculators.) See the code for the exact code needed to configure the LCM for this mode.

And there’s our first complete scientific instrument using the MSP430. We use a combination of the timer and comparator with a calibrated clock to measure the decay time in an RC circuit. The LCM displays the measured time in microseconds. Knowing the value of R and the reported time, we can calculate the actual value of C measured by the meter.

Reader Exercise: This works fine, but wouldn’t it be nice to have the LCM display the capacitance rather than the time? You can do floating point operations in the MSP430 (albeit inefficiently), but how would you display a floating point number on the LCM? If sprintf was to big for the program above, it will definitely be too large in this case. Can you come up with a way to display the capacitance without exceeding the 2 kB limit for the G2211 device? If you get stuck, one way is demonstrated in CMeterLCMFull.c. It also has the benefit of being auto-ranging. This code takes up 1934 bytes of space– just barely enough to squeeze into the G2211!

[via: http://mspsci.blogspot.com]

 

The idea behind the capacitance meter is simple, but (as may be evidenced in part by the time it took me to get to this) there are some particulars that need to be resolved.

First: how do we minimize any delay in the timing due to delays from carrying out instructions? If we’re not careful, a non-negligible amount of time can pass between when we start the capacitor discharge and the timer, or between the comparator trigger and the timer capture.

Second: how do we determine what capacitances can be measured for a given configuration? Smaller capacitors will discharge more quickly, leading to shorter measurement times (meaning more error due to the digital nature of the timer). Larger capacitors take more time to charge up, and may not be fully charged when we start discharging.

The first issue is easily resolved by using features built into the Timer_A module. The second is less easily resolved, but easily understood, so we will know in advance the limitations of our code. There are a couple of things we’ll be able to do to improve it generally, but for our purposes here we won’t be too concerned about it.

New Feature of the Timer_A Module: Output

Let’s introduce here two features in Timer_A we haven’t used yet. First off, let’s look at how we can use the timer to change an output. Recall that the Timer_A module has a certain number of “capture/compare” registers built into each MSP430 device. (The G2211 and G2231 have two.) Each of these registers can drive their own output; we can program the MSP430 to adjust the output every time the timer reaches the value stored in the register. For example, we can set the output TA0.1 to set the output (to 1, that is) every time the timer reaches the value set in TACCR1. Or we can toggle the output TA0.0 every time the timer reaches the value set in TACCR0. We also have modes that allow us to use

both

registers on the same output, giving one result at TACCR1 and another at TACCR0. (This ability is used for pulse width modulation, or PWM. We’ll talk about that in an upcoming tutorial!)

Take a look at table 12-2 in you x2xx Family User’s Guide:

This table lists all of the possible modes we can use to work with the timer outputs.

New Feature of the Timer_A Module: Capture

So far we have only discussed uses of the timer in compare mode. The other mode, capturing, can be used to do precise timing of events. In capture mode, the timer records the value in TAR into TACCRx at the moment when the capture is triggered. This trigger can come externally, or it can come from internal connections to other modules, including the comparator. By configuring the timer in this way, we can record the timer value when the comparator output provides either a rising edge (going from 0 to 1) or a falling edge (from 1 to 0).

Putting It All Together

Here’s the general concept used in the capacitance meter. Note that accurate timing requires a calibrated clock, so we’ll use the calibrated 1 MHz DCO for this project. First, we connect the resistor to a timer output. (For this purpose, we’ll use the TA0.0 output.) The junction between the resistor and capacitor is tied to a comparator input, and the other end of the capacitor is connected to ground. We start charging the capacitor by setting TA0.0, and wait some specified amount of time for the capacitor to charge. The output is then reset (grounded) at the time specified in TACCR0. While the capacitor’s voltage is above the reference voltage, the output is set at 1 (assuming we tied the RC circuit to V+ and the reference to V-). When it drops below that value, the comparator output falls, triggering a capture in the timer, recorded in TACCR1. The difference in time between TACCR1 and TACCR0 is an accurate measurement of the decay time of the RC circuit from Vcc to Vref. (Note that if TACCR0 is 0, no subtraction is needed.)

Obviously the longer the time it takes to fall, the more accurate our timing measurement will be overall. But what happens if the time is longer than 2^16 microseconds? TAR rolls over, and starts counting over again; so in our code, we’ll need to account for any rollovers that may occur.

Let’s examine the comparator configuration used in the code:

void CAinit(void) {
CACTL1 = CARSEL + CAREF_1;// 0.25 Vcc ref on – pin.
CACTL2 = P2CA4 + CAF;// Input CA1 on + pin, filter output.
CAPD = AIN1;// disable digital I/O on P1.7 (technically
// this step is redundant)
} // CAinit

This sets up the comparator to use CA1 on the V+ input. If you check the MSP430G2211 datasheet, CA1 is connected to P1.1. Looking up the register description in the Family User’s Guide, we configure for CA1 on V+ by setting P2CA4. (P2CA0 also controls V+, and for CA1 should be clear.)

Now let’s look at the timer configuration:

void TAinit(void) {

TACTL = TASSEL_2 + ID_0 + MC_0;// Use SMCLK (1 MHz Calibrated), no division,

// stopped mode

TACCTL0 = OUTMOD_1 + CCIE;// TA0 sets VCTL at TACCR0

TACCTL1 = CCIS_1 + SCS + CAP + CCIE;// Use CAOUT for input, synchronize, set to 

// capture mode, enable interrupt for TA1.

// NOTE: Capturing mode not started.

} // TAinit

The timer is set up without starting. Setting TACCTL0 to OUTMOD_1 sets the output TA0.0 when TAR reaches TACCR0, which is 0 by default. Enabling the interrupt lets us keep track of overflows. In TACCTL1, we change to capture mode by setting CAP. To know how to connect the comparator, we need to check the device datasheet. Find the table called “TIMER_A2 SIGNAL CONNECTIONS” and make sure you’re looking at the one specific to devices with COMP_A+. In the Device Input Signal column, find CAOUT (internal), and note the Module Input Name in the column next to it: CCI1B. The CCISx bits in TACCTLx select the input, and in the Family User’s Guide, we see that these two bits should be set to 0b01 to select CCIxB. In the device’s header file, we find that we can set these bits with CCIS_1.

Also note that by setting SCS, we synchronize the capture to the timer clock. We haven’t started capturing yet, just as we haven’t started the timer. The code is set up to wait for the user to push the button connected to P1.3. Doing so exits LPM0, and continues the main code. The following then happens:

First, the timer is turned on. When the timer rolls over the first time, TA0.0 (on P1.5 in this code) is set, charging the capacitor. We want to wait long enough for the capacitor to charge. The code is set to wait for 10 overflows, which corresponds to about 655 ms. At this point, the comparator is turned on, and the timer is configured to reset TA0.0 at the next overflow (so we’ve actually charged for 11 overflows at this point). We let the timer capture the next event, or when the voltage at the capacitor (on P1.1/CA1) drops to the value at Vref, or 1/4 Vcc. At this point, an interrupt is triggered. The interrupt routine turns off the captures and the timer and returns to the main code. The code loops back, and starts the process again, waiting for the user to press the button to start a measurement.

Note: Before running the code as set up, keep in mind we’re using P1.1; what else is this pin used for? You’ll want to remove the TXD jumper to get the project to work properly. Thanks to RobG over at www.43oh.com for pointing this out to me… I was really puzzled by it for an embarrassingly long time!

Try running the code found in

CMeterG2211.c

. To do this, you’ll need a resistor and a capacitor. Use a 10 kO resistor and a 100 nF capacitor (it will have a label that says 104 on it) if you can. In the debugger, set the code to run freely, and push the button. The led should switch from green to red, then back to green, indicating the measurement is done. The timer has captured the event and recorded the time in TACCR1. Unfortunately, there’s no way to see this as is yet! Pause the debugger, and examine the timer_a2 registers. The time is recorded in TACCR1. For the suggests RC combination, you should have a value somewhere around 1400. (You can change from the default hex format to decimal by right-clicking the register and selecting decimal in the format menu.)

Try increasing R to 100 kO. You should see the time increase by a factor of 10. Try using a 1 uF capacitor. (You might need to increase the number of overflows to wait while charging to get something accurate here.) In the watch window, we can view the value in the overflows variable. To do this, click where the window says , and type in the variable name. With such a large capacitor, you should see the number of times the timer wrapped around before stopping. With the measured time and the known resistor value, you should be able to use the formula provided in the previous tutorial to calculate the value of the capacitor. Feel free to experiment with this program. In particular, how consistent are your timing measurements?

Aside from the lack of ability to see the timer value without using the debugger, there are a few issues to work out still in this meter. We’ll take a careful look at some of these in the future, and bring back this code to demonstrate ways we can see data and ways we can improve the timer.

Reader Exercise: Given the discussion in the previous tutorial about the accuracy of the meter, and from your own experimentation with the program, where are the major sources of error in the measurement? How consistent is the timing? What might cause the inconsistency? What assumptions have we made in the way we measure the capacitance? Once you’ve built an instrument to make any kind of scientific measurement, it’s important to identify all of these aspects. By doing so, we identify the limitations of the instrument, both those we can fix and those we have to live with. 

[via: http://mspsci.blogspot.com]

 

A number of comments in the community lately have suggested a note on powering the MSP430 outside of the launchpad would be helpful; to that end I’m writing up this design note to help explain the options and requirements for powering the MSP device.

Chip Configuration

The MSP430 has two pins for power that correspond to the high and low states for the digital logic: Vcc and Vss respectively.For the MSP430, Vss is typically ground and Vcc is usually in the 1.5-3.6 V range, depending on the application.(On the value line devices, these are labeled as DVCC and DVSS, with the D referring to the digital circuitry.Some devices also have a separate AVCC and AVSS for analog signals and peripherals.On these devices, you can tie the A and D supply pins to each other, but the ability to use separate supplies is there.)

Regardless of your chosen power source, there is one thing that must absolutely be done in all designs.Every MSP430 device has a RST/NMI pin.This pin allows you to reset the chip externally by grounding that pin.(The non-maskable interrupt feature is something we’ll look at in a more advanced tutorial sometime in the future; for now, only the default operation of the pin is considered.)The LaunchPad is designed with a button that does just that.

In the button tutorial that for a button to behave as an input, the pin needs a definite default state (either grounded for active-high buttons or Vcc for active-low buttons).In order for a chip to be powered, the RST pin must be tied to Vcc.Note that a direct wire is not a safe method of doing this; if a reset is triggered and the pin is grounded, it would short out your power supply.A pull-up resistor is required, and a careful look at the

LaunchPad schematic

(it’s on page 15) shows that TI uses a 47 kΩ resistor, and that seems a reasonable choice for the typical supply voltages one would use for any MSP430 design.

Note in the schematic one other aspect: on the Vcc pin, there are two capacitors.One of these is a 0.1 μF capacitor (100 nF if you’re unfamiliar with SI prefixes) and the other a 10 μF capacitor. These capacitors are used to filter the power input, by which we mean keep any fluctuations in the power supply from affecting the value of Vcc.You will see below that many suggested designs for regulated power supplies use a 10 μF capacitor or similar on their output, and you may be able to get away with not using another on the MSP430 if you use one of these power sources.(You might have also noticed a small 1 nF capacitor connecting RST to ground.While not absolutely necessary, if you find your design has sporadic resets that you can’t explain, add this capacitor.A TI support technician tells me that for SBW programming to work properly, it should be a capacitor of about 1 nF in size.)

Unlike the larger filtering capacitor, the 0.1 μF should always be included unless you have good reason not to.Digital circuits, especially when run at higher frequencies, can be susceptible to noise from pins switching between high and low.This capacitor is used specifically to filter out that noise, and works best if it can be physically located close to the Vcc pin on the MSP430.If you plan on making your own board designs rather than relying solely on the LaunchPad, get a number of this size of capacitor, as it will be used a lot!

Power Options

There are a huge number of possibilities for powering your circuit: solar panels, large super-capacitors, even various fruits will supply enough electrical power to run your MSP430 designs!For most hobbiest work, however, there are three major places from which to draw power: batteries (consumer type, like you’d buy at the grocery store), the wall AC, and the USB port of a computer.

Batteries are sometimes the easiest choice, but there are some caveats to consider.For the most part, a typical battery holder with a couple of wires can be connected to Vcc and Vss.On the LaunchPad, there are pins for this purpose; soldering female header pins to the wires or using jumpers make battery use simple.Before just connecting up, however, remember that the MSP430 can’t handle voltages higher than about 4 V.A 9V battery might sound like a good idea, but alone is not safe to use on the microcontroller.

Good combinations of batteries: 1 or 2 alkaline AAA, AA, C, or D cells would give 1.5 V or 3 V, both good supply voltage values.If you switch to a rechargeable battery, double check the type.Many rechargeable cells are nominally 1.2 V rather than 1.5 V.If you use the 1.2 V cell types, you can use up to three of these in series (a total of 3.6 V) without trouble.Coin cell batteries also work well if you have a holder for them.In general, batteries need no other external parts, though it may be a good idea to have both filtering capacitors on the MSP430 in this case.

If you use a higher voltage battery or battery combination, or if you want to use the USB power (5 V), you will need to step down the voltage to an acceptable level.While there are a number of ways to do this, perhaps the simplest and most effective is to use a voltage regulator.(The LaunchPad is powered from USB; the portion of the board dedicated to the SBW programmer includes a voltage regulator to drop the USB line voltage to 3.6 V.)These components are cheap, robust, and work very well when used properly.Some can even handle such a wide range of inputs that you could connect most any common power source to it and have the output you need.If you’ve never used a voltage regulator before, read up about it and learn how to use them.An excellent tutorial can be found at

SparkFun

, and is a good place to start.For my designs, I like to use Linear Technologies regulators, such as the

LT1763

.(Note that the suggested design on the LT1763 page includes a 10 μF capacitor on its output.)This particular regulator is only available in surface mount packages, but Linear makes a number of other varieties that are comparable in through-hole packages (as an example, the

LT1965

).There are plenty of options out there that fall into the voltage ranges useful for the MSP430.

If you want to use a wall outlet, you will also need a transformer with a rectifier.Plenty of stores carry such power supplies in a variety of voltages.Anything in the 1.5-3.6 V range will work directly, but I’ve found most wall-wart supplies are 5 V or higher, and will require a regulator to operate the MSP430.

There are many, many more ways to power your circuit, but these will give you a few reliable sources to start from.Remember to tie the RST pin to Vcc with a resistor, add a 0.1 μF capacitor near the Vcc pin, and keep your voltage within the usable range for the MSP430.Don’t forget to connect ground (or the negative side of the battery as the case may be) to the Vss pin, and have fun exploring your power options!

[via: http://mspsci.blogspot.com]

 

Now that we have some degree of control over the MSP430, it’s a good opportunity to introduce the low power mode.This feature is arguably one of the most important for the MSP430, and is an excellent reason for choosing this microcontroller over many others.Let’s look at how the modes work.

The operating mode of the MSP430 is controlled with four different bits in the status register.These bits are called CPUOFF, OSCOFF, SCG0, and SCG1.The first two are fairly self-evident; the first of these controls the power to the CPU of the MSP430.The second controls the power to the external oscillator (ie. crystal usually).In addition, these bits turn on/off MCLK and ACLK respectively, since those are associated with the CPU and LFXTL.The other two bits add more control over SMCLK, the DCO, and the DC generator.

In the

x2xx Family Guide

, page 2-15 shows a diagram and table explaining how the operating modes work.For our convenience, TI has defined 6 different modes that are easily accessible to us via the header files.The MSP430 starts off in Active Mode, where the CPU and all clocks are up and running.This is the mode in which we’ve been running up until now.Any time the MSP430 needs to do something, it will be in Active Mode.That’s not to say that peripherals can’t operate in the other modes.There are 5 low power modes, labeled LPM0 through LPM4.These are the commonly used configurations, and only rarely will you need one of the other 10 possible modes available with a 4-bit configuration.(One exception to that would be turning off an unused crystal oscillator that’s soldered to your LaunchPad, but not being used in your current project.More on that later.)

the

_BIS_SR()

function we saw earlier is perhaps the easiest method of using the LPMs.The header files define a set of names called LPMx_bits to help with the bit management.To enter a low power mode, simply issue the command

_BIS_SR(LPMx_bits);

with x replaced by the LPM number you’ve chosen.Keep in mind that if you want to use interrupts, you will also have to add GIE to the argument, or at least

_enable_interrupt();

before going into the LPM.

Another good thing to keep in mind is that the LPMs are distinct; you don’t need to include LPM0 to use LPM1.In fact, doing so (eg. using a command such as

_BIS_SR(LPM0_bits + LPM1_bits + GIE);

) would have undesirable side effects by adding the binary values that the header file definitions provide together.

One of the features of the MSP430 architecture is that you don’t need to tell the CPU to wake up on the interrupt, nor do you need to put it back in the LPM manually.All of this is done automatically; when an interrupt is triggered, the Status Register is saved to memory (pushed onto the stack) and reset, waking up the CPU.Once the ISR has been satisfied, the original Status Register (including the LPM setting) is popped off the stack back into its place, and the MSP430 returns to the low power state.If you require your program to wake up the chip and stay awake, you will need to include a line in your ISR to do so:



_BIC_SR(LPMx_bits);// clears the bits corresponding to LPMx and exits the low power mode

Note the different function name; _BIS_SR() is Bit Set Status Register (ie. set to 1) while _BIC_SR() is Bit Clear Status Register (ie. clear to 0).

Example using the Low Power Modes

As an example, I’ve coded up a

simple program

that plays a tone on an 8Ω speaker.The speaker I have has two pins with 0.2″ separation, and so it plugs right into the headers on the LaunchPad.To accommodate the spacing, I’m using P1.0 and P1.2 to drive the speaker.If you don’t have a speaker available, you can simulate the same idea using the two LEDs on the LaunchPad; just follow the instructions in the code and change SOUT to BIT6 and adjust the frequency accordingly.

The idea is simple; we start with P1.0 high, P1.2 low, and switch each at a given frequency.This method provides a square wave, not a pure tone, but it does work.Keep in mind that audible frequencies are generally in the 100′s and 1000′s of Hz.It might be interesting to see how low of a frequency you can hear (clicking doesn’t count; listen for an actual tone) and how high you can hear.Also, you can keep one pin constantly low and switch only the other if you want to reduce the volume.

There’s more to explore with the low power modes, and next time we’ll start playing with the low frequency oscillator to drop into the deeper sleep modes.

Reader Exercise:This tutorial’s code uses LPM0.Try out the other low power modes by using LPMx_bits in place of LPM0_bits.What happens if you try to use LPM1?LPM2?LPM3?LPM4? Can you explain why some of these work and others don’t?Those that work, is there any actual difference between the modes as implemented for this program?As you answer these questions, think about what clock is sourcing the timer and what oscillator is used for the clock.

[via: http://mspsci.blogspot.com]

 

I ran across an interesting article in reading the news during my lunch break today.A group at the University of Washington has been doing

research

in smart sensors that communicate through the established power lines in a house.The page doesn’t say specifically, but given the power consumption they mention I wondered if this was based on an MSP430.A

close up

of the photo of the board looks like it might be, and you can just make out the TI logo on the chip.

Digging a little deeper

, I confirmed that yes, this instrument is based on the MSP430.The team is using a surface mount variation of the MSP430F2013.This chip is also available in a 14 pin DIP package that is compatible with the TI LaunchPad.Very cool idea.

In other news, my project is now at integration, and I have some extra time on my hands.Watch for a couple new tutorials coming this week!

[via: http://mspsci.blogspot.com]

© 2011 Geko Geek This is a news aggregator website. Articles and images are copyrighted to their original source authors. Gekogeek takes no responsibility about the articles content Suffusion theme by Sayontan Sinha