Файл: The quintessential PIC microcontroller (S. Katzen, 2000).pdf

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

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

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

Добавлен: 15.06.2025

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

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

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

PART III

The Outside World

Apart from our brief discussion of the Havard structure in Chapter 3, we have confined our discussions to the internal structure of the microcontroller and its software. This final part looks at how the MCU core interacts with the environment physically beyond the confines of its pins. This process involves consideration of the interaction of the software and hardware of its integrated ports and devices, ending up with a case study which builds a complete stand-alone embedded controller. We will mainly concentrate on the PIC16F84 mid-range device but will breifly look at other devices where that is pertinent. On the way you will:

Look at support issues such as the power supply, clock, power management and device configuration.

Consider parallel and serial digital data input and output.

The Timer and Watchdog subsystems.

See what is involved in dealing with analog signals.

Examine relevant interrupt-handling issues in real-time interactions.

Design an embedded MCU-based viva timer.

Consider how a system may be tested and debugged.

CHAPTER 10

The Real World

Up to this point we have mainly concentrated on how the software has interacted with the processor’s internal registers and Data memory. Now, as a prelude to how the MCU relates to its internal peripheral devices and hence monitors and controls its external environment, i.e. the real world outside its pins, we need to look at external support issues, such as power supply requirements, clocking and resetting.

After reading this chapter you will:

Be familiar with the permitted range of power supply and input/output voltages.

Distinguish between quiescent and dynamic power dissipation and recognize that the latter is directly proportional to both frequency and to the square of the supply voltage.

Be aware of how the sleep mode is invoked and exited, and its e ect on the processor.

Understand the basics of the integral clock oscillator.

Know how the PIC’s configuration can be set up during programming.

Understand the various nuances of the Reset process.

Figure 10.1 gives the external view of some typical PIC family members, ranging from the minuscule 8-pin 12-bit PIC12C508/9, which features one 5-bit general-purpose parallel I/O port, a Timer and 12 Kbyte Program store and 25/41 file registers through to the jumbo 40-pin 14bit PIC16F877 which has a 8 Kbyte flash memory Program store, 368 file registers, 33 bits of parallel I/O, three Timers, a 10-bit A/D converter, several serial port formats and a 256-byte EEPROM Data module. We are going to mainly concentrate on the 18-pin PIC16F83/41 and the 40-pin PIC16C74, but most of the characteristics are similar across all PIC families. Where relevant, other family members will be used as the exemplar; particularly the PIC16C7X and PIC16F87X devices.

All members of the PIC family will operate typically with a supply voltage VDD of nominally 5 V. The standard PIC16F84 can operate over

1The PIC16F83 is identical to the PIC16F84 but has 50% less Program memory and 36 instead of 68 file registers. The latter is currently available as the PIC16F84A with minor enhancements.


254 The Quintessential PIC Microcontroller

GP2/T0CKI

5

4

GP3/MCLR

RB4

10

9

RB3

GP1

6

3

GP4/OSC2

RB5

11

8

RB1

12

7

GP0

7

2

GP5/OSC1/CLOCK

RB6

RB0/INT

8

1

VSS

VDD

RB7

13

6

VDD

(a) PIC12C508/9

VDD

14

5

VSS

OSC2/CLKOUT

15

4

MCLR

OSC1/CLKIN

16

3

RA4/T0CKI

RD2/PSP2

21

20

RD1/PSP1

RA0

17

2

RA3

RD3/PSP3

22

19

RD0/PSP0

RA1

18

1

RA2

RC4/SDI/SDA

23

18

RC3/SCK/SCL

(c)

PIC16F83/84

RC5/SDO

24

17

RC2/CCP1

RC6/TX/CK

25

16

RC1/T1OSI/CCP2

RC7/RX/DT

26

15

RC0/T10SO

RD4/PSP4

27

14

OSC2/CLKOUT

RC4/SDI/SDA

15

14

RC3/SCK/SCL

RD5/PSP5

28

13

OSC1/CLKIN

RC5/SDO

16

13

RC2/CCP1

RD6/PSP6

29

12

VSS

RC6

17

12

RC1/T1OSI

RD7/PSP7

30

11

VDD

RC7

18

11

RC0/T1OSO/T1CKI

VSS

31

10

RE2/CS/AN7

VSS

19

10

OSC2/CLKOUT

9

VDD

32

9

RE1/WR/AN6

VDD

20

OSC1/CLKIN

8

RB0/INT

33

8

RE0/RD/AN5

RB0/INT

21

VSS

22

7

RB1

34

7

RA5/AN4/SS

RB1

RA5/AN4/SS

23

6

RB2

35

6

RA4/T0CKI

RB2

RA4/T0CKI

RB3

36

5

RA3/AN3/Vref

RB3

24

5

RA3/AN3/Vref

RB4

37

4

RA2/AN2

RB4

25

4

RA2/AN2

RB5

38

3

RA1/AN1

RB5

26

3

RA1/AN1

RB6

39

2

RA0/AN0

RB6

27

2

RA0/AN0

1

1

RB7

40

MCLR

RB7

28

MCLR

(b) PIC16C74

(d)

PIC16C73

Fig. 10.1 Pinout for a variety of PIC family members.

the range 5 ±1 V in all but the high-speed crystal clock mode (4–10 MHz) where the range is restricted to 5 ± 0.5 V. The PIC16LF83/4 low-power variant, which is restricted to 2 MHz, can operate over the range 2–6 V. The PIC 12C5XX family has an allowable range of 2.5–5.5 V for up to 4 MHz.

The logic 0 output voltage VOL is 0.6 V maximum for low and a minimum output high voltage VOH of VDD −0.7 V. Input voltages generally are


10. The Real World 255

accepted as low VIL if below 0.16VDD. A high input VIH is usually accepted

as logic 1 if above 0.5VDD.2

All the devices shown in Fig. 10.1 have a quoted typical current consumption of:

•< 2 mA at VDD = 5 V clocked at 4 MHz;

15 µA at 3 V and 32 kHz;

•< 1 µA on standby in the Sleep state.

Many microcontroller applications are battery powered and in such situations power consumption is critical. These bare figures from the data sheets show a variation of 1:2,000,000 so it is important that the factors influencing current be understood.

The relationship between the PIC’s clocking frequency and current is graphed in Fig. 10.2. Clearly power dissipation VDD × IDD is directly proportional to operating frequency. For instance, one hundred times more current is required at 10 MHz as compared to 100 kHz.

To see why this is so, consider a switch charging and discharging a capacitive load C, as in Fig. 10.3. The switch is implemented by a transistor and the load is due to the stray capacitance of the connection to the next field-e ect transistor and its input gate. RS represents the resistance of the switching transistor.

When this capacitance is charged up to V volts (switch opens), 12 CV2 Joules of energy is stored. Energy is dissipated in the load by this charging current as follows:

Initial charging current (V

c

= 0) : i

o =

V/RL

t

Instantaneous current

: ic = ioe−

τ

t

t

Instantaneous power in RL

: ic2RL = io2RLe−2

t = (V2/RL)e−2

τ

τ

Total energy dissipated in RL

: E = V2/RL

0∞ e−2

dtt

τ

=

V2/R

τ e−2

τ

V

2

τ−

1

2

/ L( 2 )

2

2

0

=

L

=

R

CV

Thus in going high, 12 CV2 Joules are dissipated in the load resistance

(irrespective of its value RL!) and 12 CV2 Joules are stored in the capacitor’s electric field. On discharge, this stored energy is dissipated in RS//RL (once again irrespective of value). The energy dissipated in one switching cycle is then CV2 Joules. The total power is this figure multiplied by the number of cycles per second (CV2f ), plus any quiescent dissipation.

The preceding relationship CV2f shows that dissipated power is proportional to frequency for any given supply voltage. Furthermore, it is

2The main exceptions are the input to MCLR (Master CLeaR) which requires a voltage VIH of 0.85VDD before coming out of reset, and 0.7VDD for any oscillator driving the OSC1 input as an external clock.


256 The Quintessential PIC Microcontroller

Fig. 10.2 Typical supply current versus clocking frequency.

proportional to the square of the supply voltage, so halving VDD from 5 V to 2.5 V should quarter the power dissipation VDD × IDD.3

The dynamic power dissipation derived above should be added to that due to the quiescent current that the device consumes when the clocking rate is dropped to zero. In the case of the PIC16F84 this power-down current IPD is quoted in the data sheet as typically 1 µA with a VDD of 4 V and 16 µA worst case. The PIC12C5XX has equivalent values quoted at VDD of 3 V of 0.3 µA and 5 µA respectively.

Of course not clocking a digital circuit is rather unproductive. All PIC families feature a Sleep mode which e ectively turns o the internal clock oscillator. This switch is actioned in software using the sleep instruction. Once asleep the contents of the Data store are retained pro-

3This is why most current microprocessors used as the PC CPU, such as the Intel Pentium III, are powered at under 3 V rather than the standard 5 V of older devices.

10. The Real World 257

V

RL

Charge

(Switch open)

SW Discharge

(Switch closed)

C

RS

Fig. 10.3 Equivalent output circuit, where C represents both intrinsic and external load capacitance.

vided that the supply voltage remains above 1.5 V. The PIC can be awakened either by Resetting the device (see page 262), by an enabled interrupt from outside or if the enabled Watchdog timer overflows. If the Global Interrupt Enable mask (GIE) (see Fig. 7.4 on page 178) is clear then the processor will simply execute the instruction after sleep and continue on as normal. If GIE is set then after the instruction following sleep is executed, the processor will go to the Interrupt Service Routine as a normal interrupt response.

To ready the processor to be awakened by any specific external interrupt source; for example by a request on the RB0/INT pin, the appropriate local flag bit (INTF in this instance) must be cleared and the corresponding mask bit (INTE in this instance) must be set. Following the sleep instruction, the programmer must reset the interrupt flag.

When the processor executes a sleep instruction it will clear the PD (Power Down) bit in the Status register (see Fig. 4.5 on page 89) and the internal clock oscillator is turned o . If the Watchdog timer is enabled at that time then it will be cleared, including its prescaler, but will continue to run as it has its own private internal oscillator. At this time the TO (Time Out) flag will be set (i.e. no Time Out). All file register contents, including the various port settings, remain unchanged.

If an enabled interrupt occurs before the sleep instruction is executed; that is the interrupt flag is set on entry, then sleep is executed as a nop (No Operation). In this situation the PD bit will not be cleared, so the programmer can determine, if necessary, after a sleep instruction if the PIC really did go through an dormant period. The software can also determine if the processor was awakened by the Watchdog timing out, by checking to see if the TO bit in the Status register has been cleared. Normally in Watchdog-enabled applications, the sleep instruction is fol-


258 The Quintessential PIC Microcontroller

lowed by a clrwdt (CLeaR WatchDog Timer) instruction. Checking the appropriate Interrupt flag in the INTCON register will determine if the source of the awakening was an interrupt.

Whatever the source of the awakening there will be a delay of 1024 clock cycles fOSC before processing of the instruction following the sleep breakpoint. This is to ensure that the crystal clock oscillator has started up and stabilized. This oscillator startup delay, illustrated in Fig. 10.7, is not implemented if the PIC is using a resistor-capacitor clock mode of Fig. 10.4(b) and itemized in Table 10.2.

The power down current IPD is lower when the Watchdog timer is not enabled; for example; for the PIC16F84 IPD is quoted as typically 1.0 µA (16 µA maximum) and 7 µA (28 µA maximum) with the Watchdog timer disabled/enabled respectively. Figures are given for a VDD of 4 V I/O ports set to input and pins tied to either VDD or VSS (usually ground).

All members of the PIC family have an integral oscillator circuit which when completed with timing elements provide the internal clocking waveforms shown in Fig. 4.4 on page 87. The PIC12C5XX family have an optional internal RC timing elements giving a nominal 4 MHz clocking rate and allow the oscillator pins OSC1 and OSC2 to used as general-purpose parallel input/output lines – GP4 and GP5 in Fig. 10.1(a).

VDD

PIC

R1

PIC

sleep

C1

Internal

Internal

OSC1

OSC1

Mode

clock

clock

C1

sleep

XTAL

RF

OSC2

Clock

out

OSC2

fosc/4

C2

(a) Crystal/ceramic resonator

(b)

External

RC

Fig. 10.4 Typical oscillator configurations.

The PIC16CXXX family can be operated in one of four di erent oscillator modes. These are:

• LP (Low Power) for crystal timing elements below around 200 kHz, eg. a 32.768 kHz watch crystal.

XT for both crystals and ceramic resonators up to 4 MHz.

HS for high speed crystals and ceramic resonators above 4 MHz.

RC for low cost external resistor/capacitor timing elements.

The 12C5XX family members call the RC mode EXTRC (EXTernal RC) to distinguish this from the INTRC (INTernal RC) mode.