ECE 2534 Lecture 16 - Analog to Digital Conversion Basics 10:10 Analog vs Digital Analog World: - Continuous values, eg. any voltage between 0 and 5 volts - Continuous time, eg. signals are defined at any point in time Digital World: - Discrete values, eg. 32 bits means 2^32 different values - Discrete time, eg. a digital system works in discrete clock cycle steps Analog World -> Analog to Digital Converter -> Digital World Analog World <- Digital to Analog Converter <- Digital World The world is driven by digital algorithms, and there is a general trend to replace analog electronics by digital electronics whenever possible Why? Because analog circuits are noisy; they don't use threshold logic levels like digital circuits do. 10:13 What is Analog-to-Digital Conversion? We convert an analog value to a digital code (bit-vector) ADC inputs: Vref+ Highest expected value to convert Vref- Lowest expected value to convert Vin Actual value to convert ADC outputs: D Digital code, n bits The conversion formula for an ideal ADC is: Vin - Vref- D = floor{ ------------- . 2^n + 0.5 } Vref+ - Vref- The SPAN is the difference between highest and lowest input voltage = Vref+ - Vref- The RESOLUTION is the number of bits of the ADC = n The VOLTAGE RESOLUTION is the voltage step of one LSB = SPAN / (2^n) When the Vin is lower than Vref- or higher than Vref+, the ADC will saturate. That means, D gets stuck at (2^n -1) when Vin exceeds Vref+ D gets stuck at 0 when Vin is lower than Vref- The accuracy of the ADC is obviously determined by the number of bits. If n is larger, then the range Vref- to Vref+ will contain more steps. Handout - staircase curve - SLAA013.pdf Figure 1 and 2 Example: Vref+ = 5V -> code FF (255) Vref- = 0V -> code 0 (0) Vin = 0.8V n = 8 bit D = ? 1 LSB is 5V / 256 = 0.01953125 V 0.8V / 0.01953125V = 40.96 --> rounded to 41 Output code is 41 Example: Vref+ = 3V -> code 3FF (1023) Vref- = -3V -> code 0 (0) Vin = 0.8V n = 10 bit D = ? 1 LSB is 6V / 1024 = 0.005859375 (0.8V + 3V) / 0.005859375 = 648.53 --> rounded to 649 Output code is 649 10:20 What is Digital to Analog Conversion? Once we have a digital code, we can convert it back to an analog value. D Vout = --- . (Vref+ - Vref-) + Vref- 2^n Note that the output values are always discrete. How do we build a DAC? Using a linear circuit using operational amplifiers. Basic design for a 3-bit ADC b2 msb +----///R///-----+-----//R///---------+ | | b1 +----//2R///-----+ | \ | | | \ | b0 lsb +----//4R///-----+-----+ - \ | | +---------+---- Vout gnd -------+ + / | / | / Vout = - R . (Vcc.b0/4R + Vcc.b1/2R + Vcc.b2/R) Where b0 = 0 or 1, b1 = 0 or 1, b2 = 0 or 1 Vout = - Vcc . (b0/4 + b1/2 + b2) Vout = - Vcc . (b0 + b1.2 + b2.4) / 4 Vout = - Vcc . D / 2^2 with D the three-bit integer represented by the bits (b2 b1 b0) You can see that this can be easily extended to n bits. However, this requires resistors with high accuracy. The smallest resistor R, determines the weight of the MSB. 10:25 DAC's using R-2R ladder An alternate design is the so-called R-2R ladder Vin -------+----///2R///----+ gnd | +----///2R///----+ gnd What is the current through each resistor? I = Vin/2R What is the currrent through the complete circuit? I = Vin/R r1 r2 Vin-+-----///R//-----+----///2R//------+ gnd | | r3 | +----///2R//------+ gnd | r4 +----///2R//-----+ gnd What is the current through r4? I4 = Vin / 2R What is the current through r1? I1 = Vin / (R + R) = Vin / 2R What is the current through r2 (and r3)? I2 = I3 = Vin / 4R Hence, we can recursively add resistors of value R, 2R and create a circuit the splits the current in half each time. This is called an R-2R ladder, and it is usually drawn as follows I/2 I/4 Vin --+----///R//-----+----///R//-----+---------------+ | | | | // // // // 2R I/2 2R I/4 2R I/8 2R I/8 // // // // | | | | + + + + gnd gnd gnd gnd I = Vin / R The current is split in half every time, such that each 2R transistor carries half the current of the previous one (except for the last two). By adding a single op-amp, we can now sum up these currents for a DAC conversion 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:35 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. 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. 2. 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:40 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 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 10:44 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 - A/D, D/A conversion - R-2R ladder - Sample/Hold Circuit - Conversion by successive approximation - Conversion Time - Example using Joystick In next few lectures, we will cover: - More advanced conversion schemes (multi-channel) - Conversion errors