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

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

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

12 DATA ACQUISITION WITH OPERATOR OVERLOADING 397

overloaded in the DCMotor class to decrement the Speed by 1 unit of resolution. A segment of sample code that uses such overloaded operators could be:

DCMotor Motor1;

Motor1++; // increase speed by 1

Another example would be to overload the << operator in the DAC class so it can output the integer value N to the Digital-to-Analog Converter:

DAC Dac;

Dac << N;

12.4 Summary

There are several methods that can be used to pass parameters into functions and return the result from the function. Most functions receive their parameters as a copy of the argument given in the calling environment. An alternate means of passing parameters is pass by reference. When passing parameters by reference the function has access to the actual argument used in the calling environment. This saves memory space and also provides an improvement in speed. For reasons such as these, pass by reference is generally preferred when passing class objects to functions.

Similarly, most functions are written to return values as a copy of the value generated within the function. If values are returned by reference, the real object within the function is returned rather than a copy of it. This facility can be used efficiently in operator overloading, in particular with the chained use of an operator.

Friend functions are a special category of functions that have unrestricted access to all members of the class they are declared in. Although they are not member functions, they have all the privileges of a member function. Friend functions have a further advantage over member functions in that they do not need to be tagged to an object when being called (using ‘.’ or ‘->’ membership operators).

12.5 Bibliography

Winston, P.H., On to C++, Addison Wesley, 1994.

Johnsonbaugh, R and Martin Kalin, Object-Oriented Programming in C++, Prentice Hall, 1995.

Staugaard A. C. (Jr), Structured and Object Oriented Techniques, Prentice Hall, 1997.

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

Wang, P.S., C++ with Object Oriented Programming, PWS Publishing, 1994.

13

The PC Timer

Inside this Chapter

ξ

ξ

ξ

ξ

ξ

ξ

ξ

What is a timer?

The PC timer architecture.

Programming the PC timer.

Time measurement.

Reflex measurement.

Plotting with a time-base.

Digitising a signal with a time-stamp.

13.1 Introduction

So far we have not used real-time for tasks that have involved timing. Recall the generation of PWM signals from Chapter 8. In these programs we generated time delays by executing software loops whose duration was unknown and dependent on the computer’s speed. A hardware timer is typically used when time needs to be measured accurately. Your PC is equipped with such a timer that can be programmed to carry out various timing-related tasks. It operates independently of the PC’s processor to ensure uninterrupted and accurate operation, and has spare resources for us to use in our own programs.

In general, the timer subsystem of your PC has three independent timers. More modern systems will have five independent timers. We will keep our discussion to the most general case, i.e. three timers. There are two principal functions associated with timers; timing of an event and counting events. The basic requirement for any timer is a clock signal; being a continuous train of pulses with a known and highly stable frequency. Having access to a steady clock allows us to write programs that can take advantage of real-time operations.

13.2 PC Timer System

Central to the timing system of all but the most recent PC’s is the 8254 Programmable Interval Timer containing three timers, named Timer 0, Timer 1 and Timer 2 as shown in Figure 13-1. The timers can be operated in several different modes controlled by gate signal level and the use of a control register (explained in section 13.2.2). These modes include single timeout, square wave generator and rate generator, discussed in section 13.2.3. They share a common clock signal driving their clock inputs, but only Timer 2 has a gate input that is free to be controlled through software.

Each of the timers contains a 16-bit counter. A counter can be considered as a special memory location in hardware, the value of which is incremented or decremented by each incoming clock pulse. In your PC, each clock pulse drives the counters down a count value. Typically, the counter’s output signal will change state when it reaches zero.

Because all three timers share the same fixed clock signal, they cannot be used for event counting. Event counting takes place when a counter/timer is used to count external pulses applied to its clock input - often arriving at irregular intervals. Regardless of the speed of a PC, its clock frequency will be 1.1932 MHz. This enables every PC to maintain a fixed standard for timing.

13 THE PC TIMER 401

Clock

 

 

 

Clock

Timer 0

 

 

 

 

 

 

 

 

(1.1932 MHz)

 

 

 

 

Timer Interrupts

 

 

Gate

(16-bit π counter)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Clock

Timer 1

 

 

 

Periodic Pulses to

 

 

 

 

 

 

 

 

 

 

 

 

Gate

(16-bit π counter)

 

 

 

Refresh Dynamic

 

 

 

 

 

 

 

RAM

 

 

 

 

Clock

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Programmable

 

 

 

Timer 2

 

 

 

 

 

 

 

Gate

 

 

 

 

 

 

 

(16-bit π counter)

 

 

 

 

Signal 1

 

 

 

 

 

 

 

 

 

To Speaker

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Drive Circuitry

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Programmable

 

 

 

 

AND

 

 

 

 

 

 

 

 

Gate

 

 

 

 

 

Signal 2

 

 

 

 

 

 

 

 

 

 

 

 

Figure 13-1 PC Timing System - 3 timers (clock input, gate, and output).

The three timers in your PC have special tasks assigned to them, explained as follows.

TIMER 0

Timer 0 is used to generate the so-called timer interrupt. The timer interrupt will regularly trigger the CPU to execute a special routine that updates the system time. This action takes place every 54.9 milliseconds. The gate signal of Timer 0 is held permanently at logic-HIGH, and therefore is not programmable. Since the clock and the gate are not programmable, the only timer variables that can be altered are the count value written to the counter and the mode of operation. Timer 0 has an output latch register that allows software to read the count value. Furthermore, the state of its output signal can be determined (i.e. high or low) by reading Timer 0’s status register.

NOTE

An interrupt is a signal generated by hardware or software that is sent to the CPU

to request its attention. Depending upon the priority of an interrupt, it will be

attended to immediately or flagged for later attention. The interrupt generated by

timer 0 has the highest priority and will be attended to immediately by the CPU.

402 13 THE PC TIMER

TIMER 1

Timer 1 is used to generate a periodic signal for driving the hardware that refreshes the dynamic RAM (memory). Timer 1 also has its clock and gate signals permanently wired. Like Timer 0, it has an output latch register that allows software to read the count value. The state of its output signal can be determined by reading Timer 1’s status register.

TIMER 2

Timer 2 is used to drive the speaker via an AND gate whose other input is programmable (see Figure 13-1). Its clock signal is permanently connected, although the gate is free to be controlled through software. Timer 2 also has an output latch register that allows software to read the count value. Similarly, the state of its output signal can be determined by reading Timer 2’s status register.

13.2.1 Configuring the Counters

As mentioned earlier, all three timers countdown when operating. The number loaded into each timer’s counter determines the timing duration. Therefore, writing a smaller number to a counter will result in a shorter time interval before it reaches zero. A particular counter can be read or written to at any time.

The counter’s value is constantly changing as it operates and can be read by sampling the count value and storing the result. The hardware device that performs such a function is known as a latch register. Each timer has an input latch register to allow writes to its counter (load it) and also an output latch register to allow its counter’s value to be read. This event is also dependent upon the status of the gate and the mode of operation.

A special Control Register is used to provide the facility to select which counter we wish to write to or read from, set the mode of operation, set the number format, etc. There is one more register in addition to the three Timer registers and the Control Register. This register allows access to the output of Timer 2 and the speaker gate control signal.

Timer Ports

Table 13-1 Port addresses - PC timers and speakers.

Address

Function

0x40

Timer 0 data latch register (input and output).

0x41

Timer 1 data latch register.

0x42

Timer 2 data latch register .

0x43

Control Register for Timers 0, 1 and 2.

0x61

Timer 2 output and speaker control.

13 THE PC TIMER 403

The timers are part of your hardware system. To be able to program the timers, software must have access to timer subsystem hardware. This is possible by accessing the timer’s ports similar to the way we accessed the parallel port hardware using its port addresses. The port addresses associated with the timer and speaker system are given in Table 13-1.

Programming the timer starts with programming the Control Register as described in the following sections.

13.2.2 The Control Register

The Control Register is used to configure a timer. It can also be used to request the counter status or to latch the current count value. The register contains eight bits that must be appropriately set to enable the following:

ξ

Select a particular counter.

ξ

Specify the byte(s) used to Read/Load the counter.

ξ

Specify the mode of operation.

ξ

Specify the counting format in binary or BCD.

The configuration of the Control Register is shown in Table 13-2.

Table 13-2 Configuration of the Control Register (address 0x43).

Bit 7

Bit 6

Bit 5

Bit 4

Bit 3

Bit 2

Bit 1

Bit 0

SC1

SC0

RL1

RL0

M2

M1

M0

BCD

 

 

 

 

 

 

 

 

 

Read/Load

 

 

 

Binary

Select Counter

MSB/LSB

 

Mode Select

 

or BCD

Select Counter

The counter can be selected by programming Bits 6 (SC0) and 7 (SC1) of the Control Register as shown:

SC1

SC0

Counter

0

0

Counter 0

01 Counter 1

10 Counter 2

Byte(s) used to Read/Load Counters

Bits 4 (RL0) and 5 (RL1) can be programmed to choose either Least Significant Byte (LSB) only, or the Most Significant Byte (MSB) only, or to use both bytes of the counter when counting:

404 13 THE PC TIMER

Operation

RL1

RL0

Counter Latch

0

0

Read/Load LSB

0

1

Read/Load MSB

1

0

Read/Load LSB then MSB

1

1

 

 

 

For example; if the LSB is to be written to a counter, RL1 and RL0 will need to be set at 0 and 1 respectively. The LSB is then loaded into the counter by writing to its data register (e.g. 0x40 for Timer 0).

If the MSB is to be written to a counter, RL1 and RL0 will need to be set to 1 and 0 respectively. The MSB is then loaded into the counter by writing to its data register (e.g. 0x40 for Timer 0).

If a 16-bit number (two bytes) is to be written to a counter, both RL1 and RL0 will need to be set to 1. The 16-bit number will be loaded into the counter by carrying out two consecutive write operations to the appropriate data register (e.g. 0x40 for Timer 0) by first writing the LSB followed by writing the MSB.

Counting format (Binary/BCD)

The mode of counting can be set to binary (typically used) or BCD (Binary Coded Decimal) - we don’t explain counting in BCD mode, so set the bit to 0.

BCD

Counting Operation

0

Binary

1

Binary Coded Decimal (BCD)

 

 

Timer mode of operation

Mode Select

M2

M1

M0

Mode Name

0

0

0

Mode 0

0

0

1

Mode 1

0

1

0

Mode 2

0

1

1

Mode 3

1

0

0

Mode 4

1

0

1

Mode 5

1

1

0

Mode 2

1

1

1

Mode 3

 

 

 

 

13 THE PC TIMER 405

The Timer’s modes of operation are explained in the next section. They are set using the Control Register bits 1, 2 and 3 as shown in the previous table.

13.2.3 Modes of Operation of the Timers

There are up to six different modes of timer operation. Timer 0 and Timer 1 have their gates hard-wired to a logic-HIGH level. This excludes them from operating in some of the modes described below. Timer 2 is the only timer that has a controllable gate. As such it can operate in all six modes.

Count

Time

Mode 0: Single Timeout

In this mode, the counter generates a low-level output signal for the fixed number of clock pulses loaded into its data register. Each incoming clock pulse will decrement the count value by one count (provided the gate input is high). When the count value reaches 0, the output line will change from low to remain high. Note that the maximum period is obtained when the decimal number 65,535 is loaded into the counter. This period will be approximately 54.9 ms.

All three counters can be programmed in this mode. To initiate single timeout operation, first configure the timer to operate in this mode and then write the count value to the data register. The countdown will begin immediately after writing the count data. If the gate signal is held low, counting stops until the gate signal returns high. Timers 0 and 1 have their gate signals hard-wired to a logic-HIGH level. This is not the case for Timer 2, whose gate input can be controlled by writing to bit 0 of the port 0x61. The bits of the port at 0x61 for operating Timer 2 are shown in Table 13-3.

Table 13-3 Bits at port address 0x61 (control of Timer 2 output & speaker).

Bit7

Bit6

Bit5

Bit4

Bit3

Bit2

Bit1

Bit0

X

X

Timer 2

X

X

X

Speaker

Timer 2

OUT

Gate

GATE

 

 

 

 

 

 

 

 

 

 

Count

 

 

 

 

 

 

 

Time

 

 

Mode 1: Re-triggerable one-shot

This mode is used to generate a low-level pulse following a trigger command from the gate (hence only Timer 2 can use this mode). The duration of the pulse is set by the count value loaded into the counter. The gate signal is briefly sent from low to high and back to low to initiate counting down. As this happens, the counter output will drop from a high state to a low state. When the count value has decremented to zero, the output will return to a high state.

406 13 THE PC TIMER

The previously used count value is automatically reloaded into the counter at the end of the countdown when the counter reaches zero. Another one-shot period is generated when another high pulse is applied to the gate (hence the term ‘retriggerable’). The timer can also be re-triggered during the current countdown. In this case, a new countdown will start immediately after the re-trigger. A new count value can also be written to the input latch register during the current countdown; this will not affect the current countdown. The new count value in the input latch register will be loaded for the next countdown immediately after the next retrigger.

Count Count

Time Time

Mode 2: Rate generator

This mode is used to periodically generate a narrow low-level output pulse. When the value that was loaded into the counter reaches 1, the output changes to a low state for one clock period. The count value is then automatically reloaded to repeat the same process. All three timers can be used in this mode. Counting is stopped whenever the gate signal is low (only applies to Timer 2). The rate generator is predominantly used to generate hardware interrupts at regular intervals since the narrow pulse can be missed when detecting using software means.

Count

Count

Count

Time

Time

Time

Mode 3: Square wave generator

This mode produces a continuous square wave output. Everytime the timer’s counter reaches zero, the output toggles and the count value is automatically reloaded into the counter from its input latch. Note that the gate must be high to enable down-counting and the count is decremented by two for each clock pulse. All three timers can operate in this mode. Timer 0 generates the timer interrupt when configured in this mode.

Count

Time

Mode 4: Software triggered strobe

Counting down is initiated when a count value is written to the data register. This mode produces a single narrow output pulse for one clock period when the count reaches zero (a non-periodic narrow pulse is known as a strobe). The counter will remain inactive until at a later time software again writes to the counter. The gate must be high at all times to enable operation.

Count

Time

Mode 5: Hardware triggered strobe

This mode is identical to Mode 4 except triggering is carried out by hardware means. The start of a countdown is triggered by applying a brief high-level pulse to the gate. The output will pulse low for one clock period when the count reaches

13 THE PC TIMER 407

zero. The counter will remain inactive until the next hardware trigger. As for mode 1, this mode is only possible with Timer 2.

13.2.4 Read-back Commands

A timer ‘read-back’ command allows the following data to be read from a counter; the count value, status of its output signal, read/load status, configuration mode, and count mode. Two very useful ‘read-back’ tasks can be invoked by writing the bit patterns described below to the Control Register.

Task 1 - Multi Counter Latch:

A counter is latched by taking a ‘snap shot’ of the selected counter’s value and transferring that count value to the counter’s output latch register. A command to latch one or more counters can be issued by writing the bit pattern shown below to the Control Register at address 0x43:

Control Register at address 0x43.

Bit 7

Bit 6

Bit 5

Bit 4

Bit 3

Bit 2

Bit 1

Bit 0

1

1

0

1

CT2

CT1

CT0

0

 

 

 

 

 

 

 

 

Setting of bits CT0, CT1, and CT2 determines which counter(s) will be latched. Once latched, a program can read the count value by reading the output latch register. Addresses of latch registers are shown in Table 13-1. Two 8-bit reads of the output latch register must be carried out to read the 16-bit count value. The low byte will be obtained in the first read followed by the high byte in the second read.

Task 2 - Status of Timers:

The status information for each timer can be read via the corresponding timer data register. The command to report the status is issued by assembling the byte as shown following, and writing it to the Control Register (at address 0x43). We select the timer(s) by setting their respective control bits CT0, CT1 and CT2 of the Control Register to 1. The 8-bit status value is obtained by reading the data register of the selected timer (addresses are shown in Table 13-1). The status information is interpreted according to Figure 13-2.

Control Register at address 0x43.

Bit 7

Bit 6

Bit 5

Bit 4

Bit 3

Bit 2

Bit 1

Bit 0

1

1

1

0

CT2

CT1

CT0

0