Analog to Digital Conversion The MSP in the name of our microcontroller MSP430G2554 is abbreviation for Mixed Signal Processor. This means that our microcontroller can be used to handle both analog and digital signals. The analog signal is interfaced with the microcontroller via simple comparators or more complicated analog to digital converters (ADCs). In this lab we are going to introduce to you the ADC module, its design parameters and how to implement analog to digital conversion. Overview Analog to digital converters, as the name implies, are used to convert continuous analog signals to digital values. The transfer function of the ADC is shown in the figure below and also in its equation form. The analog input with a range between Vr- and Vr+ Volts appears on the horizontal axis while the output appears on the vertical axis. The thin line represents the transfer function that would give us an analog output that is proportional to the analog input while the bold stepped line is the transfer function of the actual ADC which would give us a digital output signal. As you can see on the horizontal axis, the input signal is divided into a number of small divisions and each division is represented by a discrete number. The number of divisions is given by the number of bits of the ADC. For example in the diagram we assumed a 10 bit ADC which means the digital output is ranging from 0 to 2 10-1 = 1023. And the small horizontal division is therefore: (Vr+ - Vr-) /2 10 V. If the input analog signal drops below Vr- the output will be 0, similarly if the analog signal is above Vr+ then the output will be 1023.
... ADC Result NADC (Digital) 1023 1022 1 0 Vr- Vr+ Signal Input Vin (Analog) Fig 1. Ideal ADC transfer function NADC: Digital representation of sampled analog signal 1023: 2^(10)-1. This number defines the resolution of ADC VIN: Input signal voltage level VR- and VR+: Lower reference and Upper reference. These two values determine the range of input value that can be converted. How Successive Approximation ADC Works There are different workings of ADCs. The ADC we are using is called Successive Approximation (Register) ADC or SAR for short. A short hand block diagram is as shown below.
DAC = Digital-to-Analog converter EOC = end of conversion SAR = successive approximation register S/H = sample and hold circuit V in = input voltage V ref = reference voltage Fig2. SAR Block Diagram Briefly speaking, the ADC start with the MSB of SAR initialized to 1 and other bits cleared to 0. The DAC convert the SAR value to analog signal, therefore at this time the DAC output should be Vref/2. The input signal is sampled and hold and then compare with the DAC output through a comparator. If the DAC output exceed the sampled input voltage, the MSB will be reset; otherwise the MSB remains as 1. Then the second MSB is set and compared with the sample. The above procedure is repeated until all the bits are determined. After that, the result is saved in a specified register and the next sample is taken in. Mathematical expression for this procedure is: Let Vin = xvref, so x in [-1, 1] is the normalized input voltage. The objective is to approximately digitize x to an accuracy of 1/2 n. 1. Initial approximation x0 = 0. 2. ith approximation xi = xi-1 - s(xi-1 - x)/2 i. where, s(x) is the signum-function(sgn(x)) (+1 for x 0, -1 for x < 0). It follows using mathematical induction that xn - x 1/2 n.
Design Considerations To choose an ADC and its parameters for your design, here are some basic parameters you need to consider: 1. Input Range Input range is the range of analog input your ADC can handle. In most cases we would like to maximize the use of this range to give us finer representation of our analog input by manually setup our upper and lower reference. For example, if you know your signal is in range of 1.5 to 3V, using a reference of 0 to 3.5V will waste a lot of your ADC resolution. The references can be selected from either internal voltage references or from an external supply depending on the mixed signal microcontroller you are using. 2. Signal to Noise Ratio (SNR) Signal to noise ratio is a measure of the percentage actual signal with the noise imposed on the signal. When selecting your ADC, you do not just go for the highest resolution. For example if you have an analog signal which goes from 0 to 3V and a 10 bit ADC then a distinct digital output will be given when your analog signal differs by (3-0)/1024 = 3mV. Think about the case that you have noise super imposed on your signal that in average is 30mV. Then your output values differ by 10 may simply come from noise but not the signal you are converting. Necessary conditioning circuitry is needed to reduce noise level or improve your SNR. 3. Timing Constrains 4. Non ideal ADC There is a limit on how fast you can sample your analog input and convert to digital signal. In the case of SAR, this timing constraint is dependent on the ADC circuitry s time constant. We will give a detailed example in the next section when discussing the ADC module in G2553. Ideally ADC will behave as shown in Fig 1. Real world ADCs can deviates from this ideal behavior. For example, you may have an offset error that the best fit line of your transfer function missing the lower reference. These non ideal parameters can be found in the device specific data sheet. We are not going to cover this in detail, but we recommend reading the text book s section 9.2.1. as a supplement.
MSP430G2553 ADC Module ADC10 The ADC Module of MSP430G2553 is called ADC10. It is a 10 bit SAR ADC which supports 8 external input channels and 4 internal channels. There are 4 different internal voltage levels that can be used for ADC reference selects: Vss = 0V (ground), 1.5V, 2.5V and Vcc. You can supply external references as well. The block diagram, grouped by functionality can be seen in Fig 3. Here we provide a basic summary for setting up an ADC. Make sure ENC bit is 0 when you modify ADC setups. 1) Input Select (ADC10CTL1, ADC10AE0) a. Decide which channel or channels of input do you want b. Set the pin as analog input pin by setting INCHx bit in ADC10CTL1 and enable corresponding bits in ADC10AE0 register. c. ADC10AE0 register will configure the pin to be analog port and overwrites pin select (PxSEL) and pin direction (PxDIR) d. Select conversion mode via CONSEQx bit in ADC10CTL1. You can choose from: single-channel-single-conversion, sequence-of-channels, repeatsingle-channel and repeat-sequence-of channels. In this lab we will be using repeat-single-chanel. 2) Reference Setup (ADC10CTL0) a. Based on your signal, choose what upper and lower reference you want for your ADC. b. This is done by using SREFx bits in ADC10CTL0: c. If you select to use internal reference, make sure you turn the reference on by setting REFON bits. d. REFBURST can be set to turn off reference buffer when not in use e. You can also output your ADC references via REFOUT bit.
Reference Setup ADC Clock S/H Setup Input Select Output Handling
3) ADC Clock (ADC10CTL1) a. ADC Clock times the sample and hold and conversion of ADC. b. It can be selected from the internal oscillator (ADC10OSC, around 5MHz), ACLK, MCLK or SMCLK. c. Clock select is done via ADC10SSELx and ADC10DIVx bits in ADC10CTL1 register 4) Sample and Hold Interval (ADC10CTL0, ADC10CTL1) a. At what interval you want to sample and covert your signal (SAMPCON) b. Timing of sample and hold interval can be done via Timer outputs (TA0, TA1) or by software (ADC10SC). This gives you SHI (Sample and Hold Interval). c. SAMPCON is set when it sees a rising edge of SHI. d. SAMPCON needs to be on for long enough time for sampling. SAMPCON should be ON for 0.622 μs if you use a signal generator and 30.2 μs if you are using the temperature sensor. e. This is found via the following equation: For our microcontroller we use: In the case of signal generator, Rs = 50 Ω. After plugging in the value we find tsample > 0.422 μs Therefore, SAMPCON needs to be high for at least 0.622μs = 0.2μs + 0.422μs for sampling to take place properly. Here 0.2μs is the time period of the ADC10CLK which is connected to ADC10OSC in the experiments of this lab. ADC10OSC is running at 5 MHz, 1/(5 MHz) = 0.2μs.
f. SAMPCON time period is gotten by multiplying ADC10CLK period by the ADC10SHTx multiplier. g. Normally the sampling time is set by the SHI time period. However we can set the ADC to sample as soon as it it done with the previous sampe and conversion by setting the MSC bit. 5) Output Handling (ADC10MEM, ADC10CTL1, ADC10DTC0 and 1, ADC10SA) a. The result of conversion is stored in ADC10MEM b. The result of ADC can be stored in either straight binary format or 2s compliment format by setting ADC10DF bit in ADC10CTL1 c. (Optional) Set up the ADC10IE interrupt if needed. After all the setup has been put in place, remember to turn on the ADC (ADC10ON). To start conversion there has to be a raising edge on the SHI signal, this can be accomplished by toggling ENC from zero to one after the ADC has been turned enable conversion by setting. In single conversion mode (single-channel-single-conversion and repeated-single-channel-mode), ENC need to be toggled (in an ISR) every time if you want the next conversion. Experiment 1 Repeat Single Channel Mode, Individually Triggered In Experiment 1 we are going to apply analog to digital conversion on a 500Hz sine wave (originating from a signal generator) which varies between 0 to 3.5V, and store two periods of this sine wave into an array of 50 words. First select your input port. Let us use Pin 2 or GPIO P1.0 (ADC s A0 channel) select INCH_0 enable BIT0 of ADC10AE0 for analog port In order to accomplish this conversion we need to setup ADC reference Vr- = 0 and Vr+ = Vcc. use SREF_0 to select Vr+ = Vcc, Vr- = Vss Use internal oscillator for ADC clock.
use ADC10SSEL_0 to select ADC10OSC as clock source ADC10OSC is around 5MHz. tsample > 0.422μs if we use a signal generator with a source resistance of 50 ohm. By default ADC10SHTx is set to 00 which corresponds to sample and hold time of 4 times the ADC10CLK period = 4/5 μs = 0.8μs > 0.422μs. Therefore timing constraint for sampling is satisfied. Set conversion mode to be in Repeat Single Channel mode. use CONSEQ_2 to select repeated single channel mode The sample should be triggered on Timer A0 output. We want 25 samples per sine wave period. use SHS_2 to source SHI from TA0 Set Timer A0 clock to SMCLK at 1MHz in up mode. Set TA0 output in toggle mode which gives a period of TTA0OUT = 2 x TACCR0 x TTACLK at Tiner A0 output. TTA0OUT = 2 x TACCR0 x TTACLK > TADC10CLK + tsample + 13TADC10CLK = 0.20μs +.422μs + 13(0.20μs) = 3.2μs To have 25 samples per period, TA0 output period should be (our signal period)/25 = 80 μs2 x TACCR0 x TTACLK = 80 μs > 3.2μs, therefore timing constraint is satisfied. This gives: TACCR0 = 40 Set ADC10 interrupt: set ADC10IE Now check if all the settings and Enable ADC10 by setting ADC10ON Set ENC bit to enable conversion Enable Global Interrupts.
Inside the interrupt service routine read the sampled value of the signal from the register ADC10MEM and store the sample result in an array of length 50 samples. Increase your array index, when the array index reaches 50 set the array index to 0 and start saving in the array memory from the beginning again. Set a break point at the line where you reset the index to 0. Observe the digitized array stored in the memory when your program reaches this break point. Plot the array in CCS and include a screenshot in your lab report. Experiment 2 Temperature Sensor There is an on-chip temperature sensor in the G2553. The temperature sensor is connected to an input channel of the ADC by selecting INCH_10. select INCH_10 for ADC input When you select the temperature sensor channel, the on-chip reference for the temperature sensor automatically turns on and gives you a Vref+ = 1.5V. Use this Vref+ and Vref- = 0 as your ADC range. You do not need to setup Vref+ = 1.5V for temperature sensor but you need to enable the reference by setting the REFON bits. Based on the output resistance of the temperature sensor use tsample = 30μs. Modify your code in Experiment 1 to satisfy the temperature sensor constraints. Use 4 ADC10CLK with a clock divider (ADC10DIV) value of 8. Set Sample Timer divider (ADC10SHTx) to be divided by 64. Use the same settings for TimerA that you used in Experiment 1. Collect 50 sampled values of the temperature just as in Experiment 1. Show in your lab report that the above settings satisfy both of the timing constrains introduced in Experiment 1. Is the timing constrain satisfied when ADC10DIV is set to divided by 2? If everything is setup correctly, your sampled data should have a fluctuation of the order of 20 or less. Explain how you have setup your ADC to match the time constraints. Also include a screenshot of the plot of 50 of your temperature sensor sampled readings and show the highest and lowest values on your graph. The datasheet gives the following relationship between the temperatures in degree Celsius vs the sampled value of the temperature:
Export the sampled temperature data from the CCS debugger and use MATLAB to convert your data into Celsius degrees. Notice that the temperature sensor has an offset that may deviate from the equation shown above, so your result might not look reasonable. Short Answer Questions 1. What other conversion modes are supported in this microcontroller? Briefly describe each of them. 2. The temperature can now be easily converted into PWM. The PWM signal can, for example drive the motor of a fan. What PWM sampling period and PWM resolution would you use?