Файл: Microcontroller based applied digital control (D. Ibrahim, 2006).pdf

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 14.06.2025

Просмотров: 3295

Скачиваний: 1

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

MINIMUM PIC CONFIGURATION

61

Table 3.6 Some PIC18CXXX family members

Program

Data

A/D

Microcontroller

memory

RAM

I/O pins

EEPROM

converters

18C242

8192

× 16

512

2

0

5

18C452

16384

× 16

1536

34

0

8

18C658

16384

× 16

1536

52

0

12

18C858

16384

× 16

1536

68

0

16

18F242

8192

× 16

256

23

256

5

3.2 MINIMUM PIC CONFIGURATION

The minimum PIC configuration depends on the type of microcontroller used. Normally, the operation of a PIC microcontroller requires a power supply, reset circuit and oscillator.

The power supply is usually +5 V and, as shown in Figure 3.3, can be obtained from the mains supply by using a step-down transformer, a rectifier circuit and a power regulator chip, such as the LM78L05.

Although PIC microcontrollers have built-in power-on reset circuits, it is useful in many applications to have external reset circuits. When the microcontroller is reset, all of its special function registers are put into a known state and execution of the user program starts from address 0 of the program memory.

As shown in Figure 3.4, reset is normally achieved by connecting a 4.7 K pull-up resistor from the master clear (MCLR) input to the supply voltage. Sometimes the voltage rises too slowly and the simple reset circuit may not work. In this case, the circuit shown in Figure 3.5 is recommended.

In many applications it may be required to reset the microcontroller by pressing an external button. The circuit given in Figure 3.6 enables the microcontroller to reset when the button is pressed.

PIC microcontrollers have built-in clock oscillator circuits. Additional components are needed to enable such clock oscillator circuits to function; some PIC microcontrollers have these built in, while others require external components. The internal oscillator can be operated in one of six modes:

external oscillator;

LP – low-power crystal;

XT – low-speed crystal/resonator;

Figure 3.3 A simple microcontroller power source


62 THE PIC MICROCONTROLLER

Figure 3.4 Simple reset circuit

Figure 3.5 Recommended reset circuit

Figure 3.6 Push-button reset circuit


MINIMUM PIC CONFIGURATION

63

Figure 3.7 Using an external oscillator

HS – high-speed crystal/resonator;

RC – resistor and capacitor;

no external components (only some PICs).

3.2.1 External Oscillator

An external oscillator can be connected to the OSC1 input as shown in Figure 3.7. The oscillator should generate square wave pulses at the required frequency. The timing accuracy depends upon the accuracy of this external oscillator. When operated in this mode, the chip should be programmed for LP, XT or HS clock mode.

3.2.2 Crystal Operation

An external crystal should be used when very accurate timing is required. As shown in Figure 3.8, the crystal should be connected between the OSC1 and OSC2 inputs together with a pair of capacitors. The value of the capacitors should be chosen as in Table 3.7. For example, with a crystal of 4 MHz, two 22 pF capacitors can be used.

3.2.3 Resonator Operation

Resonators are usually available in the frequency range of about 4–8 MHz. Although resonators are not as accurate as crystals, they are usually accurate enough for most applications. Resonators have the advantages that they are low-cost and only one component is required compared to three components in the case of crystals (the crystal itself and two capacitors). Figure 3.9 shows how a resonator can be used with PIC microcontrollers.

64 THE PIC MICROCONTROLLER

Figure 3.8 Using a crystal

Table 3.7 Capacitor values for crystal operation

Mode

Frequency

C1, C2 (pF)

LP

32 kHz

68–100

LP

200 kHz

15–33

XT

100 kHz

100–150

XT

2 MHz

15–33

XT

4 MHz

15–33

HS

4 MHz

15–33

HS

10 MHz

15–33

Figure 3.9 Using a resonator

MINIMUM PIC CONFIGURATION

65

+ 5V

R

OSC1

C

PIC

Figure 3.10 Using an RC circuit

3.2.4 RC Operation

There are many low-cost applications where the timing accuracy is not important – flashing an LED every second, scanning a keyboard, reading the temperature every second, etc. In such applications the clock pulses can be generated by using an external resistor and a capacitor. As shown in Figure 3.10, the resistor and the capacitor should be connected to the OSC1 input of the microcontroller.

The oscillator frequency depends upon the values of the resistor and the capacitor, the supply voltage and environmental factors, such as the temperature. Table 3.8 gives a list of typical resistor and capacitor values for most of the frequencies of interest. For example, a 5 K resistor and a 20 pF capacitor can be used to generate a clock frequency of about 4 MHz.

3.2.5 Internal Clock

Some PIC microcontrollers (e.g. PIC12C672) have built-in clock generation circuitry and do not require any external components to generate the clock pulses. The built-in oscillator is

Table 3.8 Resistor and capacitor values

C (pF)

R (k )

Frequency

20

5

4.61 MHz

10

2.66 MHz

100

311 kHz

100

5

1.34 MHz

10

756 kHz

100

82.8 kHz

300

5

428 kHz

10

243 kHz

100

26.2 kHz


66 THE PIC MICROCONTROLLER

Figure 3.11 Resonator based minimum PIC microcontroller system

+5V

4.7K

MCLR

C1

OSC1

PIC

OSC2

C2

Figure 3.12 Crystal based minimum PIC microcontroller system

usually 4 MHz and can be selected during the programming of the devices. Figure 3.11 shows the circuit diagram of a minimum PIC microcontroller system using a 4 MHz resonator. A minimum PIC microcontroller system using a crystal is shown in Figure 3.12.

3.3 SOME POPULAR PIC MICROCONTROLLERS

In this section we shall look at the architectures of some of the popular PIC microcontrollers in greater detail. We have chosen the popular 18-pin PIC16F84 and the 40-pin PIC16F877 microcontrollers. The architectures and instruction sets of most of the other PIC microcontrollers are very similar, and with the knowledge gained here we should be able to use and program any other PIC microcontroller easily. Since our aim is to program the microcontrollers using a high-level language such as the C, there is no need to learn their exact architecture or assembly instruction set. We shall only look at the features which may be required while developing software using the C programming language.

SOME POPULAR PIC MICROCONTROLLERS

67

3.3.1 PIC16F84 Microcontroller

The PIC16F84 is one of the most popular PIC microcontrollers used in many commercial, industrial and hobby applications. This is an 18-pin device which can operate at up to 20 MHz clock speed. It offers 1024 × 14 flash program memory, 68 bytes of RAM data memory, 64 bytes of EEPROM nonvolatile data memory, 8-bit timer with pre-scaler, watchdog timer, 13 I/O pins, external and internal interrupt sources, and large current sink and source capability.

Figure 3.13 shows the pin configuration of the PIC16F84. The functions of various pins are as follows:

RB0–RB7

Bidirectional port B pins

RA0–RA4

Bidirectional port A pins

Vdd

Supply voltage

Vss

Ground

OSC1

Crystal, resonator, or external clock input

OSC2

Crystal or resonator input

MCLR

Reset input

INT

External interrupt input (shared with RB0)

T0CK1

Optional timer clock input (shared with RA3)

Note that some pin names have a bar on them – for example, MCLR in Figure 3.13. This means that the pin will be active when the applied signal is at logic low (logic 0 in this case).

The PIC16F84 provides four external or internal interrupt sources:

external interrupt on INT pin;

timer overflow interrupt;

state change interrupt on the four higher bits of port B (RB4–RB7);

EEPROM memory data write complete interrupt.

The data RAM is also known as the register file map (RFM) and consists of 80 bytes. As shown in Figure 3.14, the RFM is divided into two parts: the special function registers (SFR), and the general purpose registers (GPR). The RFM is organized as two banks (more complex PIC microcontrollers may have more banks): bank 0 and bank 1. The bank of a register must

16

OSC1/CLKIN

RA0

17

15

18

OSC2/CLKOUT

RA1

1

RA2

4

2

MCLR

RA3

3

RA4/T0CK1

6

14

RB0/INT

Vdd

7

RB1

8

RB2

9

5

RB3

Vss

10

RB4

11

RB5

12

RB6

13

RB7

Figure 3.13 PIC16F84 pin configuration


68 THE PIC MICROCONTROLLER

Figure 3.14 Register file map of PIC16F84

be selected before a register in a bank can be read or written to. Some of the registers are common to both banks.

The SFR are a collection of registers used by the CPU to control the internal operations and the peripherals – setting the I/O direction of a register, sending data to an I/O port, loading the timer register, etc. The SFR used while programming the microcontroller using a high-level language are described in the following sections.

3.3.1.1 OPTION REG Register

The OPTION REG register is a readable and writable register at address 0×81 (hexadecimal) of the RFM. This register controls the timer pre-scaler, timer clock edge selection, timer clock source, external interrupt edge selection, and port B pull-up resistors. OPTION REG bit definitions are given in Figure 3.15. For example, to configure the external interrupt INT pin so that external interrupts are accepted on the falling edge of the INT input, the following bit pattern should be loaded into the OPTION REG:

X0XXXXXX

where X is a don’t-care bit and can be a 0 or a 1.

3.3.1.2 INTCON Register

This is the interrupt control register at addresses 0×0B and 0×8B of the RFM. The bit definitions of this register are shown in Figure 3.16. INTCON is used to enable/disable the various interrupt sources and interrupt flags. For an interrupt to be accepted by the CPU, the

7

6

5

4

3

2

1

0

RBPU

INTEDG

T0CS

TOSE

PSA

PS2

PS1

PS0

Bit 7: PORTB pull-up control

1: PORTB pull-ups disabled 0: PORTB pull-ups enabled

Bit 6: INT external interrupt edge detect

1: Interrupt on rising edge of INT input 0: Interrupt on falling edge of INT input

Bit 5: TMR0 timer clock source 1: T0CK1 external pulse 0: internal clock

Bit 4: TMR0 source edge select

1: Increment on HIGH to LOW of T0CK1 0: Increment on LOW to HIGH of T0CK1

Bit 3: Pre-scaler assignment

1: Pre-scaler assigned to watchdog timer 0: Pre-scaler assigned to TMR0

Bit 2-0: Pre-scaler rate

000

1:2

001

1:4

010

1:8

011

1:16

1001:32

1011:64

1101:128

1111:256

Figure 3.15 OPTION REG bit definitions

7

6

5

4

3

2

1

0

GIE

EEIE

T0IE

INTE

RBIE

T0IF

INTF

RBIF

Bit 7: Global interrupt control

1: Enable all unmasked interrupts 0: Disable all interrupts

Bit 6: EEPROM write complete interrupt

1: Enable EEPROM write complete interrupt 0: Disable EEPROM write complete interrupt

Bit 5: TMR0 overflow interrupt 1: Enable TMR0 interrupt

0: Disable TMR0 interrupt

Bit 4: INT external interrupt control

1: Enable INT External interrupt 0: Disable INT External Interrupt

Bit 3: RB4–RB7 port change interrupt control

1: Enable RB4–RB7 port change interrupt 0: Disable RB4–RB7 port change interrupt

Bit 1: INT interrupt flag

1: INT interrupt occurred

0: INT interrupt did not occur

Bit 0: RB4–RB7 port change interrupt flag

1: One or more of RB4–RB7 pins changed state 0: None of RB4–RB7 pins changed state

Figure 3.16 INTCON bit definitions