ebook img

Interfacing with C++ : programming real-world applications PDF

490 Pages·2006·2.324 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Interfacing with C++ : programming real-world applications

Jayantha Katupitiya Kim Bentley Interfacing with C++ Programming Real-World Applications ABC Dr.JayanthaKatupitiya SeniorLecturer SchoolofMechanicaland ManufacturingEngineering TheUniversityofNewSouthWales SydneyNSW2052,Australia Email:[email protected] Mr.KimBentley LibraryofCongressControlNumber:2005937895 ISBN-10 3-540-25378-5SpringerBerlinHeidelbergNewYork ISBN-13 978-3-540-25378-5SpringerBerlinHeidelbergNewYork Thisworkissubjecttocopyright.Allrightsarereserved,whetherthewholeorpartofthematerialisconcerned, specificallytherightsoftranslation,reprinting,reuseofillustrations,recitation,broadcasting,reproductiononmi- crofilmorinanyotherway,andstorageindatabanks.Duplicationofthispublicationorpartsthereofispermitted onlyundertheprovisionsoftheGermanCopyrightLawofSeptember9,1965,initscurrentversion,andpermission forusemustalwaysbeobtainedfromSpringer.ViolationsareliableforprosecutionundertheGermanCopyright Law. SpringerisapartofSpringerScience+BusinessMedia springer.com (cid:1)c Springer-VerlagBerlinHeidelberg2006 PrintedinTheNetherlands Theuseofgeneraldescriptivenames,registerednames,trademarks,etc.inthispublicationdoesnotimply,evenin theabsenceofaspecificstatement,thatsuchnamesareexemptfromtherelevantprotectivelawsandregulationsand thereforefreeforgeneraluse. Typesetting:bytheauthorsandTechBooksusingaSpringerLATEXmacropackage Coverdesign:design&productionGmbH,Heidelberg Printedonacid-freepaper SPIN:11015543 89/TechBooks 543210 Table of Contents 1 GETTING STARTED....................................................................................1 1.1 INTRODUCTION................................................................................................2 1.2 PROGRAMDEVELOPMENTSOFTWARE............................................................2 1.3 A C++ PROGRAM............................................................................................6 1.4 USE OF FUNCTIONS.......................................................................................10 1.5 FUNDAMENTALDATATYPES........................................................................15 1.6 FUNCTIONS WITH PARAMETERS AND RETURNVALUES...............................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 PARALLELPORT?....................................................................24 2.3 DATAREPRESENTATION...............................................................................30 2.4 PROGRAMDEMONSTRATINGHEXADECIMAL TO DECIMAL.....................32 2.5 SUMMARY.....................................................................................................33 2.6 BIBLIOGRAPHY..............................................................................................33 3 TESTING THE PARALLEL PORT...........................................................35 3.1 INTRODUCTION..............................................................................................36 3.2 INTERFACEBOARDPOWERSUPPLY..............................................................36 3.3 PARALLELPORTINTERFACE.........................................................................39 3.4 BASICOUTPUTUSING THE PARALLELPORT................................................43 3.5 BASICINPUTUSING THE PARALLELPORT....................................................46 3.6 COMPENSATING FOR INTERNALINVERSIONS................................................50 3.7 SUMMARY.....................................................................................................55 3.8 BIBLIOGRAPHY..............................................................................................56 4 THE OBJECT-ORIENTED APPROACH.................................................57 4.1 INTRODUCTION..............................................................................................58 4.2 CONCEPTUAL AND PHYSICALLYREALISABLEOBJECTS...............................58 4.3 REAL OBJECTS..............................................................................................59 4.4 OBJECTCLASSES...........................................................................................61 4.5 ENCAPSULATION...........................................................................................63 4.6 ABSTRACTCLASSES......................................................................................64 4.7 CLASSHIERARCHIES.....................................................................................64 4.8 INHERITANCE................................................................................................65 4.9 MULTIPLEINHERITANCE...............................................................................66 4.10 POLYMORPHISM..........................................................................................66 4.11 ANEXAMPLEOBJECTHIERARCHY.............................................................67 4.12 ADVANTAGES OF OBJECT-ORIENTEDPROGRAMMING...............................72 4.13 DISADVANTAGES OF OBJECT-ORIENTEDPROGRAMMING..........................72 4.14 SUMMARY...................................................................................................73 VI TABLE OF CONTENTS 4.15 BIBLIOGRAPHY............................................................................................73 5 OBJECT-ORIENTED PROGRAMMING.................................................75 5.1 INTRODUCTION..............................................................................................76 5.2 NAMINGCONVENTION..................................................................................76 5.3 DEVELOPING AN OBJECTCLASS...................................................................77 5.4 PARALLELPORTCLASS– STAGEI................................................................82 5.5 USINGCLASSOBJECTS IN PROGRAMS..........................................................87 5.6 PARALLELPORTCLASS– STAGEII..............................................................94 5.7 PARALLELPORTCLASS– STAGEIII.............................................................99 5.8 SUMMARY...................................................................................................103 5.9 BIBLIOGRAPHY............................................................................................103 6 DIGITAL-TO-ANALOG CONVERSION...............................................105 6.1 INTRODUCTION............................................................................................106 6.2 DIGITAL-TO-ANALOGCONVERSION...........................................................106 6.3 PROGRAMMING THE DIGITAL-TO-ANALOG CONVERTER...........................117 6.4 DERIVATION OF OBJECTCLASSES..............................................................121 6.5 ADDINGMEMBERS TO DERIVEDCLASSES..................................................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 USINGPOINTERS.........................................................................................175 7.7 MACROS......................................................................................................184 7.8 DYNAMICMEMORYALLOCATION..............................................................185 7.9 EXCEPTIONHANDLING...............................................................................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 STEPPERMOTORS.......................................................................................202 8.4 A CLASSHIERARCHY FOR MOTORS...........................................................211 8.5 VIRTUALFUNCTIONS– ANINTRODUCTION...............................................212 8.6 VIRTUALFUNCTIONS- 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 EFFICIENTCODINGTECHNIQUES................................................................274 9.3 MODULAR PROGRAMS................................................................................282 9.4 CASESTUDY- MOTORDRIVERPROGRAM.................................................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 TEMPERATUREMEASUREMENT................................................................305 10.4 THEOBJECTCLASSVCO...........................................................................306 10.5 MEASURINGVOLTAGESUSING THE VCO................................................311 10.6 GRAPHICSPROGRAMMING– SQUAREWAVEDISPLAY............................318 10.7 TEMPERATUREMEASUREMENT................................................................324 10.8 SUMMARY.................................................................................................328 10.9 BIBLIOGRAPHY..........................................................................................329 11 ANALOG-TO-DIGITAL CONVERSION................................................331 11.1 INTRODUCTION..........................................................................................332 11.2 ANALOG-TO-DIGITALCONVERSION.........................................................332 11.3 CONVERSION TECHNIQUES.......................................................................334 11.4 MEASURINGVOLTAGES WITH AN ADC....................................................341 11.5 ANOBJECTCLASS FOR THE ADC.............................................................347 11.6 MEASURINGVOLTAGEUSING THE ADC..................................................356 11.7 MEASURINGTEMPERATUREUSING THE ADC..........................................359 11.8 SUMMARY.................................................................................................362 11.9 BIBLIOGRAPHY..........................................................................................362 12 DATA ACQUISITION WITH OPERATOR OVERLOADING............363 12.1 INTRODUCTION..........................................................................................364 12.2 OPERATOROVERLOADING........................................................................364 12.3 DATAACQUISITION...................................................................................393 12.4 SUMMARY.................................................................................................397 12.5 BIBLIOGRAPHY..........................................................................................397 13 THE PC TIMER..........................................................................................399 13.1 INTRODUCTION..........................................................................................400 13.2 PC TIMERSYSTEM....................................................................................400 13.3 PROGRAMMING THE TIMER.......................................................................408 13.4 THEOBJECTCLASSPCTIMER...................................................................409 13.5 MEASUREMENT OF TIME...........................................................................415 13.6 REFLEXMEASUREMENT...........................................................................417 13.7 GENERATING A TIME-BASE......................................................................419 13.8 DATAACQUISITION WITH TIMESTAMP.....................................................423 13.9 SUMMARY.................................................................................................430 13.10 BIBLIOGRAPHY........................................................................................430 VIII TABLE OF CONTENTS APPENDIX A - HARDWARE...........................................................................431 CIRCUITCONSTRUCTION..................................................................................432 INTERFACEBOARDBILL OF MATERIALS..........................................................476 APPENDIX B - SOFTWARE............................................................................479 C++ KEYWORDS...............................................................................................480 OPERATORPRECEDENCE..................................................................................481 ASCII CHARACTERSET....................................................................................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: (cid:120)(cid:3) Digital Input and Output. (cid:120)(cid:3) Analog-to-Digital Conversion and Digital-to-Analog Conversion. (cid:120)(cid:3) DC Motor and Stepper Motor Control. (cid:120)(cid:3) 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 low- level 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 wil l 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. 1 Getting Started Inside this Chapter (cid:120)(cid:3) Developing programs – what is involved? (cid:120)(cid:3) Writing and running your first C++ program. (cid:120)(cid:3) Program syntax. (cid:120)(cid:3) Functions. (cid:120)(cid:3) Fundamental data types.

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.