Êíèãà: Advanced PIC Microcontroller Projects in C

4.1 mikroC Functions

A function is a self-contained section of code written to perform a specifically defined action. Functions are usually created when a single operation must be performed in different parts of the main program. It is, moreover, good programming practice to divide a large program into a number of smaller, independent functions. The statements within a function are executed by calling (or invoking) the function.

The general syntax of a function definition is shown in Figure 4.1. The data type indicates the type of data returned by the function. This is followed by the name of the function and then a set of parentheses, within which the arguments, separated by commas, are declared. The body of the function, which includes the function’s operational code, is written inside a set of curly brackets.

type name (parameter1, parameter2, ……) {
 ……………
 function body
 ……………
}


Figure 4.1: General syntax of a function definition

In the sample function definition that follows, the function, named Mult, receives two integer arguments, a and b, and returns their product. Note that using parentheses in a return statement is optional:

int Mult(int a, int b) {
 return (a*b);
}

When a function is called, it generally expects to be given the number of arguments expressed in the function’s argument list. For example, the preceding function can be called as:

z = Mult(x, y);

where variable z has the data type int. Note that the arguments declared in the function definition and the arguments passed when the function is called are independent of each other, even if they have the same name. In the preceding example, when the function is called, variable x is copied to a and variable y is copied to b on entry into function Mult.

Some functions do not return any data. The data type of such functions must be declared as void. For example:

void LED(unsigned char D) {
 PORTB = D;
}

void functions can be called without any assignment statements, but the parentheses are needed to tell the compiler that a function call is made:

LED();

Also, some functions do not have any arguments. In the following example, the function, named Compl, complements PORTC of the microcontroller. It returns no data and has no arguments:

void Compl() {
 PORTC = ~PORTC;
}

This function can be called as:

Compl();

Functions are normally defined before the start of the main program.

Some function definitions and their use in main programs are illustrated in the following examples:

Example 4.1

Write a function called Circle_Area to calculate the area of a circle where the radius is to be used as an argument. Use this function in a main program to calculate the area of a circle whose radius is 2.5cm. Store the area in a variable called Circ.

Solution 4.1

The data type of the function is declared as float. The area of a circle is calculated by the formula:

Area = ?r?

where r is the radius of the circle. The area is calculated and stored in a local variable called s, which is then returned from the function:

float Circle_Area(float radius) {
 float s;
 s = PI * radius * radius;
 return s;
}

Figure 4.2 shows how the function Circle_Area can be used in a main program to calculate the area of a circle whose radius is 2.5cm. The function is defined before the main program. Inside the main program the function is called to calculate and store the area in variable Circ.

/******************************************************************
                         AREA OF A CIRCLE
                         ================
This program calls to function Circle_Area to calculate the area of a circle.
Programmer: Dogan Ibrahim
File:       CIRCLE.C
Date:       May, 2007
********************************************************************
/* This function calculates the area of a circle given the radius */
float Circle_Area(float radius) {
 float s;
 s = PI * radius * radius;
 return s;
}
/* Start of main program. Calculate the area of a circle where radius = 2.5 */
void main() {
 float r, Circ;
 r = 2.5;
 Circ = Circle_Area(r);
}


Figure 4.2: Program to calculate the area of a circle

Example 4.2

Write a function called Area and a function called Volume to calculate the area and volume of a cylinder respectively. Then write a main program to calculate the area and the volume of cylinder whose radius is 2.0cm and height is 5.0cm. Store the area in variable cyl_area and the volume in variable cyl_volume.

Solution 4.2

The area of a cylinder is calculated by the formula:

Area = 2?rh

where r and h are the radius and height of the cylinder. The volume of a cylinder is calculated by the formula:

Volume = ?r?h

Figure 4.3 shows the functions that calculate the area and volume of a cylinder. The main program that calculates the area and volume of a cylinder whose radius=2.0cm and height=5.0cm is shown in Figure 4.4.

float Area(float radius, float height) {
 float s;
 s = 2.0*PI * radius*height;
 return s;
}
float Volume(float radius, float height) {
 float s;
 s = PI *radius*radius*height;
 return s;
}


Figure 4.3: Functions to calculate cylinder area and volume

/*************************************************************************
                      AREA AND VOLUME OF A CYLINDER
                     ===============================
This program calculates the area and volume of a cylinder whose radius is
2.0cm and height is 5.0cm.
Programmer: Dogan Ibrahim
File:       CYLINDER.C
Date:       May, 2007
**************************************************************************/
/* Function to calculate the area of a cylinder */
float Area(float radius, float height) {
 float s;
 s = 2.0*PI * radius*height;
 return s;
}
/* Function to calculate the volume of a cylinder */
float Volume(float radius, float height) {
 float s;
 s = PI *radius*radius*height;
 return s;
}
/* Start of the main program */
void main() {
 float r = 2.0, h = 5.0;
 float cyl_area, cyl_volume;
 cyl_area = Area(r, h);
 cyl_volume(r, h);
}


Figure 4.4: Program that calculates the area and volume of a cylinder

Example 4.3

Write a function called LowerToUpper to convert a lowercase character to uppercase.

Solution 4.3

The ASCII value of the first uppercase character (‘A’) is 0x41. Similarly, the ASCII value of the first lowercase character (‘a’) is 0x61. An uppercase character can be converted to its equivalent lowercase by subtracting 0x20 from the character. The required function listing is shown in Figure 4.5.

unsigned char LowerToUpper(unsigned char c) {
 if (c >= 'a' && c <= 'z') return (c - 0x20);
 else return c;
}


Figure 4.5: Function to convert lowercase to uppercase

Example 4.4

Use the function you created in Example 4.3 in a main program to convert letter ‘r’ to uppercase.

Solution 4.4

The required program is shown in Figure 4.6. Function LowerToUpper is called to convert the lowercase character in variable Lc to uppercase and store in Uc.

/********************************************************************
                       LOWERCASE TO UPPERCASE
                     ==========================
This program converts the lowercase character in variable Lc to uppercase
and stores in variable Uc.
Programmer: Dogan Ibrahim
File:       LTOUPPER.C
Date:       May, 2007
*********************************************************************/
/* Function to convert a lower case character to upper case */
unsigned char LowerToUpper(unsigned char c) {
 if (c >= 'a' && c <= 'z') return (c - 0x20);
 else return c;
}
/* Start of main program */
void main() {
 unsigned char Lc, Uc;
 Lc = 'r';
 Uc = LowerToUpper(Lc);
}


Figure 4.6: Program calling function LowerToUpper

Îãëàâëåíèå êíèãè


Ãåíåðàöèÿ: 0.932. Çàïðîñîâ Ê ÁÄ/Cache: 2 / 0
ïîäåëèòüñÿ
Ââåðõ Âíèç