Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Interfacing with C plus plus-programing communication with microcontrolers (K. Bentley, 2006)

.pdf
Скачиваний:
192
Добавлен:
12.08.2013
Размер:
3.18 Mб
Скачать

32610 VOLTAGE AND TEMPERATURE MEASUREMENT

9.1Ask the user to enter the lower temperature and store value entered.

9.2Store the cycle time.

9.3Set the flag confirming the lower calibration temperature has been read.

The program that implements these steps is given in Listing 10-12.

Listing 10-12 Temperature measurement using thermistor & VCO – temp.cpp.

/***************************************************** This program uses the thermistor on the interface board to generate a voltage for input to the VCO, and then

repeatedly reads the cycle time of the VCO’s output pulsetrain. It also allows you to calibrate the thermistor so

the program can display the actual temperature.

*****************************************************/

#include <iostream.h> #include <bios.h> #include <conio.h> #include "vco.h"

void main()

{

VCO Vco;

int Quit=0, HiFlag = 0, LoFlag = 0; int key = 0;

float HiTemp, LoTemp, Temp; long int HiCount, LoCount;

clrscr();

while(!Quit)

{

Vco.MeasurePeriod();

clrscr();

gotoxy(10,10);

if((HiFlag == 1) && (LoFlag == 1))

{

Temp = LoTemp+(HiTemp-LoTemp)* (Vco.GetPeriod()-LoCount)/(HiCount-LoCount);

cprintf("The temperature is:%7.1 lf (deg)\a",Temp);

}

else

cprintf("The pulse period is: %10lu\a",

10 VOLTAGE AND TEMPERATURE MEASUREMENT 327

Vco.GetPeriod()/1000);

if(bioskey(1)!=0)

{

key = bioskey(0); switch(key)

{

/* Alt-X */ case 0x2d00 : Quit = 1; break;

/* Up Arrow */ case 0x4800 : gotoxy(10,5);

cout << "Enter Upper Calibration Temp: ";

cin >> HiTemp;

HiCount = Vco.GetPeriod(); HiFlag = 1;

break;

/* Down Arrow */ case 0x5000 : gotoxy(10,6);

cout << "Enter Lower

Calibration Temp: "; cin >> LoTemp;

LoCount = Vco.GetPeriod(); LoFlag = 1;

}

}

}

}

Executable File Generation

 

Required Files

 

Listing No.

 

Project File Contents

 

 

pport.cpp

 

 

 

 

 

 

 

Listing 10-8

 

pport.cpp

 

 

 

 

 

 

pport.h

Listing 10-7

 

vco.cpp

 

 

vco.cpp

Listing 10-4

 

 

vco.h

Listing 10-1

 

 

 

temp.cpp

Listing 10-12

 

temp.cpp

 

The variable HiFlag is used to denote the upper calibration temperature has been entered, and similarly the variable LoFlag to denote the lower calibration temperature has been entered. Although HiFlag and LoFlag they are declared as integer variables, they will only be used with values of 0 or 1. The variables HighTemp and LowTemp will store the upper temperature and the lower temperature entered during calibration. The value of the pulse period (measured in

328 10 VOLTAGE AND TEMPERATURE MEASUREMENT

counts) will be stored in the variable HiCount for the upper calibration temperature, and in the variable LowCount for the lower temperature. The actual temperature to be displayed is stored in the variable Temp.

The first if statement within main() tests whether both temperatures have been entered by checking the values of the flags HiFlag and LoFlag. If both flags are set, the temperature will be calculated using the calibration equation and printed on-screen.

Be aware of the importance of correctly specifying mathematical operations when calculating the value Temp in the program’s formula:

Temp = LoTemp + (HiTemp-LoTemp)*

(Vco.GetPeriod()-LoCount)/(HiCount-LoCount);

Note that Vco.GetPeriod(), HiCount and LoCount are long integer type, whereas Temp, LoTemp and HiTemp are float type. If we had placed a set of brackets around the expression shown on the lower line, the compiler would cast this part result to become a long integer number (incorrect – it should be a floating point number). Likewise, rearranging the order of mathematical operations can cause the compiler to implicitly cast part-expressions and change the result of an expression.

If both temperatures have not been entered yet, the calibration equation will not be used and the period is printed on the screen instead. The switch statement block is used to detect key presses for the up and down arrow keys, and the Alt-X key combination. If you press the up arrow key, you will be prompted to enter the upper temperature which will be stored in the variable HiTemp. The current value of Vco.GetPeriod() (returns the data member Period) will be stored in variable HiCount. The flag HiFlag will then be set to one. The equivalent procedure will be followed when the down arrow key is pressed to enter the lower calibration temperature.

Note: the thermistor requires time to reach the temperature of the body it is placed into contact with. Therefore, sufficient time must be allowed before pressing the up/down arrows to enter each calibration temperature. The program can be verified after it has been calibrated. Subject the thermistor to known temperatures and the program should display values close to those temperatures.

10.8 Summary

In this chapter we have described the operating principle of the Voltage-controlled Oscillator (VCO). The VCO produces a pulse-train having a frequency that is proportional to the voltage applied to its input. By measuring the frequency (or period as we did) the voltage/frequency relationship can be used to generate a measurement of voltage. In this way the VCO can be used as a simple and inexpensive alternative to an analog-to-digital converter.

10 VOLTAGE AND TEMPERATURE MEASUREMENT 329

A new object class named VCO was developed using the ParallelPort as the base class. Software methods have been described to continuously check the level of an incoming digital signal while incrementing a counter, and thereby measure the period of the waveform. Graphics programming was introduced to display the resulting waveform, followed by the development of a program that uses the thermistor on the interface board with the VCO to measure the actual temperature.

10.9 Bibliography

Bentley, J., Principles of Measurement Systems, Second edition, Longman Scientific & Technical, Essex, 1988.

Horowitz, P. and Hill, W., The Art of Electronics, Cambridge University Press, Cambridge, 1989.

NS CMOS, CMOS Logic Databook, National Semiconductor Corporation, 1988. Webb, R.E., Electronics for Scientists, Ellis Horwood, New York, 1990. Wobschall, D., Circuit Design for Electronic Instrumentation, McGraw-Hill, 1987.

Lafore, R. Object Oriented Programming in MICROSOFT C++, Waite Group Press, 1992.

Wang, P.S., C++ with Object Oriented Programming, PWS Publishing, 1994. Winston, P.H., On to C++, Addison Wesley, 1994.

11

Analog-to-Digital

Conversion

Inside this Chapter

ξ

ξ

ξ

ξ

ξ

Analog-to-Digital Conversion (ADC) explained.

Types of ADCs.

Sampling Signals.

An Object Class for the ADC.

Voltage and Temperature Measurement using the ADC.

11.1 Introduction

This chapter explains the principles of analog-to-digital conversion and the operation of several commonly used types of analog-to-digital converters. This is followed by a discussion of the limitations encountered when sampling and converting signals.

Transducers measure physical quantities such as temperature, pressure, flow rate, and distance. Analog transducers typically output current, voltage, or charge, which form some mathematical relationship with the measured physical quantity. This mathematical relationship can be obtained using the calibration process we described in the previous chapter. An analog-to-digital converter (ADC) is typically used to interface these analog signals to a digital computer. Signal conditioning circuitry transforms the analog currents or charge into voltages that are sampled by the ADC system and converted to digital bit patterns.

Software is used to control the ADC on the interface board and read its output. This is made possible by deriving an object from the ParallelPort class and then encapsulating the functionality of the ADC. This new object will be used in our programs to measure analog voltages.

11.2 Analog-to-Digital Conversion

Analog-to-digital conversion is the process of sampling and then converting an analog signal, usually a voltage, to a multi-bit digital number that is proportional to the amplitude of the analog signal. Analog-to-digital conversion is used in many applications ranging from encoding of voice-generated signals in telecommunication systems, to data acquisition and control systems. Figure 11-1 shows the block diagram for a typical (8-bit) ADC. Conversion is initiated by activating the ‘Start Conversion’ input of the converter. At completion of the conversion process the ‘Conversion Complete’ output of the converter will change logic state. This signal is used to notify the controlling device that data conversion is complete, and valid data can now be read.

 

8-Bit ADC

Analog

Voltage

Outputs

Voltage

Input

Digital

 

 

Commence

Start

Conversion

Conversion

Conversion

Complete

 

 

 

8-bit value (8 logic signals)

End of

Conversion

Figure 11-1 Block diagram of an 8-bit ADC.

11 ANALOG-TO-DIGITAL CONVERSION 333

The time that elapses from the start of conversion to the valid output of the digital code is referred to as the conversion time. The Conversion Complete output of the ADC can be ignored if the device requesting the converted data delays its reading of the data by a longer period than the conversion time.

An analog voltage signal has an infinite number of possible voltage levels within its range. The analog voltages are converted to digitally coded numbers by sampling and converting the analog signal into a fixed number of possible digital states or levels. This process is known as quantisation. For example, a 3-bit ADC can digitise an analog voltage and create digital numbers from zero to seven, which represents the analog voltage over a set range (say 0 to 3.5V) as shown in Figure 11-2 and Table 11-1. In this example the analog signal has been divided up or quantised into eight levels.

Digital

Code

111

110

101

100

011

010

001

000

0 0.5 1.0 1.5 2.0 2.5 3.0 3.5

Analog Input

 

Voltage

Figure 11-2 Ideal ADC Conversion.

Table 11-1 Quantisation of analog voltages to 3-bit code.

Quantised Analog

3-bit ADC

 

Input Voltage

Digital Code

Decimal

0.0 V

000

(0)

0.5 V

001

(1)

1.0 V

010

(2)

1.5 V

011

(3)

2.0 V

100

(4)

2.5 V

101

(5)

3.0 V

110

(6)

3.5 V

111

(7)

 

 

 

334 11 ANALOG-TO-DIGITAL CONVERSION

The digital code produced by the ADC will be correct and not contain any error when the analog input voltage corresponds exactly with a quantised voltage level. However, the ADC will incur an error known as quantisation error, when the input voltage is not exactly equal to the nearest quantised voltage level. For example, referring to Figure 11-2; zero quantisation error occurs when the analog input is equal to 0V, 0.5V, 1.0V, etc. The quantisation error is a maximum (equal to ½ a quantisation level) when the analog input voltage lies halfway between two quantisation levels – 0.25V for the previous example.

Another type of error can occur with analog-to-digital conversion, known as monotonic error. If the input voltage to the converter increases in discrete quantised levels, the digital output code should also increase by the same number of increments. If this does not happen, then monotonic error has occurred, reducing the useful resolution of the converter.

The digital output code from an ADC is produced in either serial or parallel format. The converter shown in Figure 11-1 uses parallel format with all 8 output bits available. Parallel output converters usually have faster operating times than the serial output types but require additional connections to the digital system. However, ADCs with serial output require more work to control than parallel output types. Some converters contain an internal analog multiplexer, allowing multiple analog input channels to be processed (at a proportionately slower speed).

At the start of the conversion process the input voltage must be sensed by the converter’s input stage circuitry. The output impedance of the external circuit that is providing the input signal to the ADC must be sufficiently low compared to the ADC’s input impedance for the ADC to function properly. The converter will operate over a limited range of input voltage, and this too must be considered when scaling the source of input voltage before connecting it to the ADC.

11.3 Conversion Techniques

Several popular analog-to-digital conversion techniques are implemented with electronic circuitry including voltage-to-frequency converters, single slope ADC, dual slope ADC, successive approximation ADC, and flash ADC. Some converters use a combination of methods to take advantage of the independent benefits of each approach. For example, the high-speed flash technique is combined with successive approximation to produce a ‘low cost’ but very fast converter. The voltage-to-frequency technique has been mentioned previously (Chapter 10) and is not normally considered for use, due to its relatively slow speed. The other converter techniques are widely used and are explained as follows.

Single Slope ADC

This converter uses a constant current source charging a capacitor, a voltage comparator, and a counter with clock source and control logic as shown in Figure 11-3.

11 ANALOG-TO-DIGITAL CONVERSION 335

VIN

 

 

Constant

VCAP

Voltage

Comparator

Current

I

 

Source

 

C

 

 

 

 

Counter, Clock

Conversion

 

Source and

Start

Complete

Control Logic

Conversion

 

 

 

Digital Output

Code

Figure 11-3 Simplified single slope ADC.

The conversion process begins immediately after the ‘Start Conversion’ input is driven to its active state and proceeds in two stages as follows:

Stage 1 - Initialisation

The discharging switch across capacitor C closes (activated by the control logic), discharging the capacitor to zero volts. Next the counter is reset to a value of zero, the counter logic opens the switch, and counting commences.

Stage 2 - Integration

The constant current source drives current into capacitor C, generating a ramping voltage at the comparator +ve input (this process is known as integration). When this ramping voltage exceeds the positive input voltage (VIN) present on the comparator –ve input, the comparator will toggle state from low to high. This change in state of the comparator will signal the counter logic to cease counting, at which time conversion is complete. The ‘Conversion Complete’ output pin will then be switched to its active state to indicate end of conversion to external devices. The counter output code will now represent the analog input voltage. A larger magnitude of input voltage will require a longer time period for the ramping voltage to reach its level, producing a larger digital output value.

The cycle described as Stage 1 and 2 will repeat when the next ‘Start Conversion’ pulse arrives.

The conversion speed of the single slope ADC is relatively slow although its accuracy is reasonably good, being affected by the long-term stability of the counter’s clock, the stability of the constant current source, and the quality of the

336 11 ANALOG-TO-DIGITAL CONVERSION

capacitor, ideally having low dielectric absorption. When capacitors with high dielectric absorption are discharged and then removed from the discharging circuit, some charge will remain stored inside the capacitor on polarised dielectric interfaces. This charge generates an unwanted error voltage. Capacitors with very low dielectric absorption will have negligible voltage across them after being discharged. A further advantage of the single slope converter is that noise on the input voltage signal is averaged out during the process of integration.

Dual Slope ADC

This converter is similar to the single slope ADC except that two ramping stages are employed during conversion to greatly improve accuracy. Figure 11-4 shows the block diagram for such a converter.

VIN

 

SW1

Voltage -

 

 

VCAP

 

 

1

 

Controlled

 

 

 

 

 

 

 

 

Voltage

Reference

2

 

Current

I

 

 

Voltage

 

 

Source

 

C

SW2

Comparator

(VREF, -ve)

 

 

 

 

 

 

 

 

Counter, Clock

 

Start

Source and

Conversion

Conversion

Control Logic

Complete

Digital Output

Code

Figure 11-4 Simplified dual slope ADC.

Once the ‘Start Conversion’ input is asserted, the conversion process will proceed in three stages as follows:

Stage 1 – Initialisation

The ADC is ‘zeroed’ by closing SW2 to fully discharge the integrating capacitor C to zero volts.

Stage 2 – Integrate Up using ‘VIN

At the start of this stage the counter is reset to a count of zero, and SW1 is set to position 1, connecting the input voltage (VIN) to the voltage-controlled current source. SW2 is opened, allowing the current source, controlled by the input voltage signal VIN, to charge the integration capacitor C, producing an upwards ramping voltage shown as ‘A’ in Figure 11-5. The ramping is