Hutchings / James | Interfacing with C | E-Book | sack.de
E-Book

E-Book, Englisch, 302 Seiten

Hutchings / James Interfacing with C


2. Auflage 2000
ISBN: 978-0-08-057435-6
Verlag: Elsevier Science & Techn.
Format: EPUB
Kopierschutz: Adobe DRM (»Systemvoraussetzungen)

E-Book, Englisch, 302 Seiten

ISBN: 978-0-08-057435-6
Verlag: Elsevier Science & Techn.
Format: EPUB
Kopierschutz: Adobe DRM (»Systemvoraussetzungen)



Interfacing with C is about interfacing personal computers using C. Anyone who is interested in ports, transducer interfacing, analog to digital conversion, convolution, filters or digital/analog conversion will benefit from reading Interfacing with C. Students will also find this a practical introduction to real-time programming with a generous collection of tried and tested programs.The pace of the book is such that the reader is encouraged to run the programs and experiment with C. The principles precede the applications in most cases in an attempt to provide genuine understanding and encourage further development. Readers will gain much from the hands-on experience the authors' approach provides, an approach designed to enable readers to climb steep learning curves with the minimum amount of assistance. The many programs included in the text provide the essential hands-on experience. Some of the programs inevitably become rather lengthy, so the source code used is available as a free download from the Newnes website. The aim of the book, however, is to give the reader enough confidence to rewrite and improve these programs.In the second edition Mike James has thoroughly updated all aspects relating to software, operating systems and graphical interfaces. He has also increased the scope of the book to include current forms of C++. Material on data acquisition has been thoroughly updated and the section on peripherals increased. - A practical and painless way of becoming an expert C programmer - New edition also covers C++ and the Windows environment - Get up to speed with the essential maths needed for C without having to buy a university maths text!

Mike James is an established author and experienced FE lecturer. He currently works for Westland. His latest Newnes titles are Microcontroller Cookbook and Higher Electronics.

Hutchings / James Interfacing with C jetzt bestellen!

Zielgruppe


Academic/professional/technical: Research and professional

Weitere Infos & Material


1;Front Cover;1
2;Interfacing with C;4
3;Copyright Page;5
4;Table of Contents;6
5;Preface;8
6;Preface to second edition;10
7;Chapter 1. An introduction to C;12
7.1;Properties and background;12
7.2;Fundamental interfacing;14
7.3;Programmable input–output devices ;15
7.4;8255 programmable peripheral interface;15
7.5;Programming the 8255;18
7.6;IBM PC bus;19
7.7;Accessing specific memory locations with C;20
7.8;Reading the contents of I/O space using pointers;22
7.9;C program development;24
7.10;Variables;28
7.11;Data type;28
7.12;Qualifiers;29
7.13;Reading the status of an input port;29
7.14;Controlling printf();31
7.15;The coercion operator: cast;32
7.16;Writing from keyboard to output port;32
7.17;Entering data using scanf();32
7.18;Further reading;35
8;Chapter 2. Loops and data conversion;37
8.1;Repetition: unconditional jumps;37
8.2;Named constants # define
;37
8.3;Infamous goto;39
8.4;For-loops in greater detail – in a binary counter
;40
8.5;Port monitoring with a do-while construction;41
8.6;Light chaser effect;44
8.7;Communicating with peripherals;44
8.8;A-to-D systems and circuits;46
8.9;Successive approximation;47
8.10;Flash conversion;48
8.11;Dual and quad slope conversion;49
8.12;Synchronization and software control;50
8.13;12-bit A-to-D conversion;51
8.14;IBM-PC AD574A interface;52
9;Chapter 3. Data acquisition using C;57
9.1;Data capture using CGA graphics;57
9.2;Controlling the coordinate;57
9.3;Data capture with EGA graphics;59
9.4;Background and foreground colour;60
9.5;Building a Windows Application;60
9.6;Transducer interfacing with C/C++;65
9.7;Conditioning the signal voltage;66
9.8;Simplifying program development;67
9.9;Displaying temperature using EGA graphics;69
9.10;Trying fancier software;70
9.11;Introducing random noise;71
9.12;A linear transducer;72
9.13;Measurement of Iight intensity;73
9.14;Statement of the problem;74
9.15;Further reading;77
10;Chapter 4. Essential mathematics;78
10.1;How many snapshots?;78
10.2;Mathematical modelling
;80
10.3;Laplace transforms;81
10.4;Electronic calculus;82
10.5;Modelling A-to-D conversion;84
10.6;Zero-order sample-and-hold;86
10.7;z-notation – the algebra of sampled data systems;88
10.8;Further reading;91
11;Chapter 5. Convolution;92
11.1;Matching analogue signals and systems;93
11.2;Frequency-shifting rule;95
11.3;Convolution;95
11.4;Graphical interpretation of the convolution integral;96
11.5;Obtaining the digital output recursively;98
11.6;Digital feedback;98
11.7;Obtaining the digital output by convolution;100
11.8;Reconciling time-domain and frequency-domain processes
;102
11.9;Computerized convolution;103
11.10;System response testing;104
11.11;Applying the program;105
11.12;Sophisticated applications;108
11.13;Further reading;110
12;Chapter 6. Digital filters;111
12.1;Peripheral hardware requirements;111
12.2;Conversion of sinusoidal signals;113
12.3;Analogue and digital filters;115
12.4;Poles and zeros;122
12.5;Band-pass digital filters;125
12.6;Real-time band-pass filter;128
12.7;The bilinear transform;132
12.8;Further reading;144
13;Chapter 7. Fourier transforms;145
13.1;Fourier transforms using a PC;145
13.2;Discrete Fourier transform DFT
;145
13.3;Developing and explaining the program
;148
13.4;Graphical spectral analysis;149
13.5;Understanding the logical system of coordinates;150
13.6;Applying the DFT program
;150
13.7;Streamlining the arithmetic using the fast Fourier transform
;153
13.8;How many multiplications?;153
13.9;FFT with real-time data capture and graphics;161
13.10;Control of the peripheral board and data capture;163
13.11;Signal truncation and spectral leakage;164
13.12;Reducing spectral spreading with a Hanning window;166
13.13;Further reading;167
14;Chapter 8. Correlation;168
14.1;Correlation methods using a PC;168
14.2;Linear systems and random inputs;168
14.3;Wiener-Khintchine theorem;171
14.4;Autocorrelation;172
14.5;Effect of finite observation time;173
14.6;Calculating discrete autocorrelation functions;173
14.7;Computing the autocorrelation function;174
14.8;Anatomy of the program;174
14.9;Graphical autocorrelation;176
14.10;Applying the program;176
14.11;ACF of sampled sine wave;176
14.12;ACF of decaying exponential;177
14.13;White noise;179
14.14;Generating random noise and computing the ACF;181
14.15;Anatomy of the program;181
14.16;Using autocorrelation to detect noise-corrupted signals;183
14.17;ACF with real-time data capture and graphics;184
14.18;Autocorrelation in the real world;185
14.19;The cross-correlation function;185
14.20;System testing and characterization using random noise;188
14.21;Order out of chaos;190
14.22;Further reading;193
15;Chapter 9. Kalman fiIters;194
15.1;Kalman filters – predicting uncertainty;194
15.2;Characterizing stochastic noise – Noddies' guide to statistics;196
15.3;Tracking a time-varying signal in the presence of noise – optimal estimation
;199
15.4;Applied optimal control;200
15.5;Real-time Kalman filter;201
15.6;Simplifying the design – the steady-state Kalman filter;203
15.7;Appendices;203
15.8;Further reading;212
16;Chapter 10. Data conversion;213
16.1;Digital-to-analogue conversion
;213
16.2;A primitive digitally controlled potentiometer;215
16.3;Buffering a binary-weighted resistor network;215
16.4;R-2R ladder network;216
16.5;Analog devices AD7226 D-to-A converter;218
16.6;Waveform generation using a D-to-A and C;219
16.7;Generating sine waves –
real-time and playback digital synthesis;220
16.8;Waveform synthesis;220
16.9;Software-based random-noise generator;222
16.10;Digital transfer functions and waveform modification;223
16.11;Generating echo and reverberation;227
16.12;Historical records and circular buffers;232
16.13;Anti-aliasing filters and filtering the D-to-A output;233
16.14;Quantization noise;234
16.15;Simulating audio reverberation;234
16.16;Special audio effects;236
16.17;Further reading;238
17;Chapter 11. Investigati ng the spectral and time-domain performance of z-transforms using computer-managed instruction;239
17.1;Characterizing the frequency-selective properties of the rational function;240
17.2;A description of the frequency-selective properties of zeros;249
17.3;Programs listed in Appendix 2;256
17.4;Further reading;257
18;Chapter 12. Introduction to digital signal processing
;258
18.1;Analog Devices;259
18.2;Software;263
18.3;Fixed point arithmetic;265
18.4;Application specific;265
19;Chapter 13. Standard programming structures;267
19.1;Introduction;267
19.2;Modular design;267
19.3;The internal structure of modules;268
19.4;Standard programming structures;269
19.5;Eight different ways to convert temperature in Celsius to Fahrenheit using C-based code;274
19.6;The sequence construct;274
19.7;Using the pre-processor directive;275
19.8;The repetition construct;276
19.9;Branching and looping;277
19.10;Writing functions in C;278
19.11;Commentary;278
19.12;Using purpose-built I/O functions – Peeking and Poking;279
19.13;Using the functions peek() and peekb();280
19.14;Using the functions poke() and pokeb();283
19.15;Port-mapped data transfer;283
19.16;Input circuit;284
19.17;Output circuit;284
19.18;Computer part of the problem;285
19.19;Microsoft C I/O calls;286
19.20;Further reading;289
20;Appendix 1;290
21;Appendix 2: Listings of programs described in Chapter 11;291
21.1;Listing 11.1 Graphical amplitude-ratio and phase response of rational z-function;291
21.2;Listing 11.2 Graphical convolution;295
21.3;Listing 11.3 Numerical amplitude-ratio and phase response of rational z-function;297
21.4;Listing 11.4 Numerical convolution;299
22;Glossary;302
23;Index;305


2 Loops and data conversion
Repetition: unconditional jumps
Repetition occurs prominently in many programming applications; C provides a number of particularly attractive and elegant constructions. In Figure 2.1, we require the main body of the program to be repeated indefinitely. One possible construction which avoids using the infamous ‘goto’ is: This is a for( ) loop which has no initial condition, no terminating condition and no action which is applied on each iteration of the loop. Since there is no terminating condition, it does not ever terminate; it is a forever loop. Figure 2.1 A simple flow diagram to light an LED The program in Listing 2.1 is designed to read a number from the keyboard, to output the binary value to leds attached to port A and to read/display the contents of port A. Repetition is illustrated by the flowchart Figure 2.1. Alongside this is a construction called Structured English. It is an alternative way of describing a problem to be committed to software. More is explained about it on page 262. It is a descriptive structure which does not require graphic symbols and yet has a very good correlation to the code which it describes. The indentation is used to help visually align code under control of the LOOP construction. Listing 2.1a: C Connecting the data display circuit Figure 1.7 demonstrates the effect of the program. Alternatively this program construction successfully controls the speed of a small d.c. motor. A suitable circuit is shown in Figure 2.2. Figure 2.2 Hardware for computerized speed control is simple: most of the work is done by the software Integers 0 to 15, input from the keyboard, are processed through the 4-bit D-to-A and power amplifier to provide 16 unique speeds. This simple uni-directional open-loop control system provides the basis for more advanced closed-loop control. Named constants #define
Listing 2.2 generates a square wave of approximately 5 V p–p at a frequency of 500 kHz, by causing PA0 to go repeatedly high, then low. As a programming exercise it demonstrates how to replace names for constants, improving the readability of the program. Most C/C++ programmers use upper-case letters when naming a #define constant: this helps to distinguish variables from defined constants. Notice that the constants are defined outside the main program and not terminated with a semicolon. Listing 2.1b: C++ Infamous goto
The primitive, repetitive nature of this program provides an excuse to demonstrate an unconditional jump; using the goto statement. Many of the erudite books written about C/C++ insist that it should be avoided, or used with extreme caution. This is good advice, since its use encourages unstructured programming and makes programs difficult to read. Not wishing to be contentious, we show how the structured infinite for-loop can be replaced by the unstructured goto, without loss of readability (in this example). When the goto statement is executed, control is transferred to the C/C++ code following the label (in this case start). The label is always terminated by a colon. The goto is generally considered to be a no-no for serious programmers, because it can lead to ‘spaghetti code’ which is very difficult to follow and debug. Many professional software houses forbid its use in any high level language programs. Having said that, it is the main method of transferring program control in assembly language programs. These are only included in small doses within a C/C++ program, usually to speed up a critical part of its operation. There is never an application in C/C++ which cannot be programmed without a goto. Sometimes, its use does make the resultant code more compact, but if its use is allowed, then use it cautiously and sparingly! For-loops in greater detail – in a binary counter
Educationally, Listing 2.3 is particularly rewarding, because it demonstrates a number of different loop constructions in a single program. Connecting the data indication circuit (Figure 1.7) to port A configured as an output provides a binary representation of the current status of the count. The denary status is displayed on the monitor using the printf and cout functions. Counting from 0 to 255 is achieved using the post increment operator (i++), a versatile construction meaning i = i+1 (this could also be written using the C/C++ convention i + = 1). Listing 2.2 Inspection of the structure shows the program consists of a number of nested loops; the outer loop ensures that the procedure is continued indefinitely. The actual count is controlled by the construction The terse nature of the expression: i++ < = 255 deserves further explanation. It means test if ‘i’ is less than or equal to 255 and regardless of the result, increase i by one. Two useful features of C/C++ are the increment and decrement operators, meaning i = i + 1 and i = i - 1. The versatility of C allows you to test the value of ‘i’ and then add 1 to it – post increment operator i + +. Or add 1 and then test, the pre-increment operator + +i. A similar construction is provided for decrementing. To produce an observable display it was necessary to introduce a significant time delay into the loop. This is exploited to demonstrate the ‘for’ construction in greater detail. The for-loop construction is particularly elegant in C, succinctly combining the three loop parameters (initialize, test and increment) in a single bracketed term. Expression 1 initializes the count. Expression 2 indicates the condition for the count to continue and expression 3 denotes the action which must be taken before the loop repeats. Referring to my example program, notice that k is incremented after the main body of the for-loop has been executed. Incidentally, the use of i, j and k as loop counters was introduced as early as the 1960s when the FORTRAN programming language reserved variables as integers which started with these letters. Port monitoring with a do-while construction
In this example (Listing 2.4) the program monitors the switches on port A and displays the decimal value on the screen, before latching the binary value to leds on port B. Including the conditional do-while statement ensures the body of instructions are always executed at least once, before the test is made. Thereupon, looping continues until the result of the test is false. Observe how the test monitors the input port and compares the current contents (called new_contents in the program) with the old_contents. When this expression is false (the double equal signs = = mean ‘of the same value’ in a comparison) control transfers to the instruction following the while-statement. Listing 2.3 Figure 2.3 For-loop flowchart Figure 2.4 Flowchart for Listing 2.4 The versatility of C provides an alternative while-do construction, which tests an expression and if true, permits looping to continue until the result of the test is false, at which point control passes to the next instruction in the program. These structured flowchart constructions are shown in Figure 2.5. Figure 2.5 The anatomy of structured control Be careful with the use of = and = =. The C/C++ expression as shown in Listing 2.4 compares the values of variables new_contents and old_contents. If these are the same, then the statement(s) which follow it will be executed. Compare this with This is a perfectly valid C/C++ expression. However, it allocates to the variable new_contents the value held in a variable called old_contents. Since new_contents and old_contents are now the same, the while statement then evaluates the contents of the bracketed term as zero. As was stated in Chapter 1, a zero is FALSE, so the statements which follow the while expression will never be executed. Light chaser effect
This program Listing 2.5 is designed to produce the effect of a light running repeatedly across the leds connected to port A, which is conditioned as an output. It works by taking powers of 2 and writing the byte to the port. Don’t imagine this is frivolous; it does have a serious side, demonstrating how to write your own functions using C. Observe that the structure is composed of the main function that controls the flow of the program, together with an external function called power(x,n) that can be invoked and executed by a single statement in the main program. The new line int power(int x, int n); is called a function prototype. All functions in the program must be declared prior to the main( ) function. This is so that the compiler knows what functions to expect and what types of data will be passed in and out. A function declaration has the...



Ihre Fragen, Wünsche oder Anmerkungen
Vorname*
Nachname*
Ihre E-Mail-Adresse*
Kundennr.
Ihre Nachricht*
Lediglich mit * gekennzeichnete Felder sind Pflichtfelder.
Wenn Sie die im Kontaktformular eingegebenen Daten durch Klick auf den nachfolgenden Button übersenden, erklären Sie sich damit einverstanden, dass wir Ihr Angaben für die Beantwortung Ihrer Anfrage verwenden. Selbstverständlich werden Ihre Daten vertraulich behandelt und nicht an Dritte weitergegeben. Sie können der Verwendung Ihrer Daten jederzeit widersprechen. Das Datenhandling bei Sack Fachmedien erklären wir Ihnen in unserer Datenschutzerklärung.