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
4.4 Summary
This chapter has discussed the important topics of functions and libraries. Functions are useful when part of a code must be repeated several times from different points of a program. They also make programs more readable and easier to manage and maintain. A large program can be split into many functions that are tested independently and, once all of them are working, are combined to produce the final program.
The mikroC language library functions have also been described briefly, along with examples of how to use several of these functions in main programs. Library functions simplify programmers’ tasks by providing ready and tested routines that can be called and used in their programs.
4.5 Exercises
1. Write a function to calculate the circumference of a rectangle. The function should receive the two sides of the rectangle as floating point numbers and return the circumference as a floating point number.
2. Write a main program to use the function you developed in Exercise 1. Find the circumference of a rectangle whose sides are 2.3cm and 5.6cm. Store the result in a floating point number called MyResult.
3. Write a function to convert inches to centimeters. The function should receive inches as a floating point number and then calculate the equivalent centimeters.
4. Write a main program to use the function you developed in Exercise 3. Convert 12.5 inches into centimeters and store the result as a floating point number.
5. An LED is connected to port pin RB0 of a PIC18F452-type microcontroller through a current limiting resistor in current sinking mode. Write a program to flash the LED in five-second intervals.
6. Eight LEDs are connected to PORTB of a PIC18F452-type microcontroller. Write a program so that the LEDs count up in binary sequence with a one-second delay between outputs.
7. An LED is connected to port pin RB7 of a PIC18F452 microcontroller. Write a program to flash the LED such that the ON time is five seconds, and the OFF time is three seconds.
8. A text-based LCD is connected to a PIC18F452-type microcontroller in 4-bit data mode. Write a program that will display a count from 0 to 255 on the LCD with a one-second interval between counts.
9. A text-based LCD is connected to a PIC microcontroller as in Exercise 8. Write a program to display the text “Exercise 9” on the first row of the LCD.
10. Repeat Exercise 9 but display the message on the first row, starting from column 3 of the LCD.
11. A two-row text-based LCD is connected to a PIC18F452-type microcontroller in 4-bit-data mode. Write a program to display the text “COUNTS:” on row 1 and then to count repeatedly from 1 to 100 on row 2 with two-second intervals.
12. Write a program to calculate the trigonometric cosine of angles from 0 to 45 in steps of 1 and store the results in a floating point array.
13. Write a function to calculate and return the length of the hypotenuse of a right-angle triangle, given its two sides. Show how you can use the function in a main program to calculate the hypotenuse of a right-angle triangle whose two sides are 4.0cm and 5.0cm.
14. Write a program to configure port pin RB2 of a PIC18F452 microcontroller as the RS232 serial output port. Send character “X” to this port at 4800 baud.
15. Port RB0 of a PIC18F452 microcontroller is configured as the RS232 serial output port. Write a program to send out string “SERIAL” at 9600 baud.
16. Repeat Exercise 15 but use the hardware USART available on the microcontroller chip.
17. Explain the differences between software-implemented serial data communication and USART hardware-based serial communication.
18. Write a function to add two arrays that are passed to the function as arguments. Store the sum in one of these arrays.
19. Write a function to perform the following operations on two-dimensional matrices:
a) Add matrices
b) Subtract matrices
c) Multiply matrices
20. Write a function to convert between polar and rectangular coordinates.
21. Write functions to convert temperature expressed in Celsius to Fahrenheit and vice versa. Show how these functions can be called from main programs to convert 20°C to °F and also 100°F to °C.
22. Write a program to store the value of function f(x) in an array as x is varied from 0 to 10 in steps of 0.5. Assume that:
f(x) = 1.3x³ – 2.5x² + 3.1x – 4.5
CHAPTER 5
PIC18 Development Tools
The development of a microcontroller-based system is a complex process. Development tools are hardware and software tools designed to help programmers develop and test systems in a relatively short time. There are many such tools, and a discussion of all of them is beyond the scope of this book. This chapter offers a brief review of the most common tools.
The tools for developing software and hardware for microcontroller-based systems include editors, assemblers, compilers, debuggers, simulators, emulators, and device programmers. A typical development cycle starts with writing the application program using a text editor. The program is then translated into an executable code with the help of an assembler or compiler. If the program has several modules, a linker is used to combine them into a single application. Any syntax errors are detected by the assembler or compiler and must be corrected before the executable code can be generated. Next, a simulator is used to test the application program without the target hardware. Simulators are helpful in checking the correctness of an algorithm or a program with limited or no input-outputs, and most errors can be removed during simulation. Once the program seems to be working and the programmer is happy with it, the executable code is loaded to the target microcontroller chip using a device programmer, and the system logic is tested. Software and hardware tools such as in-circuit debuggers and in-circuit emulators can analyze the program’s operation and display the variables and registers in real time with the help of breakpoints set in the program.