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

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

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

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

Добавлен: 15.06.2025

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

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

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

82 The Quintessential PIC Microcontroller

In terms of architecture, referring back to Figs. 3.1 and 3.2 on pages 42 and 44 respectively, the microprocessor is the central processor unit, whereas the microcontroller is the complete functioning computer-like system. As an example, consider the electronics of a car odometer monitoring system displaying total distance since manufacture and also a trip odometer. The main system input signal is a tachometer generating pulses on each rotation of the engine flywheel, which when totalized gives the number of engine revolutions – and the pulse to pulse duration could also give the road speed. Of course the actual road distance depends on the gearing of the engine, and thus we need to know which of the five gear ratios has been chosen by the driver at any time. This is shown as five lines G1…G5 originating from the gear box. One signal will be high for the appropriate forward gear, with neutral and reverse being ignored. Additional inputs are used to give a manufacturer’s option of a mile or kilometer display, and a user input to reset the trip display to zero.

The display itself consists of seven 7-segment digits (see Fig. 6.6 on page 148) to indicate up to (optimistically) .. As there are so many segments to control (49 in total), Fig. 4.1 shows the display data fed via a single digital line, shunted serially into a shift register – see Fig. 2.20 on page 36. A second line provides clock pulses for the register with 49 clock pulses being needed to refresh the display.15

The trip odometer display comprises four digits, which will record up to .. Similarly two output lines are used to feed and clock the shift register, and 28 clock pulses are needed to shift in a new 4-digit trip display.

The resource budget (list of subsystem functions) for this system is:

An edge-triggered input for the tachometer pulse train, connected to a counter/timer to totalize engine revolutions.

Seven static digital input lines to interface to the gear ratio, mi/km option and trip reset.

Four output digital lines to clock the two shift registers and provide segment data.

A microprocessor to do the calculations and to read/write to the input/ output ports respectively.

Program memory, usually ROM of some kind.

Data memory for temporary storage of program variables, usually static RAM.

Non-volatile storage for physical variables, such as total distance and distance since trip reset.

This functionality could be implemented onto a single integrated circuit, and in this situation would be known as a microcontroller, that is a mi-

15Many displays have this shift register built in as a complete subsystem.

4. The PIC16F84 Microcontroller 83

croprocessor integrated with its support circuitry giving a complete microcomputer function. Of course the resource budget listed above is specific to our example. Although the core functions (microprocessor and memory) are common to a wide range of applications, the input/output (I/O) interface needs to be tailored to the task in hand. Some typical I/O functions are:

I/O to interface to a serial bit stream of various synchronous and asynchronous protocols.

Counter/timer functions to totalize input events and to generate precision time-varying digital output signals.

Analog to digital multiplex/conversion to be able to read and digitize analog inputs.

Digital to analog conversion to output analog signals.

Display ports to drive multi-digit liquid crystal displays.

This alternative approach to using additional silicon resources led to the first MCUs in the late 1970s. For example the 35,000 transistor Motorola 6801, designed in response to a specific application from an car manufacturer, used the existing 6800 MPU as a core, with 2048 bytes of ROM program memory, 128 bytes of data RAM, 29 I/O lines and a 16bit timer. With the viability of the MCU approach vindicated, a range of families, each based on a specific core but with individual family members having a di erent selection of I/O facilities, was introduced by the leading MPU manufacturers. For example, the Motorola 68HC11 family (a development of the 6801 MCU) uses a slightly enhanced 6800 core. The 68HC12 and 68HC16 families use 16-bit cores but are designed to be upwardly compatible with the 8-bit 68HC11. It was quickly realised that many embedded applications did not even need the power of the (antique) 6800 core, and the 68HC05 family16 had a severely reduced core by lower price. Actually 4-bit MCUs outsold all other kinds of processor until the early 1990s and 8-bit MCUs, now the most popular, are likely to continue in this role for the foreseeable future.

All these MPUs and MCUs were based on the von Neumann architecture (see Fig. 3.1 on page 42) used by mainframe computers. The alternative Harvard architecture (see Fig. 3.2 on page 44), which is chiefly distinguished by having a separate memory space for program and data, originated at Harvard university for a US Defence department computing project, but was rejected in favor of a rival von Neumann design from Princeton university. The first MPU using this architecture was the Signetics 8X300, and this was adapted by General Instruments in the mid 1970s for use as a Peripheral Interface Controller (PIC) which was designed to be a programmable I/O port for their 16-bit CP1600 MPU.

16The 68HC05 has found a niche as the computing engine of smart cards, where highpower computing is not a priority.


84 The Quintessential PIC Microcontroller

General Instruments sold o their microelectronics division in 1988 to a start up company called Arizona Microchip Technology. Microchip’s main product was, and is still, a series of microcontroller families based on this PIC architecture. Their first family was introduced in 1989 with the PIC16C5X series. These Harvard processors are based on a set of only 33 instructions. All instructions are coded in a single 12-bit word. This use of a primordial instruction set is known as Reduced Instruction Set Computer (RISC) and contrasts with the Complex Instruction Set Computer (CISC) model used in most computers/MPUs where several hundred instructions/modes are provided, and because of their number take several memory words to encode. The combination of single-word instructions, the simplified instruction decoder implicit with the RISC paradigm and the Harvard separate Program and Data buses gives a fast, e cient and cost e ective processor implementation. The PIC16C5XX 12-bit core family features between 512 and 2048-instruction Program stores implemented as One-Time Programmable (OTP) EEPROM (see page 26), 25 to 73 bytes of Data memory, 12 or 20 I/O pins in the 18and 28-pin package respectively, and an 8-bit timer. The PIC12CXXX family are 8-pin equivalents.

By 1992 the PIC16CXXX family family based on a 14-bit core enabled easier addressing of larger Program spaces and additional peripheral devices, such as 16-bit timers and A/D converters as well as interrupt handling. The RISC instruction set is virtually identical to the 12-bit core, with a total of 35 instructions. The 16-bit PIC17CXXX core, introduced in 1997, has 58 instruction, with a multiplying ALU and further interface capabilities. It is complemented by the extended 16-bit core PIC18CXXX family introduced in 1999 with 77 instructions more oriented towards high-level language compiler needs.

Of the three families, the 14-bit core is a good compromise between low-cost and ease of use. The PIC16F84, which is the baseline exemplar of this book, is a member of this mid-range family.

From the software point of view all devices with the same core are identical. However, there is a di erent mix of I/O facilities from the hardware perspective, but with much commonalty. For example, the 16C74 supports an 8-channel analog input port, the PIC16C66 a synchronous serial port and the PIC16F84 a non-volatile data memory. All three devices have similar parallel I/O, timer and interrupt handling facilities.

The architecture of the PIC16F84 is shown in a simplified form in Fig. 4.2. Although initially this looks rather complex, it is little more than the architecture of our BASIC computer of Fig. 3.3 on page 45 but with interface ports connected to the internal File store data bus. You should revise this material now as background to our discussion. In essence the PIC family is based on a Harvard structure with its separate Program and Data store, and with peripheral interface ports mapped onto the Data file

4. The PIC16F84 Microcontroller 85

Vdd

14

Vss

5

MCLR

4

OSC1

16

OSC2

15

Program store

Flash ROM

1K x 14

Program 14 data bus

Pipeline

Instruction reg 1

Instruction reg 2

op-code 7

Instruction decoder & control

Q1 Q2 Q3 Q4

Oscillator and timing generation

Power-up

timer

Oscillator

start-up

Power-on

Reset

Watchdog

timer

Program

PIC16F84

EEPROM data memory

address

bus

F 02h

File

data

bus

F 08h

Program

Counter

EEPROM data

13 PLATH

8

8

F 0Ah

RAM

Data

store

EEPROM

Register file

data memory

68 x 8

8-level

stack

64 x 8

8

File

F 09h

address bus

EEPROM address

Address mux

7

Direct address

8

Indirect

Timer

address

F 04h

File

select reg.

8

Literal data

8 File value

01hF counterTimer 81hF Prescaler

Working reg.

8

Data mux

8

8

F 03h

I/O port A

ALU

RP0

Z DC C

Status

register

8

d

bit

SLEEP

from instruction

01

F 0Bh

TRISA

F

A Port

F

Int control

85h

05h

F 86h

I/O

TRISB

port

Port

B

F 06h

B

RB0/INT 6

RB1 7

RB2 8

RB4 10 RB3 9

RB5 11

RB6 12

RB7 13

RA4/TOCKI

3

RA3

2

RA2

1

RA1

18

RA0

17

Fig. 4.2 Architecture of the PIC16F84 microcontroller

store address space. That is the various ports appear to the software to be in the Data store. In more detail we have:

Central Processor Unit

As a consequence of the Harvard architecture, the CPU is split between the fetch and execution function, both of which operate in parallel with a minimum of interaction.

Fetch

The fetch section comprises a 13-bit Program Counter (PC) addressing the Program store via the Program address bus, and a two-deep Instruction pipeline through which 14-bit instructions via the Program data bus progress through to the Instruction decoder – see Fig. 3.3 on page 45.

The Program Counter is actually located in the Data file store at location File 2 and is labelled as PCL (Program Counter Low byte) in Fig. 4.6.


86 The Quintessential PIC Microcontroller

This means that it can be accessed and manipulated by the software in the same manner as any other file register. For example, if the contents of the Working register were n, then the instruction addwf 2,f (see page 49) overwrites File 2 (i.e. the PC) with its original value plus n – that is skip forward n places. A practical example is given in Program 6.4 on page 149.

There is one problem with this example, which arises because the PC is actually 13-bits wide and File 2 only holds the lower eight bits, PC[7…0]. The upper five bits, PC[12…8] are held in a ‘buried’ register; that is not directly accessible to the programmer. Actually any instruction that directly writes to File 2, such as our example above, does change all 13 PC bits as shown in Fig. 4.3. Not only will the 8-bit outcome of the instruction addwf 2,f be placed in the lower byte of the PC but the lower five bits of the PC bu er register at File 0Ah, labelled PCLATH (for Program Counter LATch High byte) in Fig. 4.3, are automatically copied into the high byte of the Program Counter. The PCLATH file data register is cleared when the PIC is reset and so an instruction like our example will usually result in an address in the first 256-byte ‘page’ of program memory unless PLATH is loaded with a non-zero value. Thus care needs to be taken when altering the state of the PC by writing to PCL in this manner; especially if the outcome overflows its 8-bit field. Instructions that indirectly alter the value of the PC, such as goto, will also use part of the contents of PCLATH in updating the PC. Such instructions carry an 11-bit address as part of the instruction code. Here bits PCLATH[4:3] are moved over to the corresponding PC bits 12 & 11 to give a complete 13-bit PC update

– see Fig. 5.4 on page 114.

Associated with the Program Counter is an area of buried storage that can stack up to eight copies of the PC. The current value of the PC is pushed into this stack when a subroutine is called or an interrupt is serviced. Conversely a return from a subroutine or interrupt causes the

PCL

12

87

0

Program Counter

File 02h

Outcome from ALU.

For example,

PCLATH

movwf

PCL

or

7

4

0

addwf

PCL,f

File 0Ah

Fig. 4.3 Showing how all 13 bits of the Program Counter are altered when writing to PCL.


4. The PIC16F84 Microcontroller 87

last stacked PC value to be popped out again into the PC. Details are given in Chapters 6 and 7.

The PIC microcontrollers have an integral oscillator that generates the internal timing sequences. The oscillator frequency fosc is normally controlled by an external crystal (or ceramic)-capacitor network across the device OSC1 and OSC2 pins – see Fig. 10.4 on page 258. A resistorcapacitor connected to OSC1 may be used as the timing elements where lower precision and frequency stability is not an issue. In this case OSC2/CLKOUT outputs a signal of frequency 14 × fosc. Alternatively an external oscillator can be used as the master clock into OSC1/CLKIN. The

PIC16F84 has a maximum frequency fOSC of 10 MHz but there is no minimum. As we shall see (Fig. 10.2 on page 256) the lower the frequency

the smaller is the power consumption. Unless otherwise stated, we will assume a fosc of 4 MHz for the rest of the text.

The internal oscillator/clock circuitry must be configured to the appropriate mode, as described in Fig. 10.5 on page 261. This is normally done when code is blasted into the Program store flash EEPROM. When the PIC is powered up (external supply voltage is applied) a 72 ms internal reset pulse is generated by the Power-up timer after the supply rises above approximately 2 volts, followed by 1024 clock pulses, counted by the Oscillator Start-Up timer to ensure that the internal oscillator has stabilised. This latter guard timer only operates for crystal-type oscillator modes – see Table 86 on page 258. The Power-Up timer does not operate if an external reset is applied to the Master CLeaR (MCLR) pin.

fosc

Q1

Q2

Q3

Q4

Program stream

Fetch inst. n

Fetch inst. n+1

Fetch inst. n+2

Fetch inst. n+3

Execute stream

Execute inst. n

Execute inst. n+1

Execute inst. n+2

Cycle n

Cycle n+1

Cycle n+2

Cycle n+3

Time

Fig. 4.4 Internal clock sequencing waveforms.


88 The Quintessential PIC Microcontroller

The clock input/crystal frequency at the OSC1 pin is divided by four to generate four internal non-overlapping quadrature clocks, as shown in Fig. 4.4. The clock-related sequence of operations in the fetch unit are: Q1: Increment the Program Counter and copy onto the Program store

address bus.

Q4: Read the instruction code o the Program store data bus into Instruction register 1 and at the same time move the previous instruction down the pipeline into Instruction register 2, where it is presented to the Instruction decoder.

Execute

The execution circuitry is centered around the Arithmetic Logic Unit (ALU) – see Fig. 2.19 on page 35. The ALU processes data from up to two sources. One of these is the 8-bit Working register. The other can be multiplexed either from a file in the Data store or an 8-bit literal, which is part of the instruction code – see page 107. For example addwf 20h,w and addlw 5 respectively add the contents of W to that of File 20h or the constant 5 to W. The outcome can be switched back into W (eg. addwf 20h,w) or into the register file (eg. addwf 20h,f) as controlled by the single Destination bit in the instruction code – see page 107.

Where an operand is a file, the execution unit can generate the Data store address in one of two ways – see Fig. 4.6.

Directly via a 7-bit address field in the instruction – see page 107. Seven bits can only directly address up to 128 files. This can be increased to 256 files if the state of the RP0 (Register Page bit 0) bit in the Status register (see Fig. 4.5) is also multiplexed in as part of the Data store address. Where RP0 is zero, then register files 00 – 7Fh can be addressed, this address page being known as Bank 0. With RP0 set to one, then

Bank 1 is addressed, ranging from 80 – FFh; see Fig. 4.6. RP0 is cleared when the PIC is reset and the Bit Set File and Bit Clear File instructions may be used alter its state. For example, bsf 3,5 sets bit 5 (RP0) in the Status register –see Fig. 4.5.

Indirectly using the File Select Register in conjunction with Indirect addressing, as described in Fig. 3.6 on page 57. In this situation the 8-bit address in the FSR is used to address the Data store whenever the virtual location File 0 is addressed. Here potentially all 256 files in both banks can be addressed irrespective of the state of RP0.

The three flag bits in the Status register STATUS are associated with the ALU, giving status information concerning the outcome from an instruction, as shown in Fig. 4.5.

Carry flag

Bit 0 of the Status register is the C flag. This primarily holds the Carry out from the last addition operation. Subtraction operations activate this bit as the complement of the Borrow out. For example, 24 − 12 = 12B1 and

4. The PIC16F84 Microcontroller 89

12 − 24 = 88B0. C also functions as an input/output bit for the Rotate instructions, as shown in Fig. 3.7 on page 61.

The label R/W ? in Fig. 4.5 indicates that this bit can be read from or written to and has an indeterminate value on a power-up reset – its value does not alter on any other type of reset.

Digit Carry flag

Bit 1 of the Status register is the DC flag. This operates in the same manner as the standard C flag but holds the Carry out from the lower nybble to the upper nybble; that is from bit 3 to bit 4. In the same manner DC holds the complement of the Borrow out from bit 3 to bit 4. Knowledge of the Carry activity between the lower and upper halves of the byte is useful where binary coded decimal data is being manipulated. Here each nybble holds a 4-bit representation of the decimal digits 0…9 (see page 6) and the half carry then indicates carries between decimal decades.

Zero flag

Bit 2 of the Status register is the Z flag. This is set whenever the outcome of the instruction is zero, otherwise it is cleared.

Status register (SR) File 3

7

6

5

4

3

2

1

0

RP0

TO

PD

Z

DC

C

(R/W 0)

(R/W 0)

(R/W 0)

(R 1)

(R 1)

(R/W ?)

(R/W ?)

(R/W ?)

Register

File Bank Select

Carry/Borrow

Bank 0

(File 00 - 7Fh)

0

Digit Carry/Borrow

Bank 1

(File 80 - FFh)

1

Zero

Time-Out

Watchdog time-out 0

clrwdt/sleep instructions

1

Power Down

By sleep instruction 0

By clrwdt instruction 1

Fig. 4.5 The PIC16F84 Status register

Unlike most MCUs, there are no instructions to specifically clear or set a flag, such as sec for SEt Carry.17 However, as the Status register is accessible as a file in the Data store, then any instruction that can alter the contents of a file can potentially change the state of a flag. There is a potential problem in that many of these instructions a ect one or

17For example the Motorola 6800/5/11 families.