Файл: Interfacing with C plus plus-programing communication with microcontrolers (K. Bentley, 2006).pdf

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 14.06.2025

Просмотров: 3589

Скачиваний: 1

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

426 13 THE PC TIMER

Duration is used to store the overall sampling duration, and is set to 5000 milliseconds (5 seconds). The variable named SamplingInterval is used to define the sampling interval and is set to 10 ms. Since the sampling interval is constant during this data acquisition process, SamplingInterval is declared as const. The identifier TempTime is used to temporarily store the time read by reading the PC timer.

Just prior to entering the do-while loop, the program resets the PCTimer object T, thereby establishing ‘zero time’. The loop will execute continuously with data read at 10 ms intervals. The if statement compares the current time with the time when the next sample must be taken. When the current time becomes greater than the time for the next sample, the ADC will be read, its value stored in the array Data[], and the time stored in array Time[]. The while loop terminates when the time exceeds the value of Duration.

All the data stored in the two arrays is then written to a data file named timestmp.dat. The ofstream constructor is called to create the file by instantiating the object os of type ostream. The file name timestmp.dat is passed as a parameter to the ofstream constructor. The for loop initialises the integer identifier j to zero and continues to write the values until the value of the subscript j reaches the number of data elements recorded in the do-while loop. The file os is then closed by calling the member function close() of the ofstream class. When the program has completed its execution, all data will be stored as two columns separated by a tab character in the text file timestmp.dat. The first column contains time values and the second column contains integer values representing the analog voltage output from the Charge/Discharge circuit.

Program 2 – Period.cpp

The second program retrieves the data from the disk file and stores this data in memory. It then processes the data to determine the period of the waveform that was sampled.

The steps involved in the second program are now given:

1.Read the data file and store the data values in memory.

2.Loop to find two data points one period apart:

-search until a data value in the second column is less than the threshold (Point O).

-continue searching until a data value is greater than the threshold; store its corresponding time value (Point A).

-continue searching until a number less than the threshold is found (Point B).

-continue searching until a number greater than threshold is found; store its corresponding time value (Point C). Then quit the loop.

3.Calculate the period of the waveform, being the difference between the two times (Point C – Point A).


13 THE PC TIMER 427

The program that performs the above data retrieval and data processing steps is shown in Listing 13-8 and does not need to use any of the object classes that have developed previously.

Listing 13-8 Program to determine the period of the output waveform - period.cpp

#include <iostream.h> #include <fstream.h>

void main()

{

// Pointer

to Time data

double *TimePtr;

double *DataPtr;

// Pointer

to ADC data

double MaxData = 0;

// Maximum

value of ADC data

int NumData=0;

// Number of data pairs in file

int i =0;

// Case in

initialised to 1

int Case = 1;

int Quit = 0;

// Quit = 0 means do not quit

unsigned char

Threshold;

double TimeA,

TimeC;

//Instantiate ifstream object (is). ifstream is("tmddata.dat");

//Read through the file to find the number of

//data pairs and the max value of ADC data. while(is)

{

is >> *TimePtr >> *DataPtr; if(!is.fail())

{

if(*DataPtr > MaxData) MaxData = *DataPtr;

NumData++;

}

}

//close input stream

is.close();

//Set Threshold based on MaxData Threshold = MaxData - 5;

//Alocate memory for Time and ADC data


428 13 THE PC TIMER

TimePtr = new double[NumData];

DataPtr = new double[NumData];

//Re-open the file so that reading starts from beginning. is.open("tmddata.dat");

//Read file and fill allocated memory

while(is)

{

is >> *(TimePtr+i) >> *(DataPtr+i); if(!is.fail())

i++;

}

//scan through all array elements pointed by DataPtr. for(int j = 0; j < i; j++)

{

switch(Case)

{

//Search for a Data element less than

//the threshold

case 1: if(*(DataPtr+j) < Threshold) Case = 2;

else break;

//Search for a Data element greater than the

//threshold. Note the time.

case 2: if(*(DataPtr+j) > Threshold)

{

TimeA = *(TimePtr+j); Case = 3;

}

else break;

//Search for a Data element less than

//threshold

case 3: if(*(DataPtr+j) < Threshold) Case = 4;

else break;

//Search for a Data element greater than

//threshold. Note the time. Set Quit flag


13 THE PC TIMER 429

case 4: if(*(DataPtr+j) > Threshold)

{

TimeC = *(TimePtr+j); Quit = 1;

}

}

if(Quit)

break;

}

//Clean up - deallocate dynamic memory delete TimePtr;

delete DataPtr;

//Display the time difference on the screen. cout << "The VCO signal period is ";

cout << TimeC - TimeA; cout << " ms." << endl;

}

The initial part of this program scans through the data file to determine the number of data pairs (time and ADC data) it contains, and stores this value in the variable NumData. During this process, the program also determines the maximum value of ADC data that was sampled, and stores this value in the variable named MaxData. A threshold value (calculated as five ADC units below the value of MaxData) is evaluated for use to determine the period of the measured waveform. Then the program dynamically allocates memory for time data and ADC data using the two pointer variables TimePtr and DataPtr. The data file is then closed and opened again so it can be re-read from its beginning to fill the dynamically allocated memory with its data.

The second part of the program scans through the data stored in the allocated memory, and evaluates the period of the signal as follows. It first searches for an element of ADC data (second column) that is below the threshold value. This would, for example, represent a point such as ‘O’ shown in Figure 13-6. Starting from this element, the program begins scanning the second column of data until an element with a value greater than the threshold is first encountered. For example, this point would represent a point such as ‘A’ shown in Figure 13-6. At this point the corresponding time value from the first column is stored. Scanning then continues down the second column of data while searching for an element less than the threshold value. Such a point would correspond to point ‘B’ in Figure 13-6. Recording of time is not needed for this point. Scanning continues down the second data column for the next number that is greater than the threshold value. This will correspond to point ‘C’ in Figure 13-6. The time corresponding to this instant is stored. The Quit flag will then be set since we no longer need to continue

430 13 THE PC TIMER

scanning the data. The period of the digitised waveform is then the difference between the time at point ‘A’ and ‘C’.

Note that the identifier Case (initially set to 1) is used to control the different phases of scanning the data. When the value of Case is 1, the ADC data array is first searched for an element pointed to by DataPtr that is less than the threshold. Once such an element has been found, the variable Case is set to 2 to commence the next phase of scanning. The identifier Quit is used in a logical sense. It is initially set to 0, meaning ‘do not quit’. The value of Quit is tested at the end of each iteration of the loop and if set, the loop will be terminated by executing the break statement. Once the last point (C) has been found, there is no need to proceed with scanning, so Quit is set to 1. The time difference representing the period of the signal is then calculated and printed on-screen.

13.9 Summary

In this chapter we learned how the built-in timer of the PC operates and how it can be used. The object class PCTimer has been developed with the capability to measure very long time periods. It has member functions to mark a time reference, accurately read the elapsed time, and also generate specific delays.

The PCTimer class operates without disabling the PC’s interrupts. As such, the interrupt service routines will generate short interruptions that can contribute to minor inaccuracies when measuring time. This was demonstrated when one of our example programs made repeated measurements of a ‘fixed-time’ event with interrupts enabled, and later with interrupts disabled. Other programs were presented in this chapter that measured a person’s reflex reaction time, generated a waveform plot using an accurate time-base, and used regular and accurate timing to digitise the electrical waveform produced by the interface board’s Charge/Discharge circuit.

13.10 Bibliography

Van Gilluwe, F., The Undocumented PC, Addison Wesley, 1994.

IBM, Technical Reference – Personal Computer AT, IBM Corporation, 1985.

Auslander D.M. and Tham, C. H., Real-Time Software for Control, Prentice Hall, 1990.

Intel, M8254 Programmable Interval Timer – Data Sheet, Intel Corporation, 1986.