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

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

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

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

Добавлен: 15.06.2025

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

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

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

90 The Quintessential PIC Microcontroller

more flags (see Table 3.1 on page 53) as part of their execution logic and this overrides any change that would result from the outcome of the instruction’s execution. For example, clrf 3 actually sets the Z flag to 1. The Bit Clear File and Bit Set File instructions are recommended where an individual bit in the Status register needs to be altered, as these instructions do not inherently a ect these flags. For instance, bsf 3,0 (Set Bit 0 in File 3) is equivalent to sec and bcf 3,2 (Clear Bit 2 in File 3) is equivalent to clz.

STATUS also holds the RP0 bank switching bit. The TO and PD readonly bits shown in Fig. 4.5 give information on what type of reset last occurred (Power-Up when power was applied to the device, Watchdog when the Watchdog timer timed out or External by bringing the MCLR pin low) or if awakened from the sleep instruction. As these are designated as read-only, they cannot be altered as part of the software, only monitored. These status bits will be discussed in Part 3 of the book.

In the normal Harvard manner, the execution unit is separated from the fetch unit, with distinct data bus, address bus and stores. It is of course controlled via the Instruction decoder which is fed from the bottom of the pipeline in the fetch unit. The fetch Program Counter is in the Data store’s address space so the execution unit can e ect the fetch sequence by altering the Program Counter, as shown in Program 6.4 on page 149.

The execute unit is also sequenced by the same four clock phases as the fetch unit operating in parallel.

Q1: Decode instruction. Q2: Read from Data store. Q3: Process data in ALU. Q4: Write into Data store.

Program store

The majority of PIC devices use EPROM for program memory. As EPROM can only be erased using UV radiation (see Fig. 2.11 on page 27) once the software has been programmed into the Program store it can be considered e ectively permanently in situ. Such devices are known as One-Time Programmable (OTP). Where it is likely that program code will need to be subsequently altered, the MCU may be housed in a ceramic package with a quartz window, allowing for erasure in around 20 minutes.

An alternative approach is to implement the Program store using EEPROM technology – see page 28. This allows the ‘fixed’ data to be erased electrically without the expense of a UV transparent package and the time delay inherent with this technology. Using this approach, code can even be reprogrammed in the field, to subsequently upgrade software, without the device having to be removed from the circuit board. Thus, say, a modem’s algorithm or a PC’s BIOS can be upgraded over a computer network by the user. Microchip’s strategy is to substantially increase the

4. The PIC16F84 Microcontroller 91

number of EEPROM devices but at the time of writing (2000) most devices are EPROM based.

The 16F84 holds its program code in an internal flash EEPROM (see page 28) memory18 holding 1024 (210) instructions, each of 14-bits width. This memory is accessed from the fetch unit via the 14-bit Program data bus into the pipelined Instruction register 1, and is addressed via the Program address bus by the lower ten bits of the Program Counter PC[9…0]. The address range is 000…3FFh. As the Program Counter is 13 bits wide, other members of the 14-bit core family can potentially interact with a 213 = 8 kbyte-instruction Program store; for example, the PIC16F876/7

– see Fig. 15.4 on page 440. The structure of 14-bit PIC instructions are discussed in Chapter 5.

All members of the mid-range PIC family use address 000h as the Reset vector (the place the PIC goes to when it is reset, for the startup of the program) and 004h for the Interrupt vector – the place the PIC goes to whenever it gets an interrupt request.

Data store

The Data store comprises 81 8-bit locations known as file registers or just files for short. The contents of any location in the Data store may be moved into or out of the Working register. The PIC16F84’s file registers are located in the Data store’s memory map as shown in Fig. 4.6. The registers can be categorised as Special-Purpose Registers (SPRs) used by the core CPU and peripheral modules for status information and controlling the desired operation of the device. The remaining implemented General-Purpose Registers (GPR) can be used by the programmer for temporary storage of program variables.

There are two ways an instruction can target a datum in the Data store. Each file register has an address, which is listed in Fig. 4.6. For example PORTA is located at File 05.

Directly

Any instruction which can process a datum in the Data store can directly specify the e ective address using seven binary bits which are part of the binary program code, the details of which are given on page 107. By itself this can address a base range of 00–7Fh. However, we see from Fig. 4.6 that this address is augmented by the RP0 bit19 in the Status register to give an e ective 8-bit address. If RP0 is 0 (as it is after reset) the range is 00–7Fh, that is Bank 0. If RP0 is made 1 then the range is 80–FFh; that is

18The older PIC16C84 uses normal EEPROM but otherwise has the same Program store architecture. However, the Data store is rather smaller.

19Actually the Data store model for the mid-range PICs is based around four 128-byte

banks with STATUS[6:5] holding two bank select bits RP1:RP0 (see Fig 5.1 on page 109) to give an e ective maximum capacity of 29 = 512 register files. Devices such as the PIC16F87X lines implement the complete model. The 8-bit file address for File Indirect memory access is augmented by bit STATUS[7], known as IRP (Indirect Register Page). This gives two 256-byte Indirect pages.


92 The Quintessential PIC Microcontroller

Direct addressing

Indirect addressing

d0

d6

RP0

File address

00h

IND

IND

01h

TMR0

OPTION

02h

PCL

PCL

03h

STATUS

STATUS

04h

FSR

FSR

05h

PORTA

TRISA

06h

PORTB

TRISB

07h

Unimplemented

Unimplemented

08h

EEDATA

EECON1

09h

EEADR

EECON2

0Ah

PCLATH

PCLATH

0Bh

INTCON

INTCON

0Ch

Images of the

68

68

General Purpose

General Purpose

register files

register files

4Fh

50h

Access

Unimplemented

Unimplemented

File 0 (IND)

File 4

7Fh

Register

d0

Select

File

d7

File address

80h

81h

82h

83h

84h

85h

86h

87h

88h

89h

8Ah

8Bh

8Ch

CFh

D0h

FFh

Fig. 4.6 Data store memory map.


4. The PIC16F84 Microcontroller 93

Bank 1. For example, to set the contents of File register TRISB (File 86h) to all 1s and then to read the contents of the File register PORTB into the Working register, we need the following program:

PORTB

equ

06

;

Register file in Bank 0

TRISB

equ

86h

;

Register file in Bank 1

STATUS

equ

03

;

Status register

RP0

equ

5

;

RP0 is bit 5 in STATUS

; ---------------------------------------------

bsf

STATUS,RP0;

Set RP0 to 1 -- select Bank 1

movlw

0FFh

;

Bit pattern 11111111b

movwf

TRISB

;

Sent to TRISB

bcf

STATUS,RP0;

Select Bank 0

movf

PORTB,w

;

Read the state of PORTB

Indirectly

If the Indirect address mode is used, as was described in Fig. 3.6 on pageref3:fig-Indirect, then the 8-bit address in the File Select Register (FSR) is used as the e ective. As we have an 8-bit address in this situation any location in the two banks are accessible with not bank switching required. The same example as described above is then implemented as:

PORTB

equ

06

;

Register file in Bank 0

TRISB

equ

86h

;

Register file in Bank 1

STATUS

equ

03

;

Status register

RP0

equ

5

;

RP0 is bit 5 in STATUS

; ---------------------------------------------

movlw TRISB

;

Set up the pointer to locate TRISB

movwf FSR

movlw 0FFh

;

Bit pattern 11111111b

movwf INDF

;

Sent to TRISB

bcf

STATUS,RP0;

Select Bank 0

movf

PORTB,w

;

Read the state of PORTB

movlw

PORTB

;

Set up the pointer to locate Port B

movwf

FSR

movf

PORTB,w

;

Read the state of PORTB

Although there is no Bank switching required, this code segment is actually longer than the previous solution! However, Indirect addressing is useful when one location in Bank 1 requires frequent access.

The bottom 12 locations of both banks are reserved for SPRs. Although the exact location can vary across members of the mid-range family, common registers; for example, PCL and PORTA tend to have the same location. For instance, see Appendix B.

Of these, we have already met most of those involved with the core function:

INDF

The INDirect File at File 0 is not physically implemented as a register. Instructions accessing this virtual location actually put the contents of the FSR onto the Data store address bus, as described in Fig. 3.6 on page 57.


94 The Quintessential PIC Microcontroller

PCL

The Program Counter Low byte is addressed as File 2. Its relationship with the total 13-bit PC is described on Fig. 4.3.

STATUS

The Status register can be accessed in File 3. As can be seen from Fig. 4.5, this file holds the three code condition bits plus several status bits and the Data store page bit RP0.

FSR

The File Select Register at File 4 holds the indirect address used when the instruction refers to the virtual INDF address.

PCLATH

File 0Ah holds the LATch High byte for the Program Counter, as described in Fig. 4.3.

INTCON

The INTerrupt CONtrol register at File 0Bh holds the mask and status bits controlling the response of the MCU to interrupts. Its operation is described in Chapter 7. Most devices have interrupt-related bits in other registers – for example see Fig. 14.10 on page 408.

All these core SPRs are images in both memory banks.

The remaining nine SPRs relate to the configuration and control of the various peripheral interface devices. More details will be given on individual peripheral SPRs in Part 3 of the book in the appropriate chapters.

The 68 GPRs are located from File 0Ch through File 4Fh and are mirrored in Bank 1.20 Thus the instruction clrf 4Fh and clrf 0Cfh are identical and target the same physical location irrespective of the state of the Register Page bit RP0 setting. The remaining file locations are not implemented and read as 00h, as does location File 07h/File 87h – which is reserved for PORTC and TRISC in devices with 28+ pins.

Peripheral functions

Each member of the PIC family has its unique set of integrated peripheral devices. However, all PICs have parallel input/output and timer facilities. As well as these standard facilities, the PIC16F84 has a peripheral 64byte EEPROM which can be used as a small data store not dependent on continuous power to retain its contents; i.e. non-volatile.

Each of these peripheral facilities are described in detail in Part 3 of the book, but for completeness are briefly cataloged here together with their associated SPRs. These registers are used to configure the function of their target peripheral interface, to control and monitor their status.

Parallel input/output

The ability to externally alter or monitor several digital lines at the same time is a virtually universal facility on microprocessor-based systems.

20This GPR mirroring is not as a rule a feature of the more sophisticated members of the family.

4. The PIC16F84 Microcontroller 95

Apart from the 8-pin PIC12XXX series, all PICs have a minimum of 12 such external input/output lines. Some have much more, such as the 40-pin PIC16C74 which has 33 I/O lines.

The PIC16F84 has 13 I/O lines, divided up into two ports. Port A has five I/O lines mapped into the Data store address space at File 5. The remaining eight lines are allocated to Port B at File 6. These ports can be thought of as a ‘window’ into the Data store in that data written to File 5 or File 6 appear to the outside world on the corresponding pins; pins RA4…RA0 and RB7…RB0 respectively – see Fig. 10.1 on page 254. However, the electrical and logic behavior of these ports is more complex than that of a purely internal register file. This will be discussed in Chapter 11 but as an example a port bit must be configurable as either an output (so that the CPU can control the state of the associated pin) or an input (so that the CPU can read the state of this pin). To do this, each Port register has an associated Data Direction register, which Microchip call TRISA and TRISB, which map to File 85h and File 86h respectively (the term TRIS stands for TRIState – see Fig. 11.2 on page 273. These registers lie in the less convenient Bank 1 as they are usually set up at the beginning of the program and never subsequently altered.

As an example, consider that we wish to make Port B bits 6…0 an input and bit 7 an output. Then the setting up code would be:

STATUS

equ

03

; Status register is at File 03

RP0

equ

05

; Bank switch bit is 5 in STATUS

TRISB

equ

86h

; Data Direction register at File 86h

PORTB

equ

06

; Port B itself is at File 06

bsf

STATUS,RP0

; Bank 1

movlw

7Fh

; Binary pattern 0111111

movwf

TRISB

; makes RB7 output, RB6...RB0 input

As an example, subsequently pin RB7 can be pulsed high and then low as follows:

bsf

PORTB,7

; Pin RB7 high (set bit 7)

nop

;

Delay

a short time

nop

;

by putting in a few NO Operations

bcf

PORTB,70

; then

low (clear bit 7

with the assumption that the CPU is still in Bank 0. The registers associated with parallel I/O are:

PORTA, File 05h

Only the lower five bits are implemented in this register file, feeding through to pins RA4…RA0. Pin RA4 is shared with the Timer peripheral. The upper three bits read as zero.


96 The Quintessential PIC Microcontroller

TRISA, File 85h

This is used to bitwise configure Port A bits as input or output. Setting TRISA[n] to 1 sets bit PORTA[n] as an input and to 0 as an output. Any type of reset sets the TRIS bits to 1 and the associated port bits to input.

PORTB, File 06h

A bi-directional 8-bit port connected to pins RB7…RB0. Bit RB0 doubles as a hardware interrupt input.

TRISB, File 86h

This is used to bitwise configure Port B bits as input or output. Details are the same as TRISA.

Timer

Most MCUs have facilities to either measure elapsed time and/or to generate digital on/o waveforms with well defined durations. This is normally based around one or more counters that are incremented either from an external pulse or internal clock. For instance, if an automatic packing machine needs to count cans of beans going along a conveyer belt, then a photoelectric-based transducer could act as the timer input. If a new packing carton needed to be in place every 24 cans then an internal 8-bit counter would be set to E8h (−24). When the counter overflows from FFh to 00h then an interrupt (see Chapter 7) would be generated and the MCU then take the appropriate action.

All PIC MCUs have at least a basic timer/counter known as Timer 0 (TMR0). The read/write TMR0 counter register at File 1 can be clocked from the outside world via the T0CKI (Timer 0 ClocK In) pin, which is shared with the RA4 Port A pin. Alternatively, the incrementing source can be the internal Q4 phase clock, which is one quarter of the crystal frequency. For example, for a 4 MHz crystal this is 1 MHz. Either clock source can be frequency divided by a buried 8-bit prescale counter. This divide ratio is controlled by the lower three PreScale bits of the OPTION_REG register at File 81h (see Fig. 13.2 on page 363), labelled PS2:PS1:PS0. The ratio is then 2PS+1. For example, if PS[2 : 0] = 111 then

the counter will increment at 256f , where f is the clock source frequency. The prescaler can be disconnected by setting bit 3 of OPTION_REG to 1. This will give a direct connection between pulse source and counter. Writing to the Timer 0 register also zeros the Prescaler counter (for example movlw 0F8h, movwf 1) enabling the time period to begin from true zero time.

When this PSA (Pre-Scale Assignment) bit is 1 the prescaler becomes a postscaler to the Watchdog timer – see Fig. 13.1 on page 362. The Watchdog timer is designed to reset the MCU unless periodically preset by the user’s program with the instruction clrwdt (CLeaR WatchDog Timer). This ensures that the PIC will eventually reset if due to an electrical disturbance or a software bug, the processor malfunctions, perhaps

4. The PIC16F84 Microcontroller 97

by jumping into an unprogrammed part of the Program store. This will disrupt the periodic preset. If the prescaler is assigned to the Timer then the Watchdog timer will periodically time-out (count down through zero) after approximately 18 ms.21 With PSA set to 1 then 2PS 18 ms Watchdog time-outs are required before the processor is reset. Thus, with PS[2 : 0] = 111, 27 = 128 time-outs gives a period to MCU reset of nominally 2.3 s. Thus the software must use the clrwdt instruction before this period elapses to prevent reset. This instruction also clears the Prescale counter. If it does time-out, then the TO bit in the Status register will be cleared. If desired the Watchdog timer may be disabled at the same time as code is programmed into the Program store. Various configuration bits (known as fuses) are located by the flash EEPROM programmer in location File 2007h (see Fig. 10.5 on page 261), which is not accessible during the normal run mode. Such details are normally hidden to the operator by the EEPROM programmer’s operative software.

Registers relating to Timer 0 are:

TMR0, File 1

Sometimes known as the Real-Time Clock/Counter (RTCC), is an 8-bit upcounter register that keeps tally of clock events. It may be preset to any byte value by moving data from W and read at any time. When it overflows from FFh to 00h it sets the T0IF (Timer 0 Interrupt Flag) in the INTCON (INterrupT CONtrol) register – see Fig. 7.4 on page 178. This may be used to generate an interrupt.

OPTION_REG, File 81h

Six bits in this register in Bank 1 at File 81h are used in conjunction with the timer – see Fig. 13.2 at page 363.

PS2, PS1, PS0 at bits 2,1,0 respectively control the prescale ratio 2PS−1 for the timer or postscale ratio 2PS for the Watchdog timer.

T0SE (Timer 0 Set Edge) at bit 4 allows the programmer to select which

edge of a pulse at the T0CKI pin will increment the counter; a 0 for / and 1 for \ .

• T0CS (Timer 0 Clock Select) at bit 5 allows the programmer to select the clock source as either the internal clock (= 0) or a transition at the

T0CKI pin.

The remaining two bits configure external interrupt edge select and electrical properties of Port B inputs.

Data EEPROM

The PIC16F84 has a block of 64 bytes of data that does not require power to retain its contents. This non-volatile memory is not part of the (volatile) Data store and is accessed through SPRs as a peripheral device. Any byte can be addressed and then read from or written to via the

21Time-out is very nominal due to process variations, 7 – 33 ms, and in addition is rather dependent on supply voltage and temperature.