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

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

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

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.

Appendix A - Hardware

Circuit Construction

Interface Board Bill of Materials

Circuit Construction

The interface board contains many different and independent circuit blocks to give the reader the option of working with any number of projects in any order. This flexibility also allows the reader to combine the board’s various circuit elements/blocks to form a wide range of custom projects. All circuit blocks need to be powered and most must be able to interface with the PC’s parallel port. To satisfy these needs, the interface board has its own power supply circuit block and a parallel port interface circuit block.

The power supply is the first circuit block that should be assembled, tested, and be operating properly - should any faults in the power supply be present, then only this part of the board will need to be investigated and debugged (assuming the interface pcb was properly checked to be functional). Next, the parallel port interface circuit block should be assembled, tested, and be operating properly before the other circuit blocks on the interface board are constructed (in any order).

IMPORTANT: The interface printed circuit board (pcb) should be checked for faults before proceeding to assemble and solder components onto the board. If in the very unlikely case the board does have faults, these faults can be detected quickly and simply on the unpopulated pcb and easily rectified as explained in the next section. The following sections provide instructions for the assembly of components, soldering, testing, and debugging of circuits.

Bare Printed Circuit Board

Visually check the bare printed circuit board (pcb) for any obvious short-circuit tracks or open-circuit tracks caused by faulty manufacture or handling. If any faults are detected, repair with a sharp blade and/or soldering iron as follows. Breaks in tracks can be repaired by scraping the coating from both sides of the break to expose the copper surface. Solder a piece of solid wire across the break. Shortcircuits can be removed by cutting between shorted tracks with a sharp blade.

Test the power supply tracks for short-circuits by measuring the resistance between the following power and ground paths using a multimeter:

 

+5V and GND.

 

12Vunreg and GND.

 

+9V and GND.

 

- 8V and GND.

The easiest place to probe each of these paths is at one of the pcb pads connected to that power path as shown in Figure A-1. In all cases the resistance should be at least several mega-ohms. If not, there is a short-circuit somewhere which needs to be detected visually, or by cutting/removing links to break the path up into more easily managed segments, and re-measuring with the multi-meter.

APPENDIX A - HARDWARE 433

:

:

:

:

Figure A-1 Test Power Supply track paths for short-circuits.

The Assembly Process

Equipment: 1 pair of Cutters/Nippers and 1 pair of small sized long-nose pliers.

The easiest approach to take when assembling the printed circuit board (pcb) is to manage the process in several stages, assembling on a project-by-project basis if convenient. This will simplify the testing and any debugging that might be necessary. However, if the whole board is to be completely assembled in just one stage, then the following guidelines should simplify and speed the process.

Components should be assembled and soldered flush with the pcb, starting with those that are lowest in height before proceeding with taller components. This strategy will result in the following order of assembly:

1.Flat mounting diodes and resistors.

2.IC sockets/ICs.

3.LEDs.

4.Pcb pins.

5.Capacitors that are small in size.

6.Vertically mounted resistors.

7.Terminal blocks and remaining tall components.

434 APPENDIX A - HARDWARE

As each component is fitted to the board, bend two of its leads over slightly to retain the part. When all components of this category are fitted, flip the board over and lay the partially complete assembly flat against the work surface (these components should now be in contact with the work surface). This will keep the parts positioned flush against the pcb, ready for soldering.

Note: It is easiest to fit a limited number of components at a time, solder them and then trim their leads close to the board surface using cutters, and then repeat these steps. This will avoid the situation occuring where a large number of component leads restrict access to a joint that needs to be soldered.

Many components must be correctly oriented when fitted to the board. These components include ICs, electrolytic capacitors, LEDs, diodes, and transistors. Figure A-2 shows the convention used to mark ICs to denote orientation with respect to pin number one. Usually a notch or dot is placed at the end of the IC where pin number one is found.

Pin 16

Pin 9

Notch

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Pin 1

 

 

 

 

 

 

 

 

 

Pin 8

Pin 1 etc.

 

 

 

 

 

 

 

 

 

 

 

 

Figure A-2 Marking of IC orientation and pin numbering (top view).

When using IC sockets (recommended) make sure the socket is soldered into the board with it’s ‘notched’ end corresponding to the IC outline marked on the board. The notched end of the socket will provide the marker for correct orientation when fitting the IC into its socket.

Other components have characteristic marking schemes to denote polarity. Electrolytic capacitors are marked with either a + or - sign on their body. To determine correct LED orientation, look through the transparent coloured body at the two terminals as shown in Figure A-3. Typically, the smaller shaped contact is the anode and the other contact the cathode.

Anode Cathode

Anode Cathode

Figure A-3 LED polarity.

APPENDIX A - HARDWARE 435

Diodes are marked with a line across the length of their body to represent the cathode. Transistors are correctly fitted to the pcb by complying with the pcb overlay markings that show appropriate orientation of the transistor bodies. Importantly, CMOS devices are sensitive to damage from static electricity and are preferably assembled with antistatic precautions in place.

Note: apply a thin film of heatsink paste to the flat metalised surfaces of the transistors and voltage regulators before fitting heatsinks. This will improve the transfer of heat to the heatsink.

The Soldering Process

Equipment & Materials: Soldering iron (> 15 Watts) and electronics grade solder (0.7 mm to 1.0 mm diameter, 60% tin, 40% lead with flux inside).

Soldering by hand can be described basically as a process where heat is transferred to the joint to be soldered, followed by the application of solder that then melts and flows into and around the joining materials. The heat source is removed and the molten solder solidifies, forming a connection between the component lead and the pcb pad.

Preparation: To ensure successful soldering, the soldering iron tip and the joint itself must be clean. Normally the printed circuit board and the component lead passing through the board will be sufficiently clean. Unfortunately, the same cannot be said for the soldering iron tip. To clean the iron tip, wet a piece of ‘kitchen’ sponge to be damp but not soaked, and then repeatedly wipe the iron tip across the sponge until the tip is in a shiny metallic state. If the iron tip cannot be brought to a shiny metallic state, try tinning the soldering iron tip by applying solder to it, waiting a short period of time and then wiping it against the sponge. If this fails to work, a new soldering iron tip is probably needed.

Soldering: This process takes place in three steps:

1.Lightly wet the iron tip with solder (the tip may be sufficiently wet with solder from the previous soldering operation). This improves the rate at which heat can be transferred from the iron tip to the joint.

2.Heat the joint with the iron tip for several seconds.

3.Apply solder sparingly to the heated joint - it will flow to fill the joint if the joint was heated sufficiently during step 2.

Note: When applying solder to the joint in step 3, do not be tempted to apply solder to the iron tip in order to melt the solder (this can result in a poorly soldered joint). If the joint is heated sufficiently in step 2, solder flows into the joint as required. Figure A-4 shows the shape of a correctly soldered joint before and after trimming using cutters.

436 APPENDIX A - HARDWARE

Solder Joint

Trimmed Lead

Board Underside

PCB

Component Side

Figure A-4 Correctly soldered and trimmed joint (plated through hole).

Schematic Diagram Conventions

The following information will help you interpret the schematic circuit diagrams when studying the circuit blocks and during times of test and debugging:

Inputs to circuits are generally drawn on the left side of the component and outputs are generally drawn on the right side of the component.

Logic circuits or circuits having logic inputs and/or outputs sometimes have small circles placed on their input or output pins at the border of the circuit block. This denotes an active low pin. In the case of an input, a low logic level will activate the pin. For the case of an output, the output pin will be at a low logic level when in an active state.

The small dots at the ends of input and output lines are pcb pins. Solid dots show connection between signal lines.

VCC denotes logic circuit +5V.

There are two types of grounds shown on the schematic; horizontally-lined triangular shape (digital ground) and hollow triangular shape (analog ground). They are connected together on the printed circuit board and will both be at 0V.

Testing and Debugging

Testing and debugging should take place in a systematic manner to minimise the time and effort required. The easiest means of achieving this is to reduce the size or scope of the system under test by breaking it into smaller circuit blocks and testing them separately in-turn.

When testing circuit blocks; the input voltages/currents must be set to appropriate levels for that block before the outputs are checked. If a fault in circuit function is detected, it will usually be caused by factors such as:

incorrect wiring lead connections.

incorrect value or type of a component(s).

incorrect orientation of a component(s).

APPENDIX A - HARDWARE 437

poor/inadequate solder joint or wiring connection.

short-circuit due to solder bridging.

defective pcb track(s) caused by faulty manufacture, improper handling of the pcb (excessive flexing, scoring, etc), and damage from poor repair by the user.

incorrect power supply voltage to circuit(s).

damaged component(s).

The typical steps in circuit test and debugging are:

1.Check for correct wiring connections.

2.Check that the correct components are installed in the right position, with proper orientation.

3.Check correct power supply to a circuit. If the voltage to the circuit is not correct:

Feel the IC to check if the package is unusually warm or hot; indicating an overloaded or damaged IC that may need to be replaced. Also, check if other components connected to the power supply are also excessively hot.

Visually check the tracks and solder joints for any unintended shortcircuits and also for unintended open-circuits or inadequate soldering. If necessary, follow this with an electrical continuity test using a multimeter as discussed below.

4.Check that voltages or currents to circuit inputs are set at the correct levels.

5.Test that circuit outputs are correct for the given state of the inputs – if this is not so:

The outputs are excessively loaded by; connection to other circuits or components on the board, or by short-circuited track(s).

The component is faulty and needs to be replaced.

CONTINUITY TESTS

Continuity tests are performed to detect short-circuits and open-circuits using a

multi-meter switched to ‘resistance’ mode. The power to all circuitry MUST be TURNED OFF before commencing with continuity tests.

As mentioned earlier, to simplify testing and debugging, one circuit block should be assembled and then tested at a time, before proceeding to build the next circuit block. The testing and debugging of these circuit blocks is explained in the following text, presented in the order the related projects appear through the book.

Removing Components

The following instructions are recommended for removing components from the pcb (and not damaging it) with the aid of the most basic hand tools. More elaborate