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

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

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

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

Добавлен: 15.06.2025

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

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

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

434 The Quintessential PIC Microcontroller

EECON1 register

7

6

5

4

3

2

1

0

EEIF

WRERR WREN

WR

RD

EECON1

(R/W 0)

(R/W X) (R/W 0)

(R/S 0)

(R/S X)

File 88h

ReaD control

WRite control

Interrupt

WRite ENable

to CPU

WRite ERRor

R

= Read

6

INTCON

W = Write

S

= Settable

EEIE

File 0B h

( )

= Reset condition

Fig. 15.2 The PIC16F8X EECON1 register.

EEPROM CONtrol register 2 EECON2

This register located at File 89h is not physically implemented – it always reads as zero. Rather the action of writing the successive code pattern 01010101 → 10101010 with no interruption is used to unlock the Write cycle. This arcane incantation is deliberately designed to convolute the process as security against unintended alterations in the data.

In order to read a specified datum from the EEPROM module we have to implement software to execute the task list:

1.Copy the target cell’s address to EEADR.

2.Set RD to 1 to initiate the Read cycle.

3.RD is automatically cleared immediately and the target 8-bit datum can be read from EEDATA any time from the next instruction cycle as convenient.

Program 15.1 Retrieving a byte from the EEPROM Data module.

; ************************************************************

; *

FUNCTION:

Gets one byte from the EEPROM Data module

*

;

*

ENTRY

:

Address in

EEADR

*

;

*

EXIT

:

Datum in W

and in EEDATA. System in Bank0

*

; ************************************************************

EE_GET bsf

STATUS,RP0

; Change to Bank1

movlw

b’00000001’

; Set RD for Read cycle

movwf

EECON1

; Read datum into EEDATA

bcf

STATUS,RP0

; Back to Bank0

movf

EEDATA,w

; Copy into W

return

; for return


15. To Have and to Hold 435

Subroutine EE_GET in Program 15.1 directly implements this process and illustrates the return of the datum from the EEPROM cell to the Working register. The datum will remain in EEDATA until the register is reused.

Writing data to the EEPROM Data module is deliberately made more Byzantine to reduce the chance of a spurious Write corrupting the data due to a software bug or processor malfunction because of, say, a power glitch. The task list to write a datum to a specified cell is:

1.Copy the target cell address to EEADR.

2.Set WREN in EECON1[2] to enable the Write process.

3.Disable all interrupts.

4.Send 55hto EECON2.

5.Send AAhto EECON2.

6.Set WR to initiate the Write cycle.

7.Clear WREN.

8.Enable interrupts.

9.Wait until WR returns to zero, signalling the completion of the Write cycle, and exit.

The Write cycle will not initiate if the interlock sequence items 4 – 6 is not exactly followed without interference. For example; in an interruptdriven system an interruption during the interlock sequence will abort the Write cycle. Thus in this situation interrupts should be disabled by clearing GIE until the Write cycle has been initiated, otherwise this step can be omitted.

If desired the completion of the Write cycle can be used to interrupt the processor. This is enabled by setting the EEIE mask bit in INTCON[6]. When the interrupt flag EEIF, located in EECON1[4], is set in the normal way then the interrupt is generated. It should be cleared in the ISR.

It is possible that the processor is reset, for example by a Watchdog overflow, before the Write cycle is complete. In this situation, the EEPROM datum may be corrupt. The WRERR flag in EECON1[3] will be set if the Write operation has been prematurely terminated with a Reset action. If this is not the case, when the cycle is complete the datum may be read back and verified to give extra security. The WREN bit may be cleared at this point to help prevent an accidental Write. Doing this before the Write is complete will not a ect the operation.

Program 15.2 implements this task list. Both EEDATA and EEADR are set up by the caller program with the byte data and address. The subroutine is not pulled out until the Write cycle has completed; typically 4 ms. This ensures that these SPRs will not be altered during the cycle which may possibly give an erroneous outcome.

In order to illustrate these concepts we will repeat Example 12.3 on page 351 replacing the external serial EEPROM with the internal module. We will assume that the odometer count is located at EEPROM cells 10

– 12h.

436 The Quintessential PIC Microcontroller

Program 15.2 Putting a byte into the EEPROM Data module.

; ************************************************************

; *

FUNCTION:

Writes one byte into the EEPROM Data module

*

; *

ENTRY

:

Datum byte in EEDATA, module address in EEADR

*

;

*

EXIT

:

Interrupts disabled for 9 instructions

*

;

*

EXIT

:

System in Bank0

*

; ************************************************************

EE_PUT

bsf

STATUS,RP0

;

Change to Bank1

bsf

EECON1,WREN

;

Enable for Write cycle

EE_LOOP

bcf

INTCON,GIE

;

Disable all interrupts

btfsc

INTCON,GIE

;

Check, did it clear?

goto

EE_LOOP

;

IF not THEN do again

movlw

55h

;

Now do the interlock

movwf

EECON2

movlw

0AAh

movwf

EECON2

bsf

EECON1,WR

;

Initiate the Write cycle

bcf

EECON1,WREN

;

Optionally disable any other Writes

bsf

INTCON,GIE

;

Re-enable interrupts

EE_EXIT

btfsc

EECON1,WR

;

Check, has the Write completed?

goto

EE_EXIT

;

IF not THEN retry

bcf

STATUS,RP0

;

Go back to Bank0

return

;

and return when cycle has finished

The coding shown in Program 15.3 makes use of the two subroutines EE_GET and EE_PUT to read and subsequently write the three odometer bytes from the EEPROM Data module. The address of the first (highest) byte is copied into EEADR at the beginning of the subroutine and is subsequently incremented and decremented in situ to point to the appropriate datum.

Once the 3-byte odometer state has been fetched and copied into memory it is incremented in exactly the same manner as in Program 12.15 on page 354. The augmented array is then written back into EEPROM in the opposite sense as it was read, with EEADR being decremented. The EE_PUT subroutine checks that the Write cycle has been completed before returning and thus timing need not be checked by the calling program.

As well as altering data under program control it is possible to initialize the state of the EEPROM Data module when the executable program is being externally blasted into the Program memory; as illustrated in Fig. 10.5(a) on page 261. The area of Program memory beyond the user Program store belongs to the special test/configuration memory space 2000h– 30FFh and can be accessed only during external programming. In Fig. 10.5(b) we observed that the Configuration fuse word is located at 2007h. The EEPROM module also lies in this space located at 2100h– 21FFh. For example, to store the value of sine every 10◦ between 0◦ and 90◦ as part of the program source code we have:


10h EEADR EE_GET MSB EEADR,f EE_GET NSB EEADR,f EE_GET LSB
; Address of high-byte odometer total ; Copy into EEPROM address register ; Read byte from EEPROM module
; and put into file register MSB ; Address of middle byte odometer ; Read byte from EEPROM module
; and put into file register NSB ; Address of low byte odometer
; Read byte from EEPROM module
; and put into file register LSB

15. To Have and to Hold 437

org

2100h

; The EEPROM Data module

SINE de

0, 2Ch, 57h, 7Fh, 0A4h, 0C4h, 0DDh, 0F0h, 0FBh, 0FFh

where the assembler directive de (Data EEPROM) specifies the comma delimited list of data. Once the PIC has been programmed, the contents of the EEPROM Data module will look like Fig. 15.3.

Program 15.3 Incrementing the non-volatile odometer count in Data EEPROM.

;************************************************************

;FUNCTION: Adds one onto the triple-precision odometer total*

; RESOURCE:

Subroutines

EE_GET and

EE_PUT

*

; ENTRY

:

Current total in EEPROM module at 10:11:12h

*

;

EXIT

:

Incremented

total

back

in EEPROM module

*

;

EXIT

:

also available in

RAM at LSB:NSB:MSB

*

; ************************************************************

EXTRA_MILE movlw movwf call movwf incf call movwf incf call movwf

; Now increment 3-byte array

incf

LSB,f

; Add one

btfss

STATUS,Z

; Is it now zero

goto

PUT_BACK

; IF not THEN continue

incfsz

NSB,f

; Increment middle byte

goto

PUT_BACK

; IF not zero THEN continue

incf

MSB,f

; Put the augmented odometer count back in Data EEPROM

PUT_BACK movf

LSB,w

; Get new odometer low byte

movwf

EEDATA

; Put in EE Data register

call

EE_PUT

; Write to EEPROM cell 12h

decf

EEADR,f

; Address of middle byte

movf

NSB,w

; Get new odometer middle byte

movwf

EEDATA

; Put in EE Data register

call

EE_PUT

; Write to EEPROM cell 11h

decf

EEADR,f

; Address of high byte

movf

MSB,w

; Get new odometer low byte

movwf

EEDATA

; Put in Data register

call

EE_PUT

; Write to EEPROM cell 10h

return


438 The Quintessential PIC Microcontroller

Fig. 15.3 The first 32 bytes of EEPROM holding the sine look-up table.

Any data programmed in the way can be subsequently read by the program. For example, to read sin(50) the contents of EEPROM module location 05h is read, giving from our diagram C4h or 196 decimal (196256 = 0.76525).

Although such load-time data is non-volatile it can be altered from within the program at run time. The Code Protection fuse does not secure data in the EEPROM Data module from modification, only code in the true Program store is protected.

Although it is possible to initialize the Program store in a similar manner using the dw (Data Word) directive in a similar manner, as shown in Program 15.6, this is of little use as the Harvard architecture’s separation of Data and Program store memory spaces means that there is no way an instruction can access this data.2 However, newer 16-bit PIC devices with a Flash EEPROM Program store allow the program to read and write such data in a similar indirect manner to that used for the EEPROM Data module.

To illustrate these devices we will be using the PIC16F87X line. Like the PIC16F8X PICs, these devices all have a main Program store implemented using Flash EEPROM technology as well as a standard EEPROM Data module. Besides the EEPROM module they have the same range of integral peripheral devices described in previous chapters, but slightly enhanced. For example, the Analog module has a 10-bit resolution and the Synchronous Serial port has hardware Master I2C modes.

To cope with the extra SPRs and Data memory the Data store is organized into four banks. Those relevant to EEPROM facilities are located in Banks 2 and 3 and so both RP0 and RP1 bits in the Status register need to used to change bank, as described on page 108.

Four devices are represented in this line:

PIC16F873

This 28-pin device has a 4 Kbyte flash Program store and 128-byte EEPROM Data store together with a 192-byte file register store. It is pin compatible with the PIC16C73 device.

2The PIC17CXXX and 18CXXX 16-bit core families have tablrd and tablewt instructions to read and write respectively data from/to the Program store.

15. To Have and to Hold 439

PIC16F874

This 40-pin device has a 8 Kbyte flash Program store and 256-byte EEPROM Data store together with 192 file registers. It is pin compatible with the PIC16C74 device.

PIC16F876

This 28-pin device is the same as the PIC16F873 but with twice the Program store capacity at 8 Kbytes and double the EEPROM Data module capacity at 256 bytes The file register store is increased to 368 file registers.

PIC16F877

This is a 40-pin version of the PIC16F876. Key EEPROM properties are:

100,000 minimum EEPROM Data module Erase/Write cycle endurance per cell.

1000 minimum flash EEPROM Program store Erase/Write cycle endurance.

Maximum Write/Erase time 8 ms (typical 4 ms) for both the Data module and flash memory.

Of particular note is the endurance limit of 1000 Write cycles for the flash EEPROM. Whilst this is entirely satisfactory when changing the device’s program, it is a limitation for some non-volatile data storage situations. Thus flash Program memory storage is more applicable to constant data, such as the sine lookup table, rather than for information that requires frequent update, such as the odometer.

Flash EEPROM has a smaller geometry than normal EEPROM. Whilst this speeds up its operation, charges which eventually trap in the floating gate insulation have a disproportional e ect on the storage mechanism and leads to earlier deterioration.

Figure 15.4 shows the PIC16F87X EEPROM Data module with the flash Program store superimposed. This form of representation is used as the EEDATA and EEADR registers are common to both EEPROM arrays. Of course, the flash Program store is larger both in the number of cells (8 Kbytes against 256 bytes) and in cell size (14 bits against 8 bits). Thus both Data and Address registers have the high-end extensions EEDATH and EEADRH respectively to cope with this additional capacity.

As we shall see, the process of reading from and writing to either array is similar. The target module is chosen using the EEPGD (EEProgram/Data) control bit control bit in EECON1[7]. Apart from this additional bit and the removal of the EEIF interrupt flag to the PIR2 register, the EECON1 register of Fig. 15.5 is identical to the basic PIC16F8X version shown in Fig. 15.2. The virtual EECON2 interlock register remains the same.


440 The Quintessential PIC Microcontroller

h0D File

PIR2

EEIF

4

h8D File

PIE2

EEIE

4

Interrupt on Write complete

EECON1

7

6

5

4

3

2

1

0

EEPGD

WRERR

WREN

WR

RD

File 18Ch

(R/W X)

(U 0)

(U 0)

(U 0)

(R/W X)

(R/W 0)

(R/S 0)

(R/S 0)

EEPROM CONtrol 2

01010101

10101010

Interlock

EECON2

EEPROM

File 18Dh

ADdRess

13

Flash Program memory

a

4/8 Kbytes

High

EEADRH h10FFile

register

8

a

EEPROM

a

7

ADdRess

h 10D File

register

a

EEADR

0

d13

d8

EEPROM DATa High register

EEDATH 10Eh

Write

Read

Enable

Write

Read

Enable

Data EEPROM 128/256 bytes

d7 d0

EEPROM DATA register

EEDATA

File 10Ch

Fig. 15.4 The PIC16F87X flash and Data EEPROM storage system.