ECE 2534 Lecture 17 - Successive Approximation A/D 10:10 Analog to Digital Conversion Key ideas of last lecture Resolution n bit Span Vref+ - Vref- Volt Voltage Resolution Span / 2^n Conversion Formula from analog to digital Vin - Vref- D = floor{ ------------- . 2^n + 0.5 } Vref+ - Vref- Conversion Formula from digital to analog D Vout = --- . (Vref+ - Vref-) + Vref- 2^n Design of a DAC using an R-2R ladder I/2 I/4 Vin --+----///R//-----+----///R//-----+---------------+ | | | | // // // // 2R I/2 2R I/4 2R I/8 2R I/8 // // // // | | | | + + + + / \ / \ / \ gnd b2-switch b1-switch b0-switch + + + + + + gnd | gnd | gnd | | | | +---------------+---------------+ | +-----+-----//R///---------+ | | | | \ | | | \ | +-----+ - \ | | +---------+---- Vout gnd -------+ + / | / | / 10:15 Analog to Digital Conversion by Successive Approximation The TI ADC uses a conversion technique called "conversion by successive approximation". It uses an comparator and an ADC as follows. Vref+ Vin ->> S/H --+ | | +---+---+ +---+---+ | DAC | | | +-- D --+ +--------->-------+Compare+-+ | | | | | | | +-------+ +-------+ | | | +-----<<< approximation logic <<<-----------+ 1. SAMPLE AND HOLD PROCESS An input voltage is sampled and stored on a capacitor in a circuit called a SAMPLE-AND-HOLD (S/H). This is needed because the input to this loop needs to remain constant during the conversion. Capturing the voltage Vin and holding it on a capacitor takes some time, because it requires charging of the capacitor. This time is called the ACQUISITION TIME. Vin ---// R1 // -+---// R2 //----+ | | | | --+-- --+-- C1 --+-- C2 --+-- | | gnd gnd C1 = parasitic capacitance on package input R1 = external source resistance R2 = S/H digital circuitry series resistance C2 = S/H capacitor For an accurate n-bit conversion, we need to leave the S/H gate open for as long as the voltage on C2 to approximate Vin within 1 LSB. If we ignore C1 and R2 for a moment, and we say R1 = 10Kohm and C2 = 15 pF, what does the sample time need to be? Assume that the input voltage drops from Vref+ (full scale, 2^n - 1) to 0+ (just above zero, 1 lsb or 1). If we normalize for Vref+ we see the voltage drop from 1 to 1/(2^n) (approx.) So we can write exp (-tacq / (R1.C2)) < 1 / 2^n From which we can derive tacq > n . (R1.C2) . ln(2) For R1 = 10K, C2 = 15pF, n = 14bit, we find tacq > 1.45 microseconds Note that this already sets an upper limit to the conversion speed. If we need to sample a signal for at least 1.45 microseconds, then, no matter how fast we build the conversion, we cannot take samples faster than 686 KHz! 2. SUCCESSIVE APPROXIMATION PROCESS Now, the circuit will try different codes for D, comparing each time the analog version of D (at the output of the DAC) with the output of the S/H circuit. This is done from msb to lsb For example, assume a 3-bit conversion, and Vref+ = 5v, Vref- = 0v, Vin = 1.8v a/ Try code D = 100 DAC output = 5v . 4/8 = 2.5v comparator says 'lower' msbit must be 0 b/ Try code D = 010 DAC output = 5v . 2/8 = 1.25v comparator says 'higher' 2nd bit must be 1 c/ Try code D = 011 DAC output = 5v . 3/8 = 1.875v comparator says 'lower' lsbit must be 0 Conclusion: ADC value = 010 Error of conversion? DAC value 010 = 1.25v Actual Vin = 1.8v Error = 1.8 - 1.25v = 0.55v 1 LSB in this system is: 5v / 8 = 0.625V The error of the conversion is smaller than 1 LSB In a real ADC, these conversion will take some time. This time is called the CONVERSION TIME. Typically, every bit takes one clock cycle of a CONVERSION CLOCK (which is smaller than the system clock). Taking a sample and converting it to a digital value therefore takes ADC time = ACQUISITION TIME + CONVERSION TIME And the conversion frequency (the speed at which samples can be captured from a continuous signal) is 1 ADC frequency = ------------ ADC time 10:30 The ADC inside of MSP432P4 Relevant documentation: a/ USER GUIDE MSP432P4 (for operation) b/ Driver Lib (for programming) The main properties of the ADC are as follows: - 1 Msps max conversion rate at max resolution of 14 bits - conversion controlled by software, or by timers - software control of main parameters - Vref+ and Vref- sources - Conversion clock - Up to 32 input channels (the MSP432P4 package supports 23 input channels) - Conversion result output buffer of 32 registers Block diagram figure: USER GUIDE MSP432P4 Page 844 Simplified Block Diagram Conversion Clock INPUT 0 -|\ | +-------+ INPUT 1 -| \ V | MEM0 | INPUT 2 -| +---> S/H ---> SAR --->| ... | ... | / ^ ^ | MEM31 | INPUT 31 -|/ +---+----+ +-------+ | Analog Mux | Memory Buffer Trigger This ADC can support a wide variety of conversion mechanisms: - Single Channel Mode A single input is converted and written to a memory buffer The trigger is controlled by software, or automatically - Sequence of Channels Mode (autoscan) A sequence of channels is converted and written to a sequence of memory buffers The trigger is controlled by software, or automatically - Repeat Single Channel Mode A single channel is converted continuously and written to a memory buffer - Repeat Sequence of Channels Mode (repeat autoscan) A sequence of channels is converted continuously and written to a sequence of memory buffers 10:45 Connectivity of the BoostXL sensor board and main board to the ADC The ADC can be used with our kit to sample voltages from the following sources (in total, 8 different input sources) - Joystick - Accelerometer - Microphone - 2 unused channels on header How do we find out what channels they are connected to? There are two aspects to this problem. 1/ Connectivity of peripherals to MSP432P4 pins 2/ Connectivity of MSP432P4 pins to AD inputs Let's start with problem 2. 2a/ You can find out all the pins connected to the ADC analog mux by taking the data sheet of the MSP432P4. Specifically, look for Table 4-2, section ADC How many ADC inputs? 23 (so only 23 are connected to the Analog Mux. The other inputs are unused). You can also see, in table 4-2 what pin is used for a given ADC channel. For example, what pin is used for channel A18? Pin 51 2b/ How do we know what pin 51 is used for? We can consult Table 4-1, pin attributes. In this case, pin 51 is also used for P8.7, or port 8 bit 7. 2c/ Yet another check is to look at the pinout diagram of the chip, which shows the label P8.7/A18 for this pin. You MUST do this evaluation in order to be able to configure the GPIO pins correctly. In order to use pin 51 in the 'A18' mode (analog input), the following driverlib call must be made upon initialization GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P8, GPIO_PIN7, GPIO_PRIMARY_MODULE_FUNCTION); Let's now move to problem 1. We will consider one specific peripheral: the Joystick x-direction. First, consider the BoostXL documentation, and look for the Joystick. It has a table: BoosterPack Plug-in Module Header Connection Pin Function J1.2 Horizontal X-axis J1.5 Select button J3.26 Vertical Y-axis So this says that the horizontal X-axis is connected to heard 1 pin 2. To find out what analog channel this signal is connected to, we need to trace connected J1 pin 2 on the main board. Page 16 of the main board user guide shows a diagram of the connectivity. It says that J1.2 is connected to A15/P6.0 That means it's connected to a microcontroller pin that can either serve as GPIO port 6 bit 0 or else as analog input channel 15. So the complete connectivity is as follows: VCC | | // R <----- J1.2 (BoostXL) --- J1.2 (Main Board) -- A15/P6.0 // | | GND You can do this for other analog input channels as well. 10:55 (If Time Left, otherwise we'll discuss on Friday) An Example Program The Joystick on the BoostXL board is made with potentiometers VCC -----/\/\/\/\/\------GND ^ | | X or Y We will write a small program that reads the voltage of the potentiometer and converts it a digital value. For a 14-bit ADC, pushing left -> code 0, pushing right -> code 3FFF, in the middle -> code 1FFF First, we set up the ADC: void initADC() { ADC14_enableModule(); // This sets the conversion clock to 3MHz ADC14_initModule(ADC_CLOCKSOURCE_ADCOSC, ADC_PREDIVIDER_1, ADC_DIVIDER_1, 0 ); // This configures the ADC to store output results // in only one single output register, ADC_MEM0. // This is useful when we convert only one single input channel ADC14_configureSingleSampleMode(ADC_MEM0, true); // This configures the ADC in manual conversion mode // Software will start each conversion. ADC14_enableSampleTimer(ADC_MANUAL_ITERATION); } Next, we set up the Joystick void initJoyStickX() { // This configures ADC_MEM0 to store the result from // input channel A15 (Joystick X), in non-differential input mode // (non-differential means: only a single input pin) // The reference for Vref- and Vref+ are VSS and VCC respectively ADC14_configureConversionMemory(ADC_MEM0, ADC_VREFPOS_AVCC_VREFNEG_VSS, ADC_INPUT_A15, // joystick X ADC_NONDIFFERENTIAL_INPUTS); // This selects the GPIO as analog input // A15 is multiplexed on GPIO port P6 pin PIN0 GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P6, GPIO_PIN0, GPIO_PRIMARY_MODULE_FUNCTION); } Finally, we can do conversions as follows unsigned getSampleJoyStickX() { // This starts the conversion process // The S/H will be followed by SAR conversion ADC14_enableConversion(); ADC14_toggleConversionTrigger(); // We wait for the ADC to complete while (ADC14_isBusy()) ; // and we read the output result from buffer ADC_MEM0 return ADC14_getResult(ADC_MEM0); } 10:55 Conclusions - Successive Approximation ADC Sample/Hold Successive Approximation - Implementation in MSP432P4 (channels, memory buffers) - Connectivity on BoostXL/Main board