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

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

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

Interfacing with C++

Jayantha Katupitiya Kim Bentley

Interfacing with C++

Programming Real-World Applications

BCA

Dr. Jayantha Katupitiya

Senior Lecturer

School of Mechanical and

Manufacturing Engineering

The University of New South Wales

Sydney NSW 2052, Australia

Email: J.Katupitiya@unsw.edu.au

Mr. Kim Bentley

Library of Congress Control Number: 2005937895

ISBN-10 3-540-25378-5 Springer Berlin Heidelberg New York

ISBN-13 978-3-540-25378-5 Springer Berlin Heidelberg New York

This work is subject to copyright. All rights are reserved, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilm or in any other way, and storage in data banks. Duplication of this publication or parts thereof is permitted only under the provisions of the German Copyright Law of September 9, 1965, in its current version, and permission for use must always be obtained from Springer. Violations are liable for prosecution under the German Copyright Law.

Springer is a part of Springer Science+Business Media springer.com

c Springer-Verlag Berlin Heidelberg 2006 Printed in The Netherlands

The use of general descriptive names, registered names, trademarks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use.

Typesetting: by the authors and TechBooks using a Springer LATEX macro package

Cover design: design & production GmbH, Heidelberg

Printed on acid-free paper

SPIN: 11015543

89/TechBooks

5 4 3 2 1 0

 

Table of Contents

 

1 GETTING STARTED ....................................................................................

1

1.1

INTRODUCTION................................................................................................

2

1.2 PROGRAM DEVELOPMENT SOFTWARE............................................................

2

1.3 A C++ PROGRAM ............................................................................................

6

1.4 USE OF FUNCTIONS .......................................................................................

10

1.5 FUNDAMENTAL DATA TYPES........................................................................

15

1.6 FUNCTIONS WITH PARAMETERS AND RETURN VALUES ...............................

18

1.7

SUMMARY .....................................................................................................

21

1.8

BIBLIOGRAPHY..............................................................................................

22

2 PARALLEL PORT BASICS AND INTERFACING ................................

23

2.1

INTRODUCTION..............................................................................................

24

2.2 WHAT IS THE PARALLEL PORT?....................................................................

24

2.3 DATA REPRESENTATION ...............................................................................

30

2.4 PROGRAM DEMONSTRATING HEXADECIMAL TO DECIMAL .....................

32

2.5

SUMMARY .....................................................................................................

33

2.6

BIBLIOGRAPHY..............................................................................................

33

3 TESTING THE PARALLEL PORT...........................................................

35

3.1

INTRODUCTION..............................................................................................

36

3.2

INTERFACE BOARD POWER SUPPLY..............................................................

36

3.3

PARALLEL PORT INTERFACE.........................................................................

39

3.4 BASIC OUTPUT USING THE PARALLEL PORT ................................................

43

3.5 BASIC INPUT USING THE PARALLEL PORT....................................................

46

3.6

COMPENSATING FOR INTERNAL INVERSIONS................................................

50

3.7

SUMMARY .....................................................................................................

55

3.8

BIBLIOGRAPHY..............................................................................................

56

4 THE OBJECT-ORIENTED APPROACH.................................................

57

4.1

INTRODUCTION..............................................................................................

58

4.2 CONCEPTUAL AND PHYSICALLY REALISABLE OBJECTS...............................

58

4.3 REAL OBJECTS ..............................................................................................

59

4.4 OBJECT CLASSES...........................................................................................

61

4.5

ENCAPSULATION ...........................................................................................

63

4.6 ABSTRACT CLASSES......................................................................................

64

4.7 CLASS HIERARCHIES .....................................................................................

64

4.8

INHERITANCE ................................................................................................

65

4.9

MULTIPLE INHERITANCE...............................................................................

66

4.10 POLYMORPHISM ..........................................................................................

66

4.11 AN EXAMPLE OBJECT HIERARCHY .............................................................

67

4.12 ADVANTAGES OF OBJECT-ORIENTED PROGRAMMING ...............................

72

4.13 DISADVANTAGES OF OBJECT-ORIENTED PROGRAMMING ..........................

72

4.14 SUMMARY ...................................................................................................

73

VI TABLE OF CONTENTS

 

4.15 BIBLIOGRAPHY............................................................................................

73

5

OBJECT-ORIENTED PROGRAMMING.................................................

75

 

5.1

INTRODUCTION..............................................................................................

76

 

5.2 NAMING CONVENTION..................................................................................

76

 

5.3 DEVELOPING AN OBJECT CLASS ...................................................................

77

 

5.4

PARALLEL PORT CLASS – STAGE I................................................................

82

 

5.5 USING CLASS OBJECTS IN PROGRAMS ..........................................................

87

 

5.6

PARALLEL PORT CLASS – STAGE II ..............................................................

94

 

5.7

PARALLEL PORT CLASS – STAGE III.............................................................

99

 

5.8

SUMMARY ...................................................................................................

103

 

5.9

BIBLIOGRAPHY............................................................................................

103

6

DIGITAL-TO-ANALOG CONVERSION ...............................................

105

 

6.1

INTRODUCTION............................................................................................

106

 

6.2

DIGITAL-TO-ANALOG CONVERSION ...........................................................

106

 

6.3 PROGRAMMING THE DIGITAL-TO-ANALOG CONVERTER ...........................

117

 

6.4 DERIVATION OF OBJECT CLASSES ..............................................................

121

 

6.5 ADDING MEMBERS TO DERIVED CLASSES..................................................

129

 

6.6

SUMMARY ...................................................................................................

145

 

6.7

BIBLIOGRAPHY............................................................................................

146

7

DRIVING LEDS .........................................................................................

147

 

7.1

INTRODUCTION............................................................................................

148

 

7.2

ITERATIVE LOOPS........................................................................................

148

 

7.3

BRANCHING.................................................................................................

152

 

7.4

ARRAYS.......................................................................................................

157

 

7.5

POINTERS.....................................................................................................

160

 

7.6 USING POINTERS .........................................................................................

175

 

7.7 MACROS ......................................................................................................

184

 

7.8 DYNAMIC MEMORY ALLOCATION ..............................................................

185

 

7.9 EXCEPTION HANDLING ...............................................................................

189

 

7.10 SUMMARY .................................................................................................

194

 

7.11 BIBLIOGRAPHY..........................................................................................

195

8 DRIVING MOTORS - DC & STEPPER..................................................

197

 

8.1

INTRODUCTION............................................................................................

198

 

8.2 DC MOTORS................................................................................................

198

 

8.3 STEPPER MOTORS .......................................................................................

202

 

8.4 A CLASS HIERARCHY FOR MOTORS ...........................................................

211

 

8.5 VIRTUAL FUNCTIONS – AN INTRODUCTION ...............................................

212

 

8.6 VIRTUAL FUNCTIONS - APPLICATION .........................................................

233

 

8.7 KEYBOARD CONTROLS ...............................................................................

256

 

8.8

SUMMARY ...................................................................................................

270

 

8.9

BIBLIOGRAPHY............................................................................................

271

TABLE OF CONTENTS VII

9

PROGRAM DEVELOPMENT TECHNIQUES......................................

273

 

9.1 INTRODUCTION............................................................................................

274

 

9.2 EFFICIENT CODING TECHNIQUES ................................................................

274

 

9.3 MODULAR PROGRAMS ................................................................................

282

 

9.4 CASE STUDY - MOTOR DRIVER PROGRAM .................................................

289

 

9.5 SUMMARY ...................................................................................................

302

 

9.6 BIBLIOGRAPHY............................................................................................

302

10 VOLTAGE AND TEMPERATURE MEASUREMENT........................

303

 

10.1

INTRODUCTION..........................................................................................

304

 

10.2 CONVERTING A VOLTAGE TO A DIGITAL PULSE-TRAIN...........................

304

 

10.3 TEMPERATURE MEASUREMENT ................................................................

305

 

10.4 THE OBJECT CLASS VCO...........................................................................

306

 

10.5 MEASURING VOLTAGES USING THE VCO ................................................

311

 

10.6 GRAPHICS PROGRAMMING – SQUARE WAVE DISPLAY ............................

318

 

10.7 TEMPERATURE MEASUREMENT ................................................................

324

 

10.8

SUMMARY .................................................................................................

328

 

10.9

BIBLIOGRAPHY..........................................................................................

329

11

ANALOG-TO-DIGITAL CONVERSION................................................

331

 

11.1

INTRODUCTION..........................................................................................

332

 

11.2 ANALOG-TO-DIGITAL CONVERSION .........................................................

332

 

11.3 CONVERSION TECHNIQUES .......................................................................

334

 

11.4 MEASURING VOLTAGES WITH AN ADC....................................................

341

 

11.5 AN OBJECT CLASS FOR THE ADC .............................................................

347

 

11.6 MEASURING VOLTAGE USING THE ADC ..................................................

356

 

11.7 MEASURING TEMPERATURE USING THE ADC..........................................

359

 

11.8

SUMMARY .................................................................................................

362

 

11.9

BIBLIOGRAPHY..........................................................................................

362

12 DATA ACQUISITION WITH OPERATOR OVERLOADING............

363

 

12.1

INTRODUCTION..........................................................................................

364

 

12.2 OPERATOR OVERLOADING........................................................................

364

 

12.3 DATA ACQUISITION...................................................................................

393

 

12.4

SUMMARY .................................................................................................

397

 

12.5

BIBLIOGRAPHY..........................................................................................

397

13

THE PC TIMER..........................................................................................

399

 

13.1

INTRODUCTION..........................................................................................

400

 

13.2 PC TIMER SYSTEM ....................................................................................

400

 

13.3

PROGRAMMING THE TIMER.......................................................................

408

 

13.4 THE OBJECT CLASS PCTIMER ...................................................................

409

 

13.5 MEASUREMENT OF TIME...........................................................................

415

 

13.6 REFLEX MEASUREMENT ...........................................................................

417

 

13.7 GENERATING A TIME-BASE ......................................................................

419

 

13.8 DATA ACQUISITION WITH TIMESTAMP .....................................................

423

 

13.9

SUMMARY .................................................................................................

430

 

13.10 BIBLIOGRAPHY........................................................................................

430

VIII TABLE OF CONTENTS

APPENDIX A - HARDWARE...........................................................................

431

CIRCUIT CONSTRUCTION ..................................................................................

432

INTERFACE BOARD BILL OF MATERIALS ..........................................................

476

APPENDIX B - SOFTWARE ............................................................................

479

C++ KEYWORDS ...............................................................................................

480

OPERATOR PRECEDENCE ..................................................................................

481

ASCII CHARACTER SET....................................................................................

482

INDEX ................................................................................................................

483

This Book is Written For…

C++ is considered by many to be among the most widely used and powerful object-oriented programming language in industry today. This book is for people who are interested in learning and exploring C++ programming in a fresh and enjoyable environment where programs are developed to interface with real world devices. Other people may leave learning C++ for a later time, instead choosing to interact with various hardware devices by simply running the fully developed programs supplied with this book.

Many readers may already have acquired some knowledge of C++ programming but know little about how to interface a computer to physical devices and want to know more. You might be an engineer, scientist, programmer, technical personnel, hobbyist, student in a technically related field or someone who is simply interested in programming and interfacing a computer to perform real activities.

Inside This Book…

C++ programming is approached in a straightforward, practical and simplified manner using mostly short programs that are clearly explained. You will explore areas of electronics integral to a wide range of modern technologies using an interface board specially developed to support all projects described in this book.

The intertwining of C++ programming and electronics knowledge takes place as we work through interesting and enjoyable real-world projects. These projects encompass the following topics:

ξ

Digital Input and Output.

ξ

Analog-to-Digital Conversion and Digital-to-Analog Conversion.

ξ

DC Motor and Stepper Motor Control.

ξ

Measuring Voltage, Temperature, and Time.

Important concepts are reinforced during the learning and exploration process as we gradually progress from simple straightforward projects to those that are more advanced. Projects on the interface board have been developed as independent modules. This allows readers with C++ programming knowledge to build and play with whichever projects they wish, in any order.

For those readers who want to know how to manage the development of larger programs, a chapter has been specially written to cover the process of program development, demonstrated with the use of a program from an earlier chapter. In this chapter we cover topics such as coding techniques, generating header files and building libraries.

X

What is C++?

C++ is a language used to program computers to perform specific tasks. There exist many other popular programming languages including C, Pascal, FORTRAN, BASIC, Cobol and Modula II. Computers operate using instructions based on binary format, i.e. on and off states (or ones and zeros). Programming languages allow the programmer to use a language similar to that normally written and then generate computer-based instructions for program execution. Specialised software is used to manage the task of developing programs; in particular converting the program written in its programming language to binary form needed by the computer.

In the recent past the language known as C became very popular and was the most significant commercially used programming language. The C language was developed in response to the need for a good programming language to develop the UNIX operating system. While it is considered a high-level language, it also has many low-level features. This is of great benefit when programs need to work with hardware. On the other hand it was also well suited to performing numerical operations. It can match the capabilities of FORTRAN and Pascal (a language able to handle complex logic). These are some of the reasons for the popularity of the C language.

As the size of programs increased, the benefits of being able to reuse millions of instructions written and assembled by programmers around the world, became apparent. Soon afterwards the concept of object-oriented programming (OOP) was born and the C++ language came into being, evolved from C. C++ can be considered an expanded and better C. In other words, C became a subset of C++. The programmer could now combine associated data and functions to avoid inadvertent misuse. The so-called virtual functions in C++ added extra flexibility allowing decision-making at run time, rather than at compile time. While C++ has gained all this extra power, it has retained other good features of C such as lowlevel bit and byte operations, easy input and output to ports, etc. In today's world, C++ is the most widely used programming language for sophisticated tasks.

Compiler and Operating System Compatibility

Most programs in this book have been written to carry out some form of interfacing task. An essential feature of such programs is the ability to read from and write to the hardware ports. Some operating systems such as DOS, Windows 3.1, Windows 95/98 allow programs to directly access ports. Other operating systems such as Windows NT/2000/XP and Linux do not allow direct port access. These operating systems will only allow programs to access ports via a piece of software known as a device driver that has the necessary privileges to access ports. The application programs access the ports via the device drivers.

Borland C++ for DOS

Apart from the programs using exception handling (See Chapter 7), all programs in the textbook can be compiled and linked using Borland C++ without any changes to generate executable files. All program listings that are to be compiled using Borland C++ are located in the directory ‘BC++’ on the companion CD.

GNU C++ for Linux

The programs in the textbook have been modified to request the required privileges to enable them to run under Linux with port access. The modified versions of programs can be found in the directory ‘GNUC++’ of the companion CD. If a make file is necessary, it is also included in the appropriate chapter subdirectories of the directory GNUC++. Graphics programs, keyboard control programs and PC timer related programs are not available to run under Linux.

Microsoft Visual C++ for Windows

The modified versions of the programs that can be used with Microsoft® Visual C++ can be found in the directory ‘VC++’ on the companion CD. The programs in the ‘Win98’ subdirectory can be run under Windows98 without the need of a device driver. The programs in the ‘Windows’ subdirectory can be run under Windows NT/2000/XP with the use of WinIO, which will act as the driver. These programs have been modified to enable them to access the ports through the use of WinIO. WinIO has not been included in the accompanying CD. Its latest version can be downloaded from http://www.internals.com/. You must first install WinIO in order to be able to run the programs in the ‘Windows’ subdirectory. The readers of this book who use WinIO are bound by the WinIO licensing agreement published on the web. Graphics programs, keyboard control programs and PC timer related programs are not available to run under Microsoft® Windows.