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

3. Explain the various ways the PIC18F microcontroller can be reset. Draw a circuit diagram to show how an external push-button switch can be used to reset the microcontroller.

4. Describe the various clock sources that can be used to provide a clock to a PIC18F452 microcontroller. Draw a circuit diagram to show how a 10MHz crystal can be connected to the microcontroller.

5. Draw a circuit diagram to show how a resonator can be connected to a PIC18F microcontroller.

6. In a non-time-critical application a clock must be provided for a PIC18F452 microcontroller using an external resistor and a capacitor. Draw a circuit diagram to show how this can be done and find the component values for a required clock frequency of 5MHz.

7. Explain how an external clock can provide clock pulses to a PIC18F microcontroller.

8. What are the registers of PORTA? Explain the operation of the port by drawing the port block diagram.

9. The watchdog timer must be set to provide an automatic reset every 0.5 seconds. Describe how to do this, including the appropriate register bits.

10. PWM pulses must be generated from pin CCP1 of a PIC18F452 microcontroller. The required pulse period is 100ms, and the required duty cycle is 50%. Assuming the microcontroller is operating with a 4MHz crystal, calculate the values to be loaded into the various registers.

11. Again, with regard to PWM pulses generated from pin CCP1 of a PIC18F452 microcontroller: If the required pulse frequency is 40KHz, and the required duty cycle is 50%, and assuming the microcontroller is operating with a 4MHz crystal, calculate the values to be loaded into the various registers.

12. An LM35DZ-type analog temperature sensor is connected to analog port AN0 of a PIC18F452 microcontroller. The sensor provides an analog output voltage proportional to the temperature (i.e., V0 = 10 mV/°C). Show the steps required to read the temperature.

13. Explain the difference between a priority interrupt and a nonpriority interrupt.

14. Show the steps required to set up INT2 as a falling-edge triggered interrupt input having low priority. What is the interrupt vector address?

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

3.1.1 Comments

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.