3
UNDERSTANDING MICROCHIP TECHNOLOGY’S PIC 16F877A: A DESCRIPTION OF THE MCU

PIC microcontrollers are manufactured by the Microchip Technology Corporation of Chandler, Arizona. The following address and Web site may be used to contact Microchip Technology. The Web site provides downloads for the datasheets.

Microchip Technology Corporation Inc.
2355 West Chandler Blvd.
Chandler, Arizona, USA 85224-6199
(480) 792-7200
(480) 899-9210 FAX
Web site:
www.microchip.com

MicroEngineering Labs, Inc. maintains a very useful and helpful Web site that will also be a tremendous aid to you as you learn about the PIC microcontrollers by using their LAB-X1.

MicroEngineering Labs

MicroEngineering Labs

Box 60039

1750 Brantfeather Grove

Colorado Springs, CO 80960-0039

Colorado Springs, CO 80960

(719) 520-5323

 

(719) 520-1867

 

e-mail: Support@melabs.com

 

Web site: www.melabs.com/index.htm

 

We will be using the 16F877A Microcontroller in the LAB-X1 board. Not all the features described in the 220-page datasheet of the 16F877A will be addressed in the exercises to follow, but enough will be discussed to give you the confidence and understanding you need to proceed on your own. In more technical terms, this MCU has the following features

16F877A Microcontroller’s Core Features

(Reduced from descriptions provided by Microchip Technology, Inc.)

High-performance RISC CPU

Operating speed: DC - 20 MHz clock input

DC - 200 ns instruction cycle

Up to 8K × 14 words of FLASH program memory

Up to 368 × 8 bytes of data memory (RAM)

Up to 256 × 8 bytes of EEPROM data memory

Interrupt capability

Power-on reset (POR)

Power-up timer (PWRT) and oscillator start-up timer (OST)

Watchdog timer (WDT) with its own on-chip RC

Programmable code-protection

Power saving SLEEP mode

Selectable oscillator options

Low-power high-speed CMOS FLASH/EEPROM technology

Fully static design

In-Circuit Serial Programming (ICSP) via two pins

Single 5-volts In-Circuit Serial Programming capability

In-Circuit Debugging via two pins

Processor read/write access to program memory

Wide operating voltage range: 2.0 volts to 5.5 volts

High sink/source current: 25 milliamp

Commercial and industrial temperature ranges

Low-power consumption

Peripheral Features

The following are peripheral features of the 16F877A Microcontroller:

sqr Timer0: 8-bit timer/counter with 8-bit prescalar.

sqr Timer1: 16-bit timer/counter with prescalar (It can be incremented during sleep via an external crystal/clock.)

sqr Timer2: 8-bit timer/counter with an 8-bit period register, prescalar, and postscalar

sqr Two PWM modules (maximum resolution is 10 bits)

sqr 10-bit multichannel analog-to-digital converter

sqr Synchronous serial port (SSP)

sqr Universal synchronous asynchronous receiver transmitter (USART)

sqr Parallel slave port (PSP) 8 bits wide

sqr Brown-out detection circuitry for Brown-out reset (BOR)

This MCU is described in profuse detail in a 220-page datasheet you can download from the Microchip Web site at no charge. The datasheet is a PDF document that you should have available to you at all times (maybe, even open, in its own window, ready for immediate access), whenever you are programming the 16F877A. The software you need in order to read (but not write) PDF files is also available at no charge on the Web. You should have a copy of the latest version (9) of this very useful software (Adobe Reader) on your computer.

We will not cover the entire 220-page datasheet in these exercises, but the most commonly used features of the MCU (especially the ones relevant to the LAB-X1 and those needed for our instruments and controllers) will be discussed. After doing the exercises, you should be comfortable with reading the datasheet and finding the information you need to get your work done.

In our particular case, on the LAB-X1 board, the MCU is already connected to the items on the board. Therefore, if you want to use the LAB-X1 for your own hardware experiments, you must use the MCU pins in a way that is compatible with the components that are already connected to them. Often times, even though a pin is being used in the LAB-X1 circuitry, you can drive something else with it without adversely affecting your experiment (depending on the load being added). The following is a list you can refer to in order to quickly determine if the pin and port you want to use is free, or discover how it is being used.

PORTA

PIN#

USAGE

PORTA HAS ONLY SIX EXTERNAL PINS

PORTA.0

2

5K ohm Potentiometer 0

Memory chips

PORTA.1

3

5K ohm Potentiometer 1

Memory chips

PORTA.2

4

Used by clock chips

 

PORTA.3

5

5K ohm Potentiometer 2

Used by clock chips, memory

PORTA.4

6

This specific pin has special pull-up needs!

No analog function

PORTA.5

7

Free for A-to-D conversion

Memory chips

PORTB

PORTB.0

33

Keypad inputs

 

PORTB.1

34

Keypad inputs

 

PORTB.2

35

Keypad inputs

 

PORTB.3

36

Keypad inputs

Progr’g device

PORTB.4

37

Keypad inputs

 

PORTB.5

38

Keypad inputs

 

PORTB.6

39

Keypad inputs

Progr’g device

PORTB.7

40

Keypad inputs

Progr’g device

PORTC

PORTC.0

15

Servo/Clock

 

PORTC.1

16

Clock chips

Memory chips, servo/clock, HPWM

PORTC.2

17

Piezo speaker

HPWM

PORTC.3

18

Clock chips

Memory chips, servo/clock

PORTC.4

23

Used with Memory chips

 

PORTC.5

24

Clock chips

A/D conversion, memory chips

PORTC.6

25

Transmit serial communications

RS232C

PORTC.7

26

Receive serial communications

RS232C

PORTD

PORTD.0

19

LCD and LED bargraph

 

PORTD.1

20

LCD and LED bargraph

 

PORTD.2

21

LCD and LED bargraph

 

PORTD.3

22

LCD and LED bargraph

 

PORTD.4

27

LCD and LED bargraph

 

PORTD.5

28

LCD and LED bargraph

 

PORTD.6

29

LCD and LED bargraph

 

PORTD.7

30

LCD and LED bargraph

 

PORTE

PORTE Has Only 3 External Pins

PORTE.0

8

LCD writing controls

 

PORTE.1

9

LCD writing controls

 

PORTE.2

10

LCD writing controls

Communications

Other Pins

Pin 1

MCLR

Microprocessor reset pin pull-up

Programming

Pin 11

Vdd

Logic power 5VDC

Has no other use

Pin 12

Vss

Logic ground

Has no other use

Pin 13

OSC1

Oscillator

Has no other use

Pin 14

OSC2

Oscillator

Has no other use

Pin 31

Vss

Logic ground

Has no other use

Pin 32

Vdd

Logic power, 5VDC

Has no other use

Re-listed in serial order, the pins are used as follows:

Pin 1

MCLR

Processor reset pin, pull up

Progr’g device

Pin 2

PORTA.0

5K ohm Potentiometer 0

 

Pin 3

PORTA.1

5K ohm Potentiometer 1

 

Pin 4

PORTA.2

A-to-D conversions

 

Pin 5

PORTA.3

5K ohm Potentiometer 2

Clock chips U6

Pin 6

PORTA.4

This specific pin has special pull-up needs!

No analog function

Pin 7

PORTA.5

A-to-D conversion

Memory chips

Pin 8

PORTE.0

LCD writing controls

 

Pin 9

PORTE.1

LCD writing controls

 

Pin 10

PORTE.2

LCD writing controls

Communications

Pin 11

Vdd

Logic power

Has no other use

Pin 12

Vss

Logic ground

Has no other use

Pin 13

OSC1

Oscillator

Has no other use

Pin 14

OSC2

Oscillator

Has no other use

Pin 15

PORTC.0

Servo/clock

 

Pin 16

PORTC.1

Clock chips

Memory chips, servo/clock, HPWM

Pin 17

PORTC.2

Piezo speaker

HPWM

Pin 18

PORTC.3

Clock chips

Memory chips, servo/clock

Pin 19

PORTD.0

LCD and LED bargraph

 

Pin 20

PORTD.1

LCD and LED bargraph

 

Pin 21

PORTD.2

LCD and LED bargraph

 

Pin 22

PORTD.3

LCD and LED bargraph

 

Pin 23

PORTC.4

Used with Memory chips

 

Pin 24

PORTC.5

Clock chips

A-to-D conversion, memory chips

Pin 25

PORTC.6

Transmit serial communications

RS232C

Pin 26

PORTC.7

Receive serial communications

RS232C

Pin 27

PORTD.4

LCD and LED bargraph

 

Pin 28

PORTD.5

LCD and LED bargraph

 

Pin 29

PORTD.6

LCD and LED bargraph

 

Pin 30

PORTD.7

LCD and LED bargraph

 

Pin 31

Vss

Logic ground

Has no other use

Pin 32

Vdd

Logic power

Has no other use

Pin 33

PORTB.0

 

Keypad inputs

Pin 34

PORTB.1

 

Keypad inputs

Pin 35

PORTB.2

 

Keypad inputs

Pin 36

PORTB.3

Progr’g device

Keypad inputs

Pin 37

PORTB.4

 

Keypad inputs

Pin 38

PORTB.5

 

Keypad inputs

Pin 39

PORTB.6

Progr’g device

Keypad inputs

Pin 40

PORTB.7

Progr’g device

Keypad inputs

PORTB lines set as inputs can be pulled up internally with a software instruction. Interrupts can be generated by changes on PORTB lines when they are programmed to do so.

USING THE A-TO-D CAPABILITIES OF THE PIC 16F877A

You can make a number of basic measurements with the LAB-X1 board by using its analog-to-digital conversion capabilities. The resolution of the conversion can be 8 or 10 bits. Still higher resolutions are available if we use ICs that go in empty socket U6. The measurements we make can be used to determine the following:

sqr Resistance

sqr Capacitance

sqr Voltage

sqr Frequency (This is, of course, not an A-to-D function.)

Resistance is determined by measuring how long it takes a resistor to discharge a capacitor that has just been charged. The measurement is as accurate as the value of the capacitor. The measurement parameters may need to be adjusted in real time to get a

f0027-01

Figure 3.1 A nearly full-size image of the versatile LAB-X1 experimental board.

usable reading (meaning the value of the two components must be selected to get a reading in a reasonable time with reasonable accuracy).

If the relative position of the wiper on a variable potentiometer is required, the A-to-D conversion capabilities of the LAB-X1 can be used to read the potentiometer wiper position (not the resistance). The A-to-D converter always measures the voltage across the device that you connect to the analog input port. You have the choice of reading the value to a resolution of either 8 or 10 bits. If you are reading an 8-bit A-to-D value, the value across the resistance is divided into 256 divisions and the reading will always be between 0 and 255 (inclusive). If you are doing a 10-bit A-to-D conversion, the value will be between 0 and 1023 (inclusive), but since 1 byte can hold only 8 bits, the remaining 2 bits must be read from another register. This is explained in more detail in the section on setting up A-to-D conversions for the IC in socket U6. Here, let’s take a quick look at the POT command (used to read a potentiometer) to see how this works.

The POT Command

The compiler provides the POT command to make it easy to read the resistive load placed on a pin. In order to use this command, it is necessary to set up the connection to the LAB-X1 as follows:

Set up the pin used on the MCU for analog mode

Select the pin to be used for input

Select what the excitation voltage source will be (internal or external)

On the 16F877A, only 16 pins can be used with the POT command, and these 16 pins have been assigned the aliases from PIN0 to PIN15 in the include file BS1DEFS.BAS (see the PBP manual). For the 16F877A, these are the pins on PORTB (0 to 7) and PORTC (8 to 15). Other PIC MCU have different designations based on their pin counts. See page 26 of the PICBASIC PRO manual for more information.

The POT command is:

 POT Pin, Scale, NMBR

Where:

Pin is the pin number we have been discussing

Scale is the adjustment for various RC constants. If the RC constant is large, the value of Scale should be small. Scale is determined experimentally with a potentiometer in place of the resistive load. At the low end of the resistance, the value of scale should be 0, and at the high end it should be 255.

NMBR is the variable the result will be placed in.

Values between 5 and 50K ohms may be read with a 0.1 uF capacitor, as shown in the Compiler manual under the POT command.

Capacitance

Capacitance can be measured by determining how long it takes to charge a capacitor through an accurately calibrated resistor or by setting up an oscillator with the two components and measuring the oscillator frequency.

Voltage

Voltage is measured by setting up an appropriate dividing network with precision resistors and measuring the voltage across an appropriate resistance.

Frequency

The PIC 16F877A can measure frequencies directly (as made possible by the compiler software). The timers and counters within the MCU are used to set the measurement intervals and counting hardware.

Reading Switches

Switches can be read from the lines of any port set up as an input port. De-bouncing must be performed either in hardware or in software to avoid false readings. (The BUTTON command provides flexible de-bouncing capabilities.)

Make sure that other hardware that may be connected to the pins does not interfere with the switch function and its detection.

Reading Switches in a Matrix

Switches arranged in a matrix can be read by setting and reading the rows and columns in the matrix. The technique activates one row of buttons at a time by making it high or low and then seeing if any of the columns have been affected. A detailed description of how this is done is in Chapter 5.

Configuring and Controlling the Properties of the Ports

The PIC 16F877A provides 33 I/O pins distributed across five ports. Each of the ports has unique capabilities built into it. The following section of this book discusses the capabilities of each of the ports with special attention to these properties.

These descriptions are cursory and are designed to provide a quick and ready reference. Refer to the actual datasheet for detailed information on these ports. The datasheet provides information at a level that cannot be provided in a short introductory text like this. Referring to the datasheets frequently will make you comfortable with them in a short time.

PORTA

PORTA is a 6-bit-wide bidirectional port with both analog and digital capability.

Note The general rule is that if a PIC device has any analog inputs built into it, it will come up as an analog input device on reset and/or startup.

The PIC 16F877A has analog capability on PORTA (and PORTE) so it comes up as an analog device on startup and reset. If you are going to use it as a digital device, you must set register ADCON as needed. The most common value is ADCON1 to %00000111 for using the LCD display. This line of code will be seen in many of the programs in this book and is explained in Chapter 5. (%00000111 sets all the analog pins to digital, but there are many other choices.)

The PIC 16F877A supports external access to only six of the eight pins on this port. Each of the six pins may be set to function as an input or output by appropriately loading the TRISA register. A “0” (zero) in a bit in this register sets the corresponding pin to function as an output, while a “1” (one) sets it to function as an input.

Thus, setting…

  TRISA=%00111000

(% means this is a binary number. We will use this binary notation throughout this book because it makes it easier to see what each bit is being set to. Bit 7, the most significant bit, is on the left; bit 0, the least significant, is on the right.)

Would make lines A0, A1, and A2 outputs and lines A3, A4, and A5 inputs. The most significant 2 bits are ignored (and could be set to 1s or 0s) because PORTA has only six active lines. (However, the 2 ignored bits are used by the processor and they can be read when necessary. You should not set them. Again, see the datasheet for specific details.)

As mentioned earlier, the specific functions (the uses the pins are put to) of the pins are controlled with the ADCON1 (the first A-to-D CONtrol) register.

All the pins have TTL level inputs and full CMOS level output drivers. This makes it easy to connect these lines directly to standard logic components (meaning that usually no intermediate resistors are needed between components if TTL or CMOS components are interconnected).

PORTA designations are somewhat complicated. Pins A0, A1, A2, A3, (skip A4), and Pin A5 can be configured as analog inputs by setting the ADCON1 register appropriately. Pin A3 is also used as a voltage input for comparing with the analog voltage inputs on other pins. Pin PORTA4, is used as input for the timer TIMER0. It is called T0CK1 for Timer0 clock Input. This is Pin 6 of the PIC and is used as the input pin for TIMER0 only when configured as such. It is a Schmitt trigger input with open drain output. Open drain means it acts like the contacts of a tiny relay. Schmitt triggered inputs have increased noise immunity (because of the hysteresis between state changes).

The two registers that control PORTA are TRISA and ADCON1.

ADCON1 controls the A-to-D and voltage reference functions of PORTA. The setting of the various bits select a complicated set of conditions that are described in detail in a table in the datasheet and in this book. (In the preceding discussion, when ADCON1 was set to %00000111, we were accessing this feature.)

Pin A4 has special needs when used as an output. It can be pulled down low but will float when set high. It must be pulled up with a (10K to 100K) resistor to tie it high when this is necessary. This pin has an open drain output rather than the usual bipolar state of the other pins. As noted earlier, this pin is skipped in the A-to-D conversion table. (Ignoring this fact is a source of many common mistakes, so keep this in mind.)

PORTB

PORTB is a full 8-bit-wide bidirectional port.

Internal circuitry (meaning built into the MCU) allows all the pins on PORTB to be pulled up to a high state (very weakly) by setting pin 7 of the option register (OPTION_REG.7) to 0. Only those pins designated at inputs are affected. These pull-ups are disabled on startup and on reset.

Pins B3, B6, and B7 are used for the low voltage programming of the PIC. Bit 3 in TRISB must be cleared (set to 0 or pulled down to 0) to negate the pull up on this pin to allow programming to take place. See pages 42 and 142 in the datasheet for more information regarding the B3 pin. This is very important.

Pins B4 to B7 will cause an interrupt to occur when their state changes if they are configured as inputs and the appropriate interrupts are configured. Any of these four pins that are configured as outputs will be excluded from the interrupt feature. The interrupts are controlled by the INTCON (interrupt control) register. This PORTB interrupt capability has the special feature of letting it be used to awaken a sleeping MCU.

Pin B0 has special interrupt functions that are controlled through the INTEDG bit, which is bit 6 of the OPTION_REG. See datasheet for more information. External interrupts are routed to the PIC through this pin. External interrupts are those that are created by events outside the PIC MCU.

The three registers that control PORTB are TRISB, INTCON, and OPTION_REG.

OPTION_REG controls the optional functions of PORTB as follows.

sqr Bit B0 of OPTION_REG sets prescalar value

sqr Bit B1 of OPTION_REG sets prescalar value

sqr Bit B2 of OPTION_REG sets prescalar value

sqr Bit B3 of OPTION_REG sets the prescalar option (used in low-voltage programming)

sqr Bit B4 of OPTION_REG sets Timer0 input pulse edge selection

sqr Bit B5 of OPTION_REG sets the clock selection

sqr Bit B6 of OPTION_REG sets edge selection for interrupts (programming uses)

sqr Bit B7 of OPTION_REG sets the pull-ups when cleared (programming uses)

PORTC

PORTC is a full 8-bit-wide bidirectional port.

All the pins on PORTC have Schmitt trigger input buffers. This means they are designed to be more immune to noise on these input lines.

The alternate function of the PORTC pins are defined as follows:

sqr Bit C0 I/O pin or Timer1 Oscillator output or Timer1 Clock input

sqr Bit C1 I/O pin or Timer1 Oscillator input or Capture 2 input or Compare 2 output or Hardware PWM2 output

sqr Bit C2 I/O pin or Capture 1 input or Compare 1 output or Hardware PWM1 output

sqr Bit C3 I/O pin or Synchronous clock for both SPI and I2C memory modes

sqr Bit C4 I/O pin or SPI data or data I/O for I2C mode

sqr Bit C5 I/O pin or Synchronous serial port data output

sqr Bit C6 I/O pin or USART Asynchronous transmit or synchronous clock

sqr Bit C7 I/O pin or USART Asynchronous receive or synchronous data

Special care must be taken when using PORTC’s special function capabilities, in that some of these functions will change/set the I/O status of certain other pins when in use, and this can cause unforeseen complications in the function of other capabilities. See the datasheet for details.

The register that controls PORTC is the TRISC register. No other registers are involved. DEFINEs are used to set or control certain PORTC functions.

Note The speaker on the LAB-X1 board is connected to pin C.1, so the use of this pin is limited because the noise generated by the speaker when this pin is used can be very irritating to humans. Since this is one of the lines that allows the generation of continuous background PWM signals (HPWM 2), it compromises the clean use of this pin unless the speaker is removed or the signal is put through a gate to clean it up. (I recommend that you avoid modifications to the board if you can.) The load of the tiny speaker loads the pin and can compromise certain other uses, but in most cases you can use this pin without concern. (Under certain situations, the frequency on this pin can be set high enough to be above human hearing range, allowing the pin to be used like the others.)

PORTD

PORTD is a full 8-bit-wide bidirectional port.

All the pins on PORTD have Schmitt trigger input buffers. This means they are designed to be more immune to noise on the input lines.

PORTD can also be configured as a microprocessor port by setting PSPMODE through setting TRISE.4 to 1. (Note that we are specifying bit 4 of PORTE here internally; there is no external pin 4.) In this mode, all the input pins are in TTL mode. (See page 48 of the datasheet.)

The alternate function of the PORTD pins are defined as follows:

Bit D0 or parallel slave port bit 0

 

Bit D1 or parallel slave port bit 1

 

Bit D2 or parallel slave port bit 2

 

Bit D3 or parallel slave port bit 3

Port D is the

Bit D4 or parallel slave port bit 4

parallel slave port

Bit D5 or parallel slave port bit 5

 

Bit D6 or parallel slave port bit 6

 

Bit D7 or parallel slave port bit 7

 

The registers that control PORTD are TRISD and TRISE. TRISE controls the operation of the PORTD parallel slave port mode when Bit TRISE.4 is set to 1. (Remember again that only pins E0, E1, and E2 are available external to the MCU on PORTE.)

Slave port functions as set by PORTE when Bit TRISE.4 is set to 1 are:

Bit TRISE.0 direction control of Pin PORTE.0 / RD / AN5

Bit TRISE.1 direction control of Pin PORTE.1 / WR / AN6

Bit TRISE.2 direction control of Pin PORTE.2 / CS / AN7

Bit TRISE.3. NOT USED

Bit TRISE.4 Slave port select

1=Slave port selected

0=Use as standard I/O port

Bit TRISE.5 Buffer overflow detect

1=Write occurred before reading old data

0=No error occurred

Bit TRISE.6 Buffer status

1=Still holds word

0=Has been read

Bit TRISE.7 Input buffer status

1=Full

0=Nothing received

Read the datasheet a couple of times to get a better understanding of these operations. The preceding is a very brief overview and is intended only to alert you and give you an idea of what the possibilities are.

PORTE

PORTE is only 3 external bits wide and is a bidirectional port. The other bits are internal and are used as mentioned earlier under PORTD (to which they are related). All external three pins can also be used as analog inputs.

All the pins on PORTE have Schmitt trigger input buffers when used as digital inputs.

The alternate function of the PORTE pins are defined as follows:

Pin RE0 direction control of Pin PORTE.0 / RD / AN5

Pin RE1 direction control of Pin PORTE.1 / WR / AN6

Pin RE2 direction control of Pin PORTE.2 / CS / AN7

TIMERS

The PIC 16F877A MCU has four timers: a watchdog timer and three regular timers. These allow the accurate timing (and counting) of chronological events. Timers are discussed in much greater detail in Chapter 6.

Some of the timers have (pre- and post-) scalars associated with them that can be used to multiply the timer setting by an integer amount. As you can imagine, the scaling ability is not adequate to allow all timed intervals to be created. We also must consider the uncertainty in the frequency of the clocking crystal, which is usually not exactly what it is stated to be (and may drift with ambient temperature). This means that though fairly accurate timings can be achieved with the hardware as received, additional software adjustments may have to be added if more accurate results are desired. This is done by having the software make a correction to the time every so often. (This also means that an external source, that is at least as accurate as the result we want, is needed to verify the timing accuracy of the device created.)

As always, the three timers in the microcontroller are clocked at a fourth of the oscillator speed, meaning that a timer within a 4 MHz system clock gets a counting signal at 1 MHz.

Very simply stated, an 8-bit timer will count up to 255 and then flip to zero and start counting from 0 to 255 again. An interrupt occurs (a bit is set) every time the timer register overflows from 255 to 0. We respond to the interrupt by doing whatever needs to be done in response to the interrupt and then resetting the interrupt flag bit (to 0). On timers that permit the use of a prescalar, the prescalar lets us increase the time between interrupts by multiplying the time between interrupts by a definable value in a 2-, 3-, or 4-bit location. On timers that can be written to, we can start the counter wherever we like (to change the interrupt timing), and on timers that can be read, we can read the contents whenever we want to.

Example: A 1-second timer setting with a prescalar set to 16 would provide us with an interrupt every 16 seconds, and we would have 16 seconds to do whatever we wanted to do between the interrupts or we would miss the next interrupt.

If we needed an interrupt every 14.5 seconds, we would use a timer set to 0.5 seconds and a prescalar of 29 if 29 was specifiable (which it is not). So not all time intervals can be created with this strategy because there are limits as to what can be put in the timer and what can be put in the prescalar when we are using 8/16-bit registers and specific oscillator speeds.

WATCHDOG TIMER

A watchdog timer is a timer that sets an interrupt when it runs out so as to tell us the program has hung up or gone awry for some reason. As such, it is expected that in a properly written program, the watchdog timer will never set an interrupt. This is accomplished by resetting the watchdog timer every so often within the program. The compiler does this automatically if the watchdog timer option is set. Setting the option does not guarantee a program that cannot hang up. Software errors and infinite loops that reset the timer within them can still cause hangups.

PRESCALARS AND POSTSCALARS

The value of the scaling factor that will be applied to the timer is determined by the contents of 2 or 3 bits in the interrupt control register. These bits multiply the time between interrupts often by powers of two. Prescalars and postscalars have the same effect on the interrupts: They delay them.

COUNTERS

Only Timer0 and Timer1 can be used as counters. Timer2 cannot be used as a counter because it does not have an external input line. Both the timers and the counters are covered in detail in Chapter 6.