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

In the PIC18F452 microcontroller, PORTE is only 3 bits wide. As shown in Table 2.10, port pins are shared with analog inputs and with parallel slave port read/write control bits. On a power-on reset, PORTE pins are configured as analog inputs and register ADCON1 must be programmed to change these pins to digital I/O.

Table 2.10: PIC18F452 PORTE pin functions

Pin Description
RE0/RD/AN5  
RE0 Digital I/O
RD Parallel slave port read control pin
AN5 Analog input 5
RE1/WR/AN6  
RE1 Digital I/O
WR Parallel slave port write control pin
AN6 Analog input 6
RE2/CS/AN7  
RE2 Digital I/O
CS Parallel slave port CS
AN7 Analog input 7

2.1.9 Timers

The PIC18F452 microcontroller has four programmable timers which can be used in many tasks, such as generating timing signals, causing interrupts to be generated at specific time intervals, measuring frequency and time intervals, and so on.

This section introduces the timers available in the PIC18F452 microcontroller.

Timer 0

Timer 0 is similar to the PIC16 series Timer 0, except that it can operate either in 8-bit or in 16-bit mode. Timer 0 has the following basic features:

• 8-bit or 16-bit operation

• 8-bit programmable prescaler

• External or internal clock source

• Interrupt generation on overflow

Timer 0 control register is T0CON, shown in Figure 2.24. The lower 6 bits of this register have similar functions to the PIC16-series OPTION register. The top two bits are used to select the 8-bit or 16-bit mode of operation and to enable/disable the timer.

Figure 2.24: Timer 0 control register, T0CON

Timer 0 can be operated either as a timer or as a counter. Timer mode is selected by clearing the T0CS bit, and in this mode the clock to the timer is derived from FOSC/4. Counter mode is selected by setting the T0CS bit, and in this mode Timer 0 is incremented on the rising or falling edge of input RA4/T0CKI. Bit T0SE of T0CON selects the edge triggering mode.

An 8-bit prescaler can be used to change the timer clock rate by a factor of up to 256. The prescaler is selected by bits PSA and T0PS2:T0PS0 of register T0CON.

8-Bit Mode Figure 2.25 shows Timer 0 in 8-bit mode. The following operations are normally carried out in a timer application:

• Clear T0CS to select clock FOSC/4

• Use bits T0PS2:T0PS0 to select a suitable prescaler value

• Clear PSA to select the prescaler

• Load timer register TMR0L

• Optionally enable Timer 0 interrupts

• The timer counts up and an interrupt is generated when the timer value overflows from FFH to 00H in 8-bit mode (or from FFFFH to 0000H in 16-bit mode)

Figure 2.25: Timer 0 in 8-bit mode

By loading a value into the TMR0 register we can control the count until an overflow occurs. The formula that follows can be used to calculate the time it will take for the timer to overflow (or to generate an interrupt) given the oscillator period, the value loaded into the timer, and the prescaler value:

Overflow time = 4 × TOSC × Prescaler × (256–TMR0)    (2.1)

where

 Overflow time is in ms

 TOSC is the oscillator period in μs

 Prescaler is the prescaler value

 TMR0 is the value loaded into TMR0 register

For example, assume that we are using a 4MHz crystal, and the prescaler is chosen as 1:8 by setting bits PS2:PS0 to 010. Also assume that the value loaded into the timer register TMR0 is decimal 100. The overflow time is then given by:

4MHZ clock has a period; T = 1/f = 0.25 μs

using the above formula

Overflow time = 4 × 0.25 × 8 × (256 – 100) = 1248 μs

Thus, the timer will overflow after 1.248 msec, and a timer interrupt will be generated if the timer interrupt and global interrupts are enabled.

What we normally want is to know what value to load into the TMR0 register for a required overflow time. This can be calculated by modifying Equation (2.1) as follows:

TMR0 = 256 – (Overflow time)/(4 × TOSC × Prescaler)    (2.2)

For example, suppose we want an interrupt to be generated after 500ms and the clock and the prescaler values are as before. The value to be loaded into the TMR0 register can be calculated using Equation (2.2) as follows: