# Connexions

You are here: Home » Content » C Language Programming through the ADC and the MSP430

### Recently Viewed

This feature requires Javascript to be enabled.

# C Language Programming through the ADC and the MSP430

Module by: Matthew Johnson. E-mail the author

This lab covers the basic principals behind Analog to Digital Conversion, as well as the basics of programming in C. You are expected to have some background in C from class, but if you are confused, see this basic reference.

1. Using Code Composer Studio 4, write a C language program turning your MSP430 LaunchPad into a simple 10 level voltmeter. Your program should divide the 0-3.3V input range of the ADC into 10 zones, and then output from a 0 to a 9 on the LED display depending on the input voltage. DO NOT EXCEED AN INPUT VOLTAGE OF 3.3V. You will damage your circuits and destroy your MSP430. Assignment Details

## The ADC and "C" Through a Practical Example

### Interfacing with the Analog World: The ADC

ADC's play an incredibly important role in digital electronics and DSP. ADC stands for Analog to Digital Converter, and it does exactly what you would expect it to. It samples an external voltage, and then converts that voltage to a binary number compared to the reference voltage range from Vdd to Vss. (In plain English terms, the ADC samples what fraction the input is of some maximum allowed reference voltage.) The ADC's result gets written to a memory mapped register, where the programmer can access it and use it in his or her code.

An ADC has a finite voltage range it can safely convert (usually related to its power supply range, but not always). The precision of the converted sample is related to the number of bits used by the ADC. More bits means more precision (more finite "slots" into which the infinitely variable analog single can be quantized) and a lower "quantization error." To learn more about error and ADC, see this except from the Introduction to Electrical Engineering course notes. ADC's also have a maximum sampling rate specification (how frequently the ADC can make a conversion), but in this course we will be sampling very low frequency signals, so we won't need to worry about it.

The MSP430 G2231 has one 12 channel 10 bit 200Khz ADC. ADC channels allow the single ADC to select between several different signals (such as two different analog inputs on different GPIO pins) like an analog multiplexer. In the G2231, channels 1-8 are connected to the 8 P1 GPIO pins, and channel 10 is connected to the chip's internal temperature sensor. You can select which channel to convert by setting the  ADC10CTL1 register's (10 bit ADC Control 1) INCH property (Input Channel).

For this lab, we will configure the ADC to use the internal 3.3 Vdd as the reference voltage.

• A voltage of 3.3V would result in the ADC register holding 11 1111 1111 (0x03FF)
• A voltage of 0.0V would result in the ADC register reading 00 0000 0000 (0x0000)
• A voltage of 1.65V would result in the ADC register reading 01 1111 1111 (0x01FF)
• The ADC will have a sample resolution of 3.3V/1024 [Voltage Range/2#Bits], or .0032 Volts.

The ADC is a peripheral device, so it operates independently from the CPU. It has several operation modes (configured by writing to its control registers).

Definition 1: Peripheral
A device that can operate independently from direct CPU support and control. Peripherals often rely on interrupts to notify the CPU when they have completed some given task or need attention, and use independent control registers for configuration. The ADC 10 is a peripheral, as well as the MSP430's UART (serial controller) and timers.

• Single Sample and Hold-- the ADC10 will start a conversion when triggered by the CPU. After that conversion, it will hold the converted value in the ADC10MEM register and automatically go into sleep mode until signaled to begin another conversion. We will mostly use this mode.
• Sequence of Channels Sample and Hold-- the ADC10 will convert a series of different channels once, and store the result to ADC10MEM.
• Repeat Single Channel Mode-- it will continuously sample on channel, continuously updating the ADC10MEM register.
• Repeat Sequence of Channels Mode-- the ADC will continuously sample through a series of channels.
The MSP430's ADC 10 also has a built in memory controller. We won't be using it, but it becomes important when using the repeat modes. The memory controller can automatically write the ADC data into main memory as conversions finish, bypassing the CPU.

The G2231's ADC can run off of one of several available clock signals of varying speeds. The ADC10 also has a clock divider that can further slow the conversion speed by up to a factor of 8. Once a sample has been captured, it is held ready in the ADC10MEM register for a defined number of clock cycles. Since we are concerned with a low frequency signal, we will want to slow down the ADC10 as much as possible. (Students who have had Elec241 will notice some fundamental flaws in the assumptions made regarding high-frequency noise, but in practice this has very little effect on the final results). Even in its slowest mode, the ADC10 will still sample too quickly, so the use of some kind of moving average will help stabilize its DC readings.

### Controlling the ADC10 in C

#### C Basics

Your C program will be structured similarly to its assembly language counterparts, but with a much different syntax. Like before, the register names are all pre-defined in the "msp430x20x2.h" header file. To set a register, now just use an equals sign and set it like any other variable. For example, you will want to disable the watchdog timer in the first line of your program. WDTCTL=WDTPW+WDTHOLD; The compiler will execute the void main(void) function first. From that function, you can call any other functions or run any loops that you would like.

##### C Skeleton Program


#include  "msp430x20x2.h"

//Global Variable Declarations

//Global Function Declarations

void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
//Other Setup

//Can call other helper functions, loops, etc.
}



The ADC10 has two main control registers ADC10CTL0 and  ADC10CTL1, and two analog input enable registers ADC10AE0 and ADC10AE1 (10bit ADC Analog Enable 0/1). These registers control all the timing and conversion aspects of the ADC.

In the first control register (ADC10CTL0), we only need to change two parameters,

• ADC10SHTx--10bit ADC Sample Hold Time-- a higher value means each sample will be held for a longer period of time. We want to set this at the max value of ADC10SHT_3 .
• ADC10ON--10bit ADC ON/OFF--setting this bit to "1" (denoted by the label ADC10ON) turns on the ADC, a vital step to performing any conversion!
To actually do this in C, just use addition and an equals sign:



In the second control register (ADC10CTL1), we want to again set two parameters, but we will need to use 4 alias labels instead of just two.

• ADC10DIVx--10bit ADC clock Divider bit x-- for "more flexibility", you set each bit individually in the three bit ADC10DIVx section of the register. Since we want the maximum divider, we will set all the bits.
##### Aside:
Since some of the bit labeling is inconsistent (ADC10DIV is bit-wise while ADC10SHT is not), it is always good to examine the header file for a controller to see how its aliases are defined before using them in your code.
• INCHx--Input Channel #-- this 4 bit section determines which of the possible input channels the ADC will actually convert in single convert mode. In series mode, this determines the highest channel to be converted in the series (all channels below this number will also be converted).
ADC10CTL1 = ADC10DIV0 + ADC10DIV1 + ADC10DIV2 + INCH_X;


Lastly, the ADC10 has the ADC10AE0/1 registers that enable analog input on the different pins. These act as gates to prevent leakage current from flowing from a pin set as an output through the ADC to ground-- a substantial waste of power. To enable the ADC for your desired GPIO pin, just set the corresponding bit in  ADC10AE0  to "1".

ADC10AE0 |= BIT#;




Remember that we have setup the ADC for single conversion and hold, so if you want another value, you will have to tell it to sample and convert again. You do so by modifying two values in ADC10CTL0:
• ENC--Enable Conversion-- locks in the ADC settings and stabilizes it for conversion.


##### Note:
Be sure to use OR equal (|=) so that the configuration bits you set before don't get overridden.
##### Note:
Also, don't forget to configure P1 as usual. You will need to set the pins you wish to use as ADC inputs to input mode at the  P1DIR register as well as the ADC10AE0 register. You can configure the P1 registers using aliases and variable assignments just like with the ADC registers.

## Assignment Details

Using Code Composer Studio 4, write a C language program turning your MSP430 LaunchPad into a simple 10 level voltmeter. Your program should divide the 0-3.3V input range of the ADC into 10 zones, and then output from a 0 to a 9 on the LED display depending on the input voltage. Don't worry about a value landing on the boundary between two zones, just deal with it consistently. Test your volt meter by attaching it to some of the variable power supplies around the room. DO NOT EXCEED AN INPUT VOLTAGE OF 3.3V. You will damage your circuits and destroy your MSP430.

• A "void main(void)" function that drives your program
• A successful setup routine that properly configures the ADC10
• An output routine that successfully re-scales the 1024 ADC possibilities to 10 zones

## Content actions

PDF | EPUB (?)

### What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

#### Definition of a lens

##### Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

##### What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

##### Who can create a lens?

Any individual member, a community, or a respected organization.

##### What are tags?

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks

### Reuse / Edit:

Reuse or edit module (?)

#### Check out and edit

If you have permission to edit this content, using the "Reuse / Edit" action will allow you to check the content out into your Personal Workspace or a shared Workgroup and then make your edits.

#### Derive a copy

If you don't have permission to edit the content, you can still use "Reuse / Edit" to adapt the content by creating a derived copy of it and then editing and publishing the copy.