A AND THE SAMPLING THEOREM

LAB 3 A/D, D/A AND THE SAMPLING THEOREM 1. LAB OBJECTIVES  Reading in voltages  Writing out voltages  Setting the sampling frequency  Aliasing  U...
Author: Emory Hancock
12 downloads 3 Views 505KB Size
LAB 3 A/D, D/A AND THE SAMPLING THEOREM 1. LAB OBJECTIVES  Reading in voltages  Writing out voltages  Setting the sampling frequency  Aliasing  Use of Fourier transform to extract frequency information from sampled data 2. BACKGROUND 2.1. Sensoray Multifunction I/O Board For the remainder of the labs we will be using the Sensoray Model 626 PCI Multifunction I/O board for converting external voltages to digital numbers (A/D), converting digital numbers to external voltages (D/A), counting encoder pulses, and reading and writing digital signals. The 626 board contains the following:  16 differential analog inputs  4 analog outputs  6 24bit up/down counters that we will use for counting encoder pulses  48 digital I/O channels  a watchdog timer

2.2. Analog Inputs: Analog to Digital Converters (ADC, A/D) There are 16 analog input channels on the Sensoray card. These channels can read in voltages in ranges of 5V or 10V. The desired range is set through software commands sent to the card. The ADC has 16bit resolution. The maximum number that could be represented with a 16-bit word is = 65535 (216-1). Since the 15th bit is reserved for sign representation, we can represent numbers from –32768 to +32767. The following illustrates how voltages are represented by integers. A digital value of: 32767 = 0x7FFF represents 10 volts. A digital value of: -32768 = 0x8001 represents –10 volts. For any other digital reading, the equivalent voltage is V = 10.0*(digital reading)/32767 when the voltage range is +/-10V.

For any other digital reading, the equivalent voltage is V = 5.0*(digital reading)/32767 when the voltage range is +/- 5V. For example, Digital value

Digital value in hex

Analog output voltage

20000

0x4E20

6.10 V

-10000

0xD8F0

-3.06 V

It is important to use the appropriate data type here. Consider the following program. #include main() { short int hex1 = 0xD8F0; int hex2 = 0xD8F0; printf(“\n Short integer 0xD8F0 in decimal is:%d”,hex1); printf(“\n Integer 0xD8F0 in decimal is:%d”,hex2); }

The output is: Short integer 0xD8F0 in decimal is: -10000 Integer 0xD8F0 in decimal is: 55536

The second output is not –10,000 as you would expect. Instead we get 13*16^3 + 8*16^2 + 15*16 + 0 = 55536. You must realize that the data type short int represents a 16 bit integer where the 15th bit is the sign bit. A zero at the 15th bit indicates a positive number and a “1” at the 15th bit indicates a negative number. In the above case, 0xD8F0 had a “1” at the 15th bit, and is therefore a negative number. However, int is a 32-bit integer where the 31st bit is the sign bit. Thus, an int variable stores 0xD8F0 as 0x0000D8F0. This number has a zero in the sign bit and is not recognized as a negative number. So the upshot of this is that we need to read in voltage data from the ADC into a short int variable, so that the presence of a sign bit at the 15th bit is properly recognized by the compiler. 2.3 Details on adc.c The program adc.c can be obtained from the course website. This program reads in an analog voltage

and prints the value to the screen. Go through the program carefully and understand the commands needed for using the Sensoray card to read in voltages. This program will be the basic framework that you will be using for several more labs. When you are developing applications, you can open this file and modify it appropriately. The rest of this section describes the individual statements in adc.c in detail. HBD hbd = 0;

is a 32-bit value used as a “handle” to the board. It is possible to install several 626 boards in a single system. Therefore, all functions need a handle to know which particular board is being accessed. When there are several boards placed in a computer, for example a data acquisition board, an image processing board etc., we need to know which board’s functionality we want to use. In our case, we have only one PCI card installed in our computer, so hbd is always set to “zero.” HBD

SetPriorityClass(GetCurrentProcess(),REALTIME_PRIORITY_CLASS);

This sets the program to run at a high priority under Windows XP to ensure real-time behavior S626_DLLOpen();

This is a function call that prepares the board for use. S626_OpenBoard(hbd, 0, 0, 3);

The S626_OpenBoard function performs system initialization. It identifies the supported boards present in the system and initializes internal data structures. The 626 board has 16 A/D channels. The poll_list entry specifies the order in which the channels are read. A poll_list entry is a byte, where  The lower 4 bits (0 through 3) specify a channel number (0x00 through 0x0f) BYTE poll_list[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f | EOPL};

 Bit 4 specifies the ADC gain. Zero corresponds to a +/-10V range, 1 corresponds to +/-5V range. ±

In our example, we have set the voltage range of all channels to 10V. Changing the poll_list to {0x10, 0x11, 0x12, 0x13, 0x14, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f | EOPL} changes the voltage range of the channels AD0 to AD4 to ±5V. Channels AD5 to AD15 continue to be in the 10V range. The defined constant EOPL stands for “end of poll list.” This tells the ADC how many channels to convert when S626_ReadADC is called. For this course we will only be using channel 0 (AD0), so we will set poll_list to be BYTE poll_list[16] = {0x00 | EOPL, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};

The resolution of the ADC is dependent on the voltage range. For example, Voltage range = +/-10V - > Resolution = 10V/32767 = 0.305 mV. The smallest change in voltage that you could detect with the ADC board set for a +/- 10V ranges is 0.305mV. For a voltage range of +/-5V, the resolution is 5V/32767 = 0.153 mV. The smaller the voltage range, the better the resolution. For applications where the change in voltage signal is very small, for example voltage signals from strain gauges, we want to have a high resolution. Depending on the type of application, one can choose the appropriate voltage range. short data[16];

An array of size 16. The voltages read from the ADC channels will be stored in this variable. The array variable data[0] contains the voltage read from AD0 channel, data[1] contains voltage read from AD1 channel and so on. S626_ResetADC(hbd, &poll_list[0]);

This command initializes the ADC for data input. S626_ReadADC(hbd,data);

This performs A/D conversion and stores the voltage values from each of the channel in the array data.

S626_CloseBoard(hbd);

This function terminates all internal processes related to the current poll_list. S626_DLLClose();

This function must be called before terminating the application, i.e., at the end of any program in which S626_DLLOpen() was previously called. This function frees allocated system resources. Make sure you understand the adc.c program available on the course website. You will be using this program during the lab to read in voltages. 2.4. Analog Output: Digital to Analog Converters (DAC, D/A) The Sensoray 626 card has four D/A channels. These channels have a 14-bit resolution (including the sign bit). As we have seen earlier, the range of numbers that can be represented with 14 bits is: +/- 8191. This corresponds to an analog voltage between +/-10V. For example, a digital value of +8191 represents +10V. The statements dacdata = 8191; S626_WriteDAC(hbd,0,dacdata);

will produce a voltage of +10V at the DAC0 channel (pin 42: +DAC0, pin 40: ground). If you connect a voltage meter between pins 42 and 40, it should read a value of +10V (or close to it). Similarly – 8191 represents –10V. For other dacdata, the output voltage equals 10.0*(dacdata)/8191. The statement S626_WriteDAC(hbd, channel, dacdata) writes the value of dacdata to the specified channel number (channel 0 for this example). For example: Dacdata

Analog output

2000

+2.44 volts

-1000

-1.22 volts

Question: What happens if we try to write out a number greater than +8191. In other words, what is the output of the command: S626_WriteDAC(hbd,0,9000)? Answer: Beyond +8191 and –8191, the voltage values repeat. For example, a digital value of 9000 gives a voltage of [(9000-8191)/8191]*10 V. So it is important to make sure that only values between +8191 and –8191 are sent to the DAC. Note: There is a bug in the Sensoray software. It is necessary to call S626_WriteDAC twice to ensure that the desired voltage is properly output. Often only a single call to S626WriteDAC is necessary, but on occasion, and always immediately after calling S626Init, two calls are required for the DAC output to be valid. Because of this bug, we have made it a practice to always call S626WriteDAC twice. 2.5. Sampling Signals All physical data are continuous with respect to time. However, to collect data by a computer we collect data at discrete time intervals. This is called sampling. 2.5.1. Sampling frequency (F s)

The rate at which we collect data is called the sampling frequency. In the below figure, we have four sampling points per cycle. The sampling frequency is four times the signal frequency. For the plot this means a sampling frequency of 4Hz and a signal frequency of 1Hz.

The frequency with which we decide to sample a signal, called the sampling frequency, is one of the most critical parameters we must determine. The higher the sampling frequency, the better we can reproduce the signal we are trying to measure, but more data must be collected and more expensive hardware may be needed. Sampling frequency Fs is often limited by the A/D hardware used. In our case, A/D conversion takes approximately 20ms per channel. Therefore, the maximum Fs that we can achieve is 1 Fs = ------------------- = 50 ,000Hz = 50kHz 20 10 6– ms ´ If Fs is chosen too small, we may not be able to properly reproduce the original signal. This phenomenon is called ALIASING. You may have observed aliasing on television, for example, when the wheels of a fast moving car appear to be rotating slowly in the opposite direction. The phenomenon of aliasing has been demonstrated through graphs shown in the lecture. To avoid aliasing, we need to understand the Nyquist criterion: A signal must be sampled at a frequency at more than twice the maximum frequency present in the signal. In other words, Fs > 2 fmax, where fmax is the maximum frequency present in the signal. For a given Fs, the observed frequency varies with actual frequency as shown in the figure below.

2.5.2. Setting the sampling frequency Refer to sine_io.c on the website. This program reads in voltages at 1000 Hz. This sampling frequency is

achieved by asking the computer to sleep for 1ms (0.001s) (The sampling time Ts is the reciprocal of Fs, Ts=1/Fs). This is achieved by using the sleep_for() subroutine. Include the file sine_io.c in your project. Usage: To sleep for one millisecond, the following functions are used. QueryPerformancecounter(&hr0); sleep_for(hr0, one_msec);

The variable one_msec is predefined. For now, simply use the function QueryPerformanceCounter with the suggested syntax. For other sampling frequency values: Fs = 2kHz - > Ts = 0.5ms - > sleep_time = one_msec/2; Fs = 500Hz - > Ts = 2ms - > sleep_time = 2 * one_msec; For example, the following piece of code reads in voltages at a sampling rate of 2kHz: sleep_time = one_msec/2; while(!kbhit()) { S626_ReadADC(hbd,data); sleep_for(hr0, sleep_time); QueryPerformanceCounter(&hr0); }

Note: The order in which sleep_for and QueryPerformanceCounter are called is important in this loop! 3. PRELAB 1. How will the 16 bit A/D represent the following voltages when it is in +/- 10V range and when it is in +/- 5V range? What will the reading be when the input voltage is out of range? For example, what is the reading corresponding to –9V and 6V when the ADC is ± set to 5V range? Voltage -9V -1.5V 2.5V 6V

A/D representation (in decimal) (when in +/- 10V range)

A/D representation (in decimal) (when in +/- 5V range)

2. What voltage will appear at the analog output port after the following constants are sent to the D/A (D/A has a 14 bit resolution including the sign bit)? Constant sent to D/A

D/A Voltage (V)

-6000 +320 +7000 3. Study adc.c on the course website

4. Study dac.c on the course website. Note: the resolution of the ADC and the DAC is different. 5. Study sine_io.c on the course website. Learn how to change the sampling frequency. Make sure you understand all aspects of this program since this forms the basic building blocks for many of the rest of the programs we will write for the remainder of the course. 6. (Question 12, p.91 in Bolton, “Mechatronics”) What is the resolution of an analogue-to-digital converter with a word length of 12 bits and an analogure signal input range of 100V? Show work. 7. (Question 13, p.91 in Bolton, “Mechatronics”) A sensor gives a maximum analogue output of 5V. What word length is required for an analog-todigital converter if there is to be a resolution of 10mv? Show work. 8. (Question 16, p.91 in Bolton, “Mechatronics”) What is the conversion time for a 12-bit ADC with a clock frequency of 1MHz? Show work. Prelab 3 must be completed before coming to lab (the TAs will check this). It will be collected at the end of the lab and graded. 4. LAB PROCEDURE Create a directory lab3 under your home directory. Under lab3, use the Microsoft Visual C++ Developers Environment to create the project adc. Create a C file in this project called adc.c and get

the contents of this file from the course website (File>New->C++ Source File). You can use the cursor to cut and paste text from the website into adc.c. Compile this program and do Exercise 1. Close the adc project and create a new project called dac, following the exact same steps as for adc, only use the program dac.c from the website. Compile dac.c and do Exercise 2. Close the dac project open the sine_io project, just as you did above.

4.1. Exercise 1 This exercise demonstrates the A/D converter. You will read in the voltages given in prelab Question 1. Step 1: Make sure the digital multimeter and the frequency generator are correctly connected to channel ADC0 (pin no.4: +AD0, pin no. 3: -AD0). Step 2: Set the power supply to –9V DC. Execute adc.exe. This prints out the digital value corresponding to –9V on the screen. Write down the value. Step 3: Change the power supply to –1.5V. Hit the space bar to read in the new voltage. Write down the corresponding value. Step 4: Repeat the above steps for +2.5V and +6.0V. Step 5: Modify the steps 2 to 4.

poll_list

in the program so that the voltage range is 5V for ADC0. Repeat

4.2. Exercise 2 This exercise demonstrates the DAC. Step 1: Make sure the multimeter is connected to the channel DAC0. (pin no.42: DAC0, pin no.40: ground). Step 2: Execute dac.exe and enter the DAC mode. Step 3: Enter a value of –6000. Write down the multimeter voltage reading. Step 4: Repeat the above procedure for the other values specified in the prelab question-2. 4.4.3.

4.3 Exercise 3 This exercise demonstrates the aliasing phenomenon using sine_io.c. Step 1: With the help of the TA, connect the function generator, multimeter and channel 1 of the oscilloscope to analog input channel 0 (ADC0). Connect channel 2 of the oscilloscope to the DAC0 channel. Step 2: Set the function generator to produce a sinewave with an amplitude of 1 volt rms. (note: rms is “root mean square” voltage. 1 rms = 2.828 volts peak to peak voltage). Set the frequency generator to output a 50Hz signal. Step 3: Execute sine_io.exe. Step 4: Estimate the frequency of the output signal using the oscilloscope. Set the function generator to each frequency in the table given below and write down the estimated output frequency. Input frequency (Hz) 50 250 500 1000 1500

Observed output frequency (Hz)

5. POSTLAB AND LAB REPORT Answer the following questions: 1. Consider an 8-bit (including sign bit) A/D converter with a voltage range of +/-1V. What is the resolution of the A/D converter? How is 0.3V represented in this A/D? 2. Consider the analog signal: xa(t) = 3cos(100pt). Determine the minimum sampling rate to avoid aliasing. If this signal is sampled at 75Hz, what is the frequency of the signal reconstructed from the sampled data points. Lab report requirements: Answers to all questions. Be sure to attach the actual vs. observed frequency table that you created in Exercise 4.3.

Suggest Documents