15. Show the steps required to set up both INT1 and INT2 as falling-edge triggered interrupt inputs having low priority.
16. Show the steps required to set up INT1 as falling-edge triggered and INT2 as rising-edge triggered interrupt inputs having high priorities. Explain how to find the source of the interrupt when an interrupt occurs.
17. Show the steps required to set up Timer 0 to generate interrupts every millisecond with a high priority. What is the interrupt vector address?
18. In an application the CPU registers have been configured to accept interrupts from external sources INT0, INT1, and INT2. An interrupt has been detected. Explain how to find the source of the interrupt.
CHAPTER 3
C Programming Language
There are several C compilers on the market for the PIC18 series of microcontrollers. These compilers have many similar features, and they can all be used to develop C-based high-level programs for PIC18 microcontrollers.
Some of the C compilers used most often in commercial, industrial, and educational PIC18 microcontroller applications are:
• mikroC
• PICC18
• C18
• CCS
The popular and powerful mikroC, developed by MikroElektronika (web site: www.microe.com), is easy to learn and comes with rich resources, such as a large number of library functions and an integrated development environment with a built-in simulator and an in-circuit debugger (e.g., mikroICD). A demo version of the compiler with a 2K program limit is available from MikroElektronika.
PICC18, another popular C compiler, was developed by Hi-Tech Software (web site: www.htsoft.com) and is available in two versions: standard and professional. A powerful simulator and an integrated development environment (Hi-Tide) are provided by the company. PICC18 is supported by the PROTEUS simulator (www.labcenter.co.uk) which can be used to simulate PIC microcontroller–based systems. A limited-period demo version of this compiler is available on the developer’s web site.
C18 is a product of Microchip Inc. (web site: www.microchip.com). A limited-period demo version, as well as a limited functionality version of C18 with no time limit, are available from the Microchip web site. C18 includes a simulator and supports hardware and software development tools such as in-circuit emulators (e.g., ICE2000) and in-circuit debuggers (e.g., ICD2).
CCS has been developed by the Custom Computer Systems Inc. (web site: www. ccsinfo.com). The company offers a limited-period demo version of their compiler. CCS provides a large number of built-in functions and supports an in-circuit debugger (e.g., ICD-U40) which are very helpful in the development of PIC18 microcontroller–based systems. In this book we are mainly concentrating on the use of the mikroC compiler, and most of the projects are based on this compiler.
3.1 Structure of a mikroC Program
Figure 3.1 shows the simplest structure of a mikroC program. This program flashes an LED connected to port RB0 (bit 0 of PORTB) of a PIC microcontroller in one-second intervals. Do not worry if you don’t understand the operation of the program at this stage, as all will come clear as this chapter progresses. Some of the programming elements in Figure 3.1 are described in detail here.
/********************************************************************
LED FLASHING PROGRAM
*********************************
This program flashes an LED connected to port pin RB0 of PORTB with
one second intervals.
Programmer : D. Ibrahim
File : LED.C
Date : May, 2007
Micro : PIC18F452
**********************************************************************/
void main() {
for(;;) // Endless loop
{
TRISB = 0; // Configure PORTB as output
PORTB.0 = 0; // RB0 = 0
Delay_Ms(1000); // Wait 1 second
PORTB.0 = 1; // RB0 = 1
Delay_Ms(1000); // Wait 1 second
} // End of loop
}
Figure 3.1: Structure of a simple C program
Comments are used to clarify the operation of the program or a programming statement. Comment lines are ignored and not compiled by the compiler. In mikroC programs comments can be of two types: long comments, extending several lines, and short comments, occupying only a single line. Comment lines at the beginning of a program can describe briefly the program’s operation and provide the author’s name, the program filename, the date the program was written, and a list of version numbers, together with the modifications in each version. As shown in Figure 3.1, comments can also be added after statements to describe the operations that the statements perform. Clear and succinct comment lines are important for the maintenance and thus the lifetime of a program, as a program with good comments is easier to modify and/or update.
As shown in Figure 3.1, long comments start with the character “/*” and terminate with the character “*/”. Similarly, short comments start with the character “//” and do not need a terminating character.
3.1.2 Beginning and Ending of a Program
In C language, a program begins with the keywords:
void main()
After this, a curly opening bracket is used to indicate the beginning of the program body. The program is terminated with a closing curly bracket. Thus, as shown in Figure 3.1, the program has the following structure:
void main() {
program body
}
3.1.3 Terminating Program Statements
In C language, all program statements must be terminated with the semicolon (“;”) character; otherwise a compiler error will be generated:
j = 5; // correct
j = 5 // error
White spaces are spaces, blanks, tabs, and newline characters. The C compiler ignores all white spaces. Thus, the following three sequences are identical:
int i; char j;
or
int i;
char j;
or
int i;
char j;
Similarly, the following sequences are identical:
i = j + 2;
or
i = j
+ 2;
In general, C language is case sensitive and variables with lowercase names are different from those with uppercase names. Currently, however, mikroC variables are not case sensitive (although future releases of mikroC may offer case sensitivity) so the following variables are equivalent:
total TOTAL Total ToTal total totaL
The only exception is the identifiers main and interrupt , which must be written in lowercase in mikroC. In this book we are assuming that the variables are case sensitive, for the sake of compatibility with other C compilers, and variables with the same name but different cases are not used.
In C language, variable names can begin with an alphabetical character or with the underscore character. In essence, variable names can include any of the characters a to z and A to Z, the digits 0 to 9, and the underscore character “_”. Each variable name should be unique within the first 31 characters of its name. Variable names can contain uppercase and lowercase characters (see Section 3.1.5), and numeric characters can be used inside a variable name. Examples of valid variable names are:
Читать дальше