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.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) |
|
|
|