Файл: Interfacing with C plus plus-programing communication with microcontrolers (K. Bentley, 2006).pdf

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 14.06.2025

Просмотров: 3571

Скачиваний: 1

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

1 GETTING STARTED 17

1.The data type

2.The identifier name

Data type

Identifier Name

int a; Semi-colon is a must!

Figure 1-10 An example showing the syntax of a single identifier declaration.

An example of an identifier declaration is:

int a;

The data type is int and the identifier name is a. If needed, more than one identifier can be declared in one statement as shown in Figure 1-11:

Data type

Identifiers, all of type int

int a,b,c;

Semi-colon is a must!

Commas used to separate identifiers

Figure 1-11 An example showing the syntax of a multiple identifier declaration.

Such a declaration must be provided before being able to use an identifier in your program.

An identifier can also be declared and initialised simultaneously. In such a case, in addition to declaring the variable, we also set the identifier to take up an initial value. An example of such a situation that applies to a single identifier is:

int a=0;

The data type is int, variable name is a and it is initialised to have a value of 0 as shown in Figure 1-12.

Data type

Identifier

Assigned value

int a=0;

Semi-colon is a must!

Equal sign is used to assign the initial value

Figure 1-12 An example - syntax of a single identifier declaration and definition.

18 1 GETTING STARTED

The identifier declarations we have seen so far can be combined in any manner. Such a declaration is shown in Figure 1-13:

Data type

Identifiers, all of type int

int a=0,b,c;

Semi-colon is a must!

Commas used to separate identifiers

This identifier is initialised to 0

Figure 1-13 An example showing the syntax of a general identifier declaration.

1.6 Functions with Parameters and Return Values

In Section 1.4.1 we learned how to make a function call with a view to understand the concept of procedure abstraction. In this section we will look at a function that can be called repeatedly to carry out the addition of two numbers. We will program the function to receive the two numbers as parameters and to return their sum as the end result produced by the function. This will help us understand the role of function parameters and their return value.

Listing 1-3 Functions with parameters and return values.

/* This program calls a function (twice) to add two numbers together from within the main function and outputs the result to the screen. */

#include <iostream.h>

float Add (float a, float b) // The Add() function

{

float sum;

sum = a + b; return sum;

}

void main()

// The main function.

{

float p=1, q=2.3, r=3, s=4.5;


1 GETTING STARTED 19

float Sum1, Sum2;

Sum1 = Add(p,q); // First call to ‘Add’ function cout << “First Sum “ << Sum1 << endl;

Sum2 = Add(r,s); // Second call to ‘Add’ function

cout << “Second Sum “ << Sum2 << endl;

}

In the program shown in Listing 1-3 we have defined a new function named Add. As mentioned earlier, the definition of a function provides the return value type, the function name, the list of parameters and their types, and the body of the function. Unlike the function we have seen so far in this book, the Add() function’s pair of parentheses are not empty, meaning the function receives some parameters. In this case the Add() function receives two parameters of type float. Furthermore, the return value type of the Add() function is float. This means the function must produce a return value of type float. The value to be returned must also be specified within the body of the function in a return statement. The Add() function is reproduced below to explain its operation:

float Add (float a, float b)

{

float sum;

sum = a + b; return sum;

}

NOTE

According to the C language, the declaration of the Add() function is:

float Add();

Therefore, a declaration in C does not provide information about the parameters.

The prototype of the Add() function is:

float Add(float, float);

This does provide information about the parameters. In C++, the declaration and

the prototype of a function are exactly the same. Therefore, the prototype (and

declaration) of the function Add() is:

float Add(float, float);

20 1 GETTING STARTED

Within the body of this function we have declared a float type identifier named sum. Then sum is assigned the result of adding a to b. Finally, the return statement sends the value of sum out of the function. Note that the type of the returned value, i.e. the type of sum (which is float), is the same as the return value type of the Add() function (specified on the first line).

The main() function of our program is shown ahead, with its function calls highlighted in bold typeface. In the first call to the Add() function, its parameters or formal arguments a and b are replaced by copies of the actual arguments p and q, which carry real values. The parameters a and b can be viewed as placeholders. In the second call to the Add() function, its parameters are replaced by copies of r and s.

void main()

{

float p=1, q=2.3, r=3, s=4.5; float Sum1, Sum2;

Sum1 = Add(p,q); // First call to ‘Add’ function cout << “First Sum “ << Sum1 << endl;

Sum2 = Add(r,s); // Second call to ‘Add’ function

cout << “Second Sum “ << Sum2 << endl;

}

The value returned by the first call to the Add() function is assigned to Sum1. Therefore, Sum1 becomes the summation of p and q. In our case, Sum1 will have the value of 3.3. Similarly, Sum2 will have the value of 7.5. Since the main() function is making the calls to the Add() function, the main() function becomes the caller and at the same time the recipient of any return values. In this case, the main() function’s body is also known as the calling environment. The other lines in the main function are identifier declarations and/or definitions, and the statement used to print the values of Sum1 and Sum2 on the screen.

Figure 1-14 shows an example of the sequences a program goes through. This complete program consists of a main() function and a number of other functions and data. The program starts from within the main() function where various other functions are called throughout its operation.

The main() function and all the other functions are stored in the so-called code area of program memory, and are generally not expected to change during the life of the program. The data is stored in the data area where its contents are expected to change. Apart from the data in fixed data areas, there may be other data that is created in a temporary area known as the stack, and also in a semi-permanent area known as the heap (or free store).


1 GETTING STARTED 21

Function A

2

(reads Data A)

1

3

Data A

7

Function C

main( )

(reads then

writes Data B)

10

4

9

6

Function B

8

(writes Data B)

Data B

5

Figure 1-14 Program with main()function, other functions and data.

1.7 Summary

Program development software is typically used to write C++ programs. This software provides an integrated environment for editing, compiling and linking programs. Built-in libraries known as Run-Time Libraries are part of the development environment and contain useful functions. To use these functions, header files are included at the start of the program to provide the respective function declarations.

A C++ program comprises the code written using correct syntax, comments, keywords, identifiers, fundamental data types, user-written data types and header files. Keywords are the reserved words that are part of the C++ language. Fundamental data types are built-in data types +and can be used to develop more complex user-written data types. Identifier names are chosen by the programmer and must not be C++ keywords. Both identifiers and functions must be declared ahead of their use in a program.

C++ programs carry out procedures by using functions that operate on specific data. This simplifies programming since the programmer only calls the function to perform a task and does not need to know how the function implements the call (this is procedure abstraction). A special type of function named main starts and ends program execution. Functions can return a value from within their body after carrying out their assigned operations. The type of this data must be specified at the time of defining the function, therefore, the function has what is known as a return


22 1 GETTING STARTED

value type. In addition to this, functions often require input data in order to carry out their dedicated operations. This input data is passed into functions with the use of their function parameters.

Early in this chapter we explained a basic C++ program comprising just the main() function. An additional function was then added to this program to carry out the same task and demonstrate procedure abstraction. Finally, a program was presented and discussed that added two numbers using a function that had parameters and a return value.

1.8 Bibliography

Kelley, A. and I. Pohl, A Book on C – programming in C, Benjamin Cummins, 1995.

House, R., Beginning with C – An Introduction to Professional Programming,

International Thompson Publishing, 1994.

Deitel H.M. and P.J. Deitel C: How to Program, Prentice Hall, 1994.

2

Parallel Port

Basics and

Interfacing

Inside this Chapter

ξ

ξ

ξ

ξ

Parallel port configuration & functionality.

Digital logic fundamentals.

Number systems: decimal, hexadecimal and binary.

Electronics: port, byte, synchronous, asynchronous, addresses.

2.1 Introduction

A basic understanding of digital logic principles and converting data between number systems is needed before the parallel port can be used effectively. This chapter covers these topics and also describes the configuration of the parallel port itself. Concepts such as binary logic, logic levels, input/output address space and the physical connection to the port will be explained.

Working through this chapter will prime you for programming and connecting to the parallel port. You will use this knowledge in future chapters when developing programs to control and monitor hardware through the port. An understanding of basic electronic logic principles is also beneficial when constructing and testing many circuits on the interface board.

2.2 What is the Parallel Port?

Generally speaking, a port is a portion of electronic hardware that is used as an interface to connect with another electronic device for the purpose of information exchange. This connection allows information in the form of data to flow into, out of, or both into and out of the port.

The parallel port has the facility to transfer data both in and out, between the PC and the outside world. It is normally used for sending information to a printer and also known as the printer port. With older computers, the printer port is made up of circuitry residing on a separate printed circuit board (referred to as a pcb) which plugs into the PC motherboard. Newer computers, however, tend to have the parallel port circuitry integrated along with the rest of the PC motherboard.

Having a basic familiarisation with concepts such as logic families, logic levels and noise margins helps to be able to gain an understanding how electronic devices communicate digitally. This understanding will also prove useful should electrical problems arise when using digital circuitry on the interface board.

2.2.1 Digital Logic

As mentioned earlier, computer programs are executed by hardware which operates using binary logic, also known as digital logic. Binary logic has two possible states, ON and OFF. Typically these binary logic states are represented using binary logic notation, where 1’s denote the ON state and 0’s denote the OFF state.

The ON and OFF states used by the parallel port circuitry and many other digital logic circuits are implemented using voltage levels known as logic levels which commonly lie between 0V and +5V. Note that not all types of logic circuits use the same logic voltage levels. These logic circuits are also known as integrated circuits (IC’s), containing groups of circuit elements housed on a single piece of semiconductor material known as a “chip”. The chip is packaged inside either