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

4.3.7 Miscellaneous Library

4.3.7 Miscellaneous Library

The functions in the Miscellaneous library include routines to convert data from one type to another type, as well as to perform some trigonometric functions. Table 4.10 lists the commonly used functions in this library.

Table 4.10: Commonly used Miscellaneous library functions

Function Description
ByteToStr Convert a byte into string
ShortToStr Convert a short into string
WordToStr Convert an unsigned word into string
IntToStr Convert an integer into string
LongToStr Convert a long into string
FloatToStr Convert a float into string
Bcd2Dec Convert a BCD number into decimal
Dec2Bcd Convert a decimal number into BCD

The following general programs illustrate the use of various library routines available with the mikroC language.

Example 4.18

Write a function to convert the string pointed to by p into lowercase or uppercase, depending on the value of a mode parameter passed to the function. If the mode parameter is nonzero, then convert to lowercase, otherwise convert it to uppercase. The function should return a pointer to the converted string.

Solution 4.18

The required program listing is given in Figure 4.29 (program CASE.C). The program checks the value of the mode parameter, and if this parameter is nonzero the string is converted to lowercase by calling function ToLower, otherwise the function ToUpper is called to convert the string to uppercase. The program returns a pointer to the converted string.

/*********************************************************************
                CONVERT A STRING TO LOWER/UPPERCASE
               =====================================
This program receives a string pointer and a mode parameter. If the mode is 1
then the string is converted to lowercase, otherwise the string is converted
to uppercase.
Programmer: Dogan Ibrahim
File:       CASE.C
Date:       May, 2007
***********************************************************************/
unsigned char *Str_Convert(unsigned char *p, unsigned char mode) {
 unsigned char *ptr = p;
 if (mode != 0) {
  while (*p != '') *p++ = ToLower(*p);
 } else {
  while (*p != '') *p++ = ToUpper(*p);
 }
 return ptr;
}


Figure 4.29: Program for Example 4.18

Example 4.19

Write a program to define a complex number structure, then write functions to add and subtract two complex numbers. Show how you can use these functions in a main program.

Solution 4.19

Figure 4.30 shows the required program listing (program COMPLEX.C). At the beginning of the program, a data type called complex is created as a structure having a real part and an imaginary part. A function called Add is then defined to add two complex numbers and return the sum as a complex number. Similarly, the function Subtract is defined to subtract two complex numbers and return the result as a complex number. The main program uses two complex numbers, a and b, where,

a = 2.0 – 3.0j

b = 2.5 + 2.0j

/*************************************************************
           COMPLEX NUMBER ADDITION AND SUBTRACTION
         ===========================================
This program creates a data structure called complex having a real part and
an imaginary part. Then, functions are defined to add or subtract two complex
numbers and store the result in another complex number.
The first complex number is,  a = 2.0 – 2.0j
The second complex number is, b = 2.5 + 2.0j
The program calculates, c = a + b
and,                    d = a ? b
Programmer: Dogan Ibrahim
File:       COMPLEX.C
Date:       May, 2007
***************************************************************/
/* Define a new data type called complex */
typedef struct {
 float real;
 float imag;
} complex;
/* Define a function to add two complex numbers and return the result as
 a complex number */
complex Add(complex i, complex j) {
 complex z;
 z.real = i.real + j.real;
 z.imag = i.imag + j.imag;
 return z;
}
/* Define a function to subtract two complex numbers and return the result as
 a complex number */
complex Subtract(complex i, complex j) {
 complex z;
 z.real = i.real - j.real;
 z.imag = i.imag - j.imag;
 return z;
}
/* Main program */
void main() {
 complex a, b, c, d;
 a.real = 2.0; a.imag =?3.0; // First complex number
 b.real = 2.5; b.imag = 2.0; // second complex number
 c = Add(a, b);              // Add numbers
 d = Subtract(a, b);         // Subtract numbers
}


Figure 4.30: Program for Example 4.19

Two other complex numbers, c and d, are also declared, and the following complex number operations are performed:

The program calculates, c = a + b and, d = a – b

Example 4.20

A projectile is fired at an angle of y degrees at an initial velocity of v meters per second. The distance traveled by the projectile (d), the flight time (t), and the maximum height reached (h) are given by the following formulas:

 

Write functions to calculate the height, flight time, and distance traveled. Assuming that g=9.81m/s?, v=12 m/s, and ?=45°, call the functions to calculate the three variables. Figure 4.31 shows the projectile pattern.


Figure 4.31: Projectile pattern

Solution 4.20

The required program is given in Figure 4.32 (program PROJECTILE.C). Three functions are defined: Height calculates the maximum height of the projectile, Flight_time calculates the flight time, and Distance calculates the distance traveled. In addition, a function called Radians converts degrees into radians to use in the trigonometric function sine. The height, distance traveled, and flight time are calculated and stored in floating point variables h, d, and t respectively.

/**********************************************************************
                            PROJECTILE CALCULATION
                           ========================
This program calculates the maximum height, distance traveled, and the flight
time of a projectile. Theta is the firing angle, and v is the initial
velocity of the projectile respectively.
Programmer: Dogan Ibrahim
File:       PROJECTILE.C
Date:       May, 2007
***********************************************************************/
#define gravity 9.81
/* This function converts degrees to radians */
float Radians(float y) {
 float rad;
 rad = y * 3.14159 / 180.0;
 return rad;
}
/* Flight time of the projectile */
float Flight_time(float theta, float v) {
 float t, rad;
 rad = Radians(theta);
 t = (2.0*v*sin(rad)) / gravity;
 return t;
}
float Height(float theta, float v) {
 float h, rad;
 rad = Radians(theta);
 h = (v*v*sin(rad)) / gravity;
 return h;
}
float Distance(float theta, float v) {
 float d, rad;
 rad = radians(theta);
 d = (v*v*sin(2*rad)) / gravity;
 return d;
}
/* Main program */
void main() {
 float theta, v, h, d, t;
 theta = 45.0;
 v = 12.0;
 h = Height(theta, v);
 d = Distance(theta, v);
 t = Flight_time(theta, v);
}


Figure 4.32: Program for Example 4.20

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


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