Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
Скачиваний:
475
Добавлен:
12.08.2013
Размер:
4.99 Mб
Скачать

Mid-range PIC Architecture

153

8.3 EEPROM Data Storage

EEPROM (pronounced double-e PROM or e-squared PROM) stands for electri- cally-erasable programmable read-only memory. EEPROM is used in computers and digital devices as non-volatile storage. EEPROM is not RAM, since RAM is volatile and EEPROM retains its data after power is removed. EEPROM is found in USB flash drives and in the non-volatile storage of several microcontrollers, including many PICs.

One advantage of EEPROM is that it can be erased and written electrically, without removing the chip. The predecessor technology, named EPROM, required that the chip be removed from the circuit and placed under ultraviolet light. EEPROM simplifies the erasing and re-writing process.

EEPROM data memory refers to both on-board EEPROM memory and to EEPROM memory ICs as separate circuit components. In general, EEPROM elements are classified according to their electrical interfaces into serial and parallel. Most EEPROM memories used in PICs are serial EEPROMs, also called SEEPROMs. The typical use of serial EEPROM on-board memory and EEPROM on ICs is in the storage of passwords, codes, configuration settings, and other information to be remembered after the system is turned off. For example, a PIC-based security system can use EEPROM memory to store the system password. Since EEPROM can be written, the user can change this password and the new one will also be remembered.

8.3.1 EEPROM in Mid-Range PICs

The mid-range PICs are equipped with EEPROM memory in three possible sizes: 64 bytes, 128 bytes, and 256 bytes. EEPROM memory allows read and write operations. This memory is not mapped into the processor’s data or program area, but in a separate block that is addressed through some SFRs. The registers related to EEPROM operations are:

1.EECON1

2.EECON2 (not a physically implemented register)

3.EEDATA

4.EEADR

EECON1 contains the control bits, and EECON2 is used to initiate the EEPROM read and write operations. The 8-bit data item to be written must first be stored in the EEDATA register, while the address of the location in EEPROM memory is stored in the EEADR register. The EEPROM address space always starts at 0x00 and extends linearly to maximum in the device.

When a write operation is performed, the contents of the EEPROM location are automatically erased. The EEPROM memory used in PICs is rated for high erase/write cycles. EEPROM programming is the topic of Chapter 15.

154

Chapter 8

8.4 Data Memory Organization

The structure and organization of data memory in the PIC hardware also has some unique and interesting features. The programmer accustomed to the flat, addressable memory space of the von Neumann computer with its multiple machine registers may require some time in order to gain familiarity with the PIC’s data formats.

8.4.1 The w Register

PICs have only one addressable register called the work register or the w register. The CISC programmer who is used to having multiple general purpose registers into which data can be moved and later retrieved has to become used to a single machine register that takes part in practically every instruction. Add to this the lack of an addressable stack into which data can be pushed and popped, and you see that PIC programming is a different paradigm.

8.4.2 The Data Registers

PIC’s data memory consists of registers, also called file registers. These behave more like conventional variables, and can be addressed directly and indirectly. All data registers are 8-bits. Data registers come in two types: general purpose registers (GPRs) and special function registers (SFRs).

Memory Banks

The PIC instruction format devotes seven bits to the address field (see Figure 8-2, Section 8.0.4). A 7-bit address allows access to only 128 memory locations. Since many PICs of the mid-range family have more than 128 bytes of data memory, an addressing scheme based on memory banks must be implemented. The memory banking mechanism adopted by the PICs is effective, although not very user-friendly.

The number of banks vary according to the amount of available RAM, always in multiples of 128-bytes. All mid-range PICs have banked memory. Banking is accomplished through the special bank-select bits in the STATUS register (see Figure 8-5). Not all banking bits are implemented in all devices. For example, the 16F84/16F84A contain two memory banks; therefore, bank shifting requires a single bank-select bit (RP0). In this case the RP1 bit is not implemented. In devices with more than two memory banks bank selection is as shown in Table 8.2.

Table 8.2

Mid-Range Bank Selection Options in Direct Addressing

BANK

STATUS REGISTER

ACCESSED

BITS (RP1:RP0)

 

 

0

0 : 0

1

0 : 1

2

1 : 0

3

1 : 1

Figure 8-7 shows how banked memory is accessed in direct addressing. The illustration refers to a mid-range PIC with four banks, as is the case with the 16F87x.

Mid-range PIC Architecture

155

RP1

RP0

6

0

<== offset in bank

 

 

 

 

 

 

 

 

 

 

 

11

 

 

 

 

00

01

10

 

 

 

 

 

 

 

 

 

 

 

 

 

0x00

 

0x00

 

 

 

0x00

 

0x00

 

 

Bank 0

Bank 1

Bank 2

Bank 3

0x7f

0x7f

0x7f

0x7f

Figure 8-7 Memory Access in Direct Addressing

The SFRs

The special function registers are defined by the device architecture and have reserved names. For example, the TMR0 register is part of the system timer, the STATUS register holds several processor flags, and the INTCON register is used in controlling interrupts. Some SFRs can be written and read and others are read-only. Some reserved and not-implemented SFR bits always read as zero. Two SFR registers, which are used in indirect addressing, have special characteristics: one of them (the indirect address register) is not a physical register, and the other one (the FSR register) is used to initialize the indirect pointer. The SFR are allocated starting at the lowest RAM address (address 0).

Figure 8-8 (in the following page) is a map of the register file in the 16F87x family. Note in Figure 8-8 that the general purpose registers do not start at the same address offset in each bank. However, there is a common area that extends from 0x70 to 0x7f that is accessible no matter which bank is selected. In applications that require frequent bank switching, this 16-byte area is very valuable real-estate since user variables created in it are accessible no matter which bank is currently selected. GPRs created outside this common area are only accessible when the corresponding bank is selected.

156

Chapter 8

Bank 0

INDF 0x00

TMR0 0x01

PCL 0x02

STATUS 0x03

FSR 0x04

PORTA 0x05

PORTB 0x06

PORTC 0x07

PORTD 0x08

PORTE 0x09

PCLATH 0x0a

INTCON 0x0b

PIR1 0x0c

PIR2 0x0d

TMR1L 0x0e

TMR1H 0x0f

T1CON 0x10

TMR2 0x11

T2CON 0x12

SSPBUF 0x13

SSPCON 0x14

CCPR1L 0x15

CCPR1H 0x16

CCP1CON 0x17

RCSTA 0x18

TXREG 0x19

RCREG 0x1a

CCPR2L 0x1b

CCPR2H 0x1c

CCP2CON 0x1d

ADRESH 0x1e

ADCON0 0x1f

0x20

General

Purpose

Registers

Common area

0x70-0x7f 0x7f

Bank 1

INDF 0x80

OPTION* 0x81

PCL 0x82

STATUS 0x83

FSR 0x84

TRISA 0x85

TRISB 0x86

TRISC 0x87

TRISD 0x88

TRISE 0x89

PCLATH 0x8a

INTCON 0x8b

PIE1 0x8c

PIE2 0x8d

PCON 0x8e

0x8f

0x90 SSPCON2 0x91 PR2 0x92 SSPADD 0x93 SSPTAT 0x94 0x95 0x96 0x97

TXSTA 0x98 SPBRG 0x99 0x9a 0x9b 0x9c 0x9d

ADRESL 0x9e

ADCON1 0x9f

0xA0

General

Purpose

Registers

0xef

0xf0

Common area

0x70-0x7f 0xff

* Actual name is OPTION_REG

Figure 8-8 16F87x File Register Map

Bank 2

 

Bank 3

 

 

0x100

 

0x180

INDF

INDF

 

0x101

 

0x181

TMR0

OPTION*

 

0x102

 

0x182

PCL

PCL

 

0x103

 

0x183

STATUS

STATUS

 

0x104

 

0x184

FSR

FSR

 

 

 

0x185

 

0x105

 

PORTB

0x106

TRISB

0x186

 

0x107

 

0x187

 

 

 

0x108

 

0x188

 

0x109

 

0x189

PCLATH

0x10a

PCLATH

0x18a

 

0X10b

 

0x18b

INTCON

INTCON

 

0x10c

 

0x18c

EEDATA

EECON1

 

0x10d

 

0x18d

EEADR

EECON2

 

 

 

 

EEDATH

0x10e

Reserved 0x18e

 

 

 

 

EEADRH

0x10f

Reserved 0x18f

 

0x110

 

0x190

 

 

General

General

Purpose

Purpose

Registers

Registers

 

0x16f

 

0x1ef

 

 

 

0x1f0

Common 0x170

Common

area

area

 

0x70-0x7f

0x17f

0x70-0x7f

0x1ff

The registers in boldface in Figure 8-8 are accessible from any bank. These registers, such as STATUS and the indirect addressing registers FSR and INDF, are bank-independent. Also, some registers are mirrored in more than one bank. For example, the PORTB register is accessible in bank 0 and in bank 2, and the TRISB register in bank 1 and bank 3. The mirrored registers are designed to simplify data access and minimize bank changes in applications.

Mid-range PIC Architecture

157

Other members of the mid-range PIC group, such as the 16F84 and 16F84A, have a different memory footprint. Figure 8-9 is a bitmap of the 16F84A.

Bank 0

 

Bank 1

 

 

 

 

 

INDF

0x00

INDF

0x80

 

0x01

 

0x81

TMR0

OPTION*

 

0x02

 

0x82

PCL

PCL

 

0x03

 

0x83

STATUS

STATUS

 

0x04

 

0x84

FSR

FSR

 

0x05

 

0x85

PORTA

TRISA

 

0x06

 

0x86

PORTB

TRISB

 

0x07

 

0x87

 

 

 

0x08

 

0x88

EEDATA

EECON1

 

0x09

 

0x89

EEADR

EECON2

 

0x0a

 

0x8a

PCLATH

PCLATH

 

0x0b

 

0x8b

INTCON

INTCON

 

0x0c

 

0x8c

 

 

 

 

General

 

General

 

Purpose

 

 

 

 

Purpose

 

Registers

 

 

 

 

Registers

 

-

 

 

 

 

 

 

mapped to

 

 

 

bank 0

 

 

 

-

 

 

0x4f

 

0xcf

* Actual name is OPTION_REG

Figure 8-9 16F84A File Register Map

Here again, the general purpose registers do not start at the same address offset in each bank. Also note that all GPRs are mapped to bank 0. In the 16F84A, this means that user-defined registers created in bank 0 are accessible no matter which bank is currently selected.

The GPRs

General purpose registers are created and named by the programmer and must be allocated in the reserved memory space. In the 16F84A all GPRs are mapped to the same memory area, no matter in which bank they are defined. The GPR memory space actually extends from 0x0c to 0x4f (68 bytes). A different situation exists in the 16F87x PICs, in which only 16 bytes of GPR space is mirrored in all three banks. This is the memory referred to as the common area in Figure 8-8. In the 16F87x the total available GPR space is as follows: