Выбрать главу

void main() {

 float Radius = 2.5, Height = 10;

 float Volume;

 Volume = PI * Radius * Radius * Height;

}

Example 3.7

Write a program to find the largest element of an integer array having ten elements.

Solution 3.7

At the beginning, variable m is set to the first element of the array. A loop is then formed and the largest element of the array is found:

void main() {

 unsigned char j;

 int m, A[10];

 m = A[0]; // First element of array

 for(j = 1; j < 10; j++) {

  if(A[j] > m) m = A[j];

 }

}

Example 3.8

Write a program using a while statement to clear all ten elements of an integer array M.

Solution 3.8

As shown in the program that follows, NUM is defined as 10 and variable j is used as the loop counter:

#define NUM 10

void main() {

 int M[NUM];

 unsigned char j = 0;

 while (j < NUM) {

  M[j] = 0;

  j++;

 }

}

Example 3.9

Write a program to convert the temperature from °C to °F starting from 0°C, in steps of 1°C up to and including 100°C, and store the results in an array called F.

Solution 3.9

Given the temperature in °C, the equivalent in °F is calculated using the formula:

F = (C – 32.0)/1.8

A for loop is used to calculate the temperature in °F and store in array F:

void main() {

 float F[100];

 unsigned char C;

 for(C = 0; C <= 100; C++) {

  F[C] = (C – 32.0) / 1.8;

 }

}

3.4 Summary

There are many assembly and high-level languages for the PIC18 series of microcontrollers. This book focuses on the mikroC compiler, since it is easy to learn and a free demo version is available that allows users to develop programs as large as 2K in size.

This chapter presented an introduction to the mikroC language. A C program may contain a number of functions and variables plus a main program. The beginning of the main program is indicated by the statement void main().

A variable stores a value used during the computation. All variables in C must be declared before they are used. A variable can be an 8-bit character, a 16-bit integer, a 32-bit long, or a floating point number. Constants are stored in the flash program memory of PIC microcontrollers, so using them avoids using valuable and limited RAM memory.

Various flow control and iteration statements such as if, switch, while, do, break, and so on have been described in the chapter, with examples.

Pointers are used to store the addresses of variables. As we shall see in the next chapter, pointers can be used to pass information back and forth between a function and its calling point. For example, pointers can be used to pass variables between a main program and a function.

3.5 Exercises

1. Write a C program to set bits 0 and 7 of PORTC to logic 1.

2. Write a C program to count down continuously and send the count to PORTB.

3. Write a C program to multiply each element of a ten element array by 2.

4. Write a C program to add two matrices P and Q. Assume that the dimension of each matrix is 3×3 and store the result in another matrix called W.

5. Repeat Exercise 4 but this time multiply matrices P and Q and store the product in matrix R.

6. What do the terms variable and constant mean?

7. What does program repetition mean? Describe the operation of while, do-while, and for loops in C.

8. What is an array? Write example statements to define the following arrays:

 a) An array of ten integers

 b) An array of thirty floats

 c) A two-dimensional array having six rows and ten columns

9. Trace the operation of the following loops. What will be the value of variable z at the end of each loop?

 a)

unsigned char j = 0, z = 0;

while (j < 10) {

 z++;

 j++;

}

 b)

 unsigned char z = 10;

 for (j = 0; j < 10; j++) z--;

10. Given the following variable definitions, list the outcome of the following conditional tests in terms of “true” or “false”:

unsigned int a = 10, b = 2;

if (a > 10)

if (b >= 2)

if(a == 10)

if (a > 0)

11. Write a program to calculate whether a number is odd or even.

12. Determine the value of the following bitwise operations using AND, OR, and EXOR operations:

Operand 1: 00010001

Operand 2: 11110001

13. How many times does each of the following loops iterate, and what is the final value of the variable j in each case?

 a) for(j = 0; j < 5; j++)

 b) for(j = 1; j < 10; j++)

 c) for(j = 0; j <= 10; j++)

 d) for(j = 0; j <= 10; j += 2)

 e) for(j = 10; j > 0; j -= 2)

14. Write a program to calculate the sum of all positive integer numbers from 1 to 100.

15. Write a program to evaluate factorial n, where 0! and 1! evaluate to 1 and n! = n × (n – 1)!

16. Write a program to calculate the average value of the numbers stored in an array. Assume that the array is called M and has twenty elements.

17. Modify the program in Exercise 16 to find the smallest and largest values of the array. Store the smallest value in a variable called Sml and the largest value in a variable called Lrg.

18. Derive equivalent if-else statements for the following tests:

 a) (a > b) ? 0 : 1

 b) (x < y) ? (a > b) : (c > d)

19. Given that f1 and f2 are both floating point variables, explain why the following test expression controlling the while loop may not be safe:

do {

 ...............

 ...............

} while(f1 != f2);

Why would the problem not occur if both f1 and f2 were integers? How would you correct this while loop?

20. What can you say about the following while loop?

k = 0;

Total = 0;

while (k < 10) {

 Sum++;

 Total += Sum;

}

21. What can you say about the following for loop?

Cnt = 0;

for (;;) {

 Cnt++;

}

CHAPTER 4

Functions and Libraries in mikroC