Figure 2.17: External clock in EC mode
The ECIO mode is similar to the EC mode, except that the OSC2 pin can be used as a general purpose digital I/O pin. As shown in Figure 2.18, this pin becomes bit 6 of PORTA (i.e., pin RA6).
Figure 2.18: External clock in ECIO mode
In the many applications where accurate timing is not required we can use an external resistor and a capacitor to provide clock pulses. The clock frequency is a function of the resistor, the capacitor, the power supply voltage, and the temperature. The clock frequency is not accurate and can vary from unit to unit due to manufacturing and component tolerances. Table 2.5 gives the approximate clock frequency with various resistor and capacitor combinations. A close approximation of the clock frequency is 1/(4.2RC), where R should be between 3K and 100K and C should be greater than 20pF.
Table 2.5: Clock frequency with RC
C (pF) | R (K) | Frequency (MHz) |
---|---|---|
22 | 3.3 | 3.3 |
4.7 | 2.3 | |
10 | 1.08 | |
30 | 3.3 | 2.4 |
4.7 | 1.7 | |
10 | 0.793 |
In RC mode, the oscillator frequency divided by 4 (FOSC/4) is available on pin OSC2 of the microcontroller. Figure 2.19 shows the operation at a clock frequency of approximately 2MHz, where R=3.9K and C=30pF. In this application the clock frequency at the output of OSC2 is 2MHz/4=500KHz.
Figure 2.19: 2MHz clock in RC mode
RCIO mode is similar to RC mode, except that the OSC2 pin can be used as a general purpose digital I/O pin. As shown in Figure 2.20, this pin becomes bit 6 of PORTA (i.e., pin RA6).
Figure 2.20: 2MHz clock in RCIO mode
One of the problems with using high-frequency crystals or resonators is electromagnetic interference. A Phase Locked Loop (PLL) circuit is provided that can be enabled to multiply the clock frequency by 4. Thus, for a crystal clock frequency of 10MHz, the internal operation frequency will be multiplied to 40MHz. The PLL mode is enabled when the oscillator configuration bits are programmed for HS mode.
Some devices in the PIC18F family have internal clock modes (although the PIC18F452 does not). In this mode, OSC1 and OSC2 pins are available for general purpose I/O (RA6 and RA7) or as FOSC/4 and RA7. An internal clock can be from 31KHz to 8MHz and is selected by registers OSCCON and OSCTUNE. Figure 2.21 shows the bits of internal clock control registers.
Figure 2.21: Internal clock control registers
It is possible to switch the clock from the main oscillator to a low-frequency clock source. For example, the clock can be allowed to run fast in periods of intense activity and slower when there is less activity. In the PIC18F452 microcontroller this is controlled by bit SCS of the OSCCON register. In microcontrollers of the PIC18F family that do support an internal clock, clock switching is controlled by bits SCS0 and SCS1 of OSCCON. It is important to ensure that during clock switching unwanted glitches do not occur in the clock signal. PIC18F microcontrollers contain circuitry to ensure error-free switching from one frequency to another.
2.1.7 Watchdog Timer
In PIC18F-series microcontrollers family members the watchdog timer (WDT) is a free-running on-chip RC-based oscillator and does not require any external components. When the WDT times out, a device RESET is generated. If the device is in SLEEP mode, the WDT time-out will wake it up and continue with normal operation.
The watchdog is enabled/disabled by bit SWDTEN of register WDTCON. Setting SWDTEN = 1 enables the WDT, and clearing this bit turns off the WDT. On the PIC18F452 microcontroller an 8-bit postscaler is used to multiply the basic time-out period from 1 to 128 in powers of 2. This postscaler is controlled from configuration register CONFIG2H. The typical basic WDT time-out period is 18ms for a postscaler value of 1.
2.1.8 Parallel I/O Ports
The parallel ports in PIC18F microcontrollers are very similar to those of the PIC16 series. The number of I/O ports and port pins varies depending on which PIC18F microcontroller is used, but all of them have at least PORTA and PORTB. The pins of a port are labeled as RPn, where P is the port letter and n is the port bit number. For example, PORTA pins are labeled RA0 to RA7, PORTB pins are labeled RB0 to RB7, and so on.
When working with a port we may want to:
• Set port direction
• Set an output value
• Read an input value
• Set an output value and then read back the output value
The first three operations are the same in the PIC16 and the PIC18F series. In some applications we may want to send a value to the port and then read back the value just sent. The PIC16 series has a weakness in the port design such that the value read from a port may be different from the value just written to it. This is because the reading is the actual port bit pin value, and this value can be changed by external devices connected to the port pin. In the PIC18F series, a latch register (e.g., LATA for PORTA) is introduced to the I/O ports to hold the actual value sent to a port pin. Reading from the port reads the latched value, which is not affected by any external device.
In this section we shall be looking at the general structure of I/O ports.
In the PIC18F452 microcontroller PORTA is 7 bits wide and port pins are shared with other functions. Table 2.6 shows the PORTA pin functions.
Table 2.6: PIC18F452 PORTA pin functions
Pin | Description |
---|---|
RA0/AN0 | |
RA0 | Digital I/O |
AN0 | Analog input 0 |
RA1/AN1 | |
RA1 | Digital I/O |
AN1 | Analog input 1 |
RA2/AN2/VREF– | |
RA2 | Digital I/O |
AN2 | Analog input 2 |
VREF– | A/D reference voltage (low) input |
RA3/AN3/VREF+ | |
RA3 | Digital I/O |
AN3 | Analog input 3 |
VREF+ | A/D reference voltage (high) input |
RA4/T0CKI | |
RA4 | Digital I/O |
T0CKI | Timer 0 external clock input |
RA5/AN4/SS/LVDIN | |
RA5 | Digital I/O |
AN4 | Analog input 4 |
SS | SPI Slave Select input |
RA6 | Digital I/O |