Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf

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

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

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

Добавлен: 14.06.2025

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

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

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

478

Chapter 15

The lack of the ACK signal is interpreted as NACK (represented by the letter N). NACK represents a negative acknowledge. The NACK signal is a passive response since the SDA line is normally held high. Both ACK (A) and NACK (N) refer to the previous byte of data.

15.1.4 EEPROM Write Operation

Figure 15-5 represents the I2C action sequence that takes place during a write to a small EEPROM, that is, one that requires a single address byte. Later we will see operations that access I2C EEPROMS with a 2-byte address space.

MASTER:

S

Control In

Address

Data

P

SLAVE:

A

A

A

Figure 15-5 I2C Write Sequence to Small EEPROM

Note in Figure 15-5 that three bytes of information are required in the data transfer. The transmission starts with the S condition issued by the master, followed by a control byte. In the case of an EEPROM, the control byte indicates either a read or a write operation. In Figure 15-5, the control byte is labeled Control In, since it places the EEPROM device in input mode required for the write to take place. The EEPROM acknowledges the control byte by issuing the A condition. At this point, the master proceeds to transmit the address byte, which defines the EEPROM memory location at which the write operation is to take place. The slave (in this case the

EEPROM) acknowledges reception of the address byte by issuing another A condition. Next, data is sent by the master and “ACKed” by the EEPROM. Finally, the master transmits the stop signal (condition P) which concludes the operation. The EEPROM does not proceed to write the data until the P signal is received.

15.1.5 EEPROM Read Operation

The read operation to a small EEPROM is similar to the write. In this case, 4 bytes of information must be exchanged: 3 from the master to the slave and one (the data item read) from the slave to the master. Figure 15-6 shows the sequence.

MASTER: S Control In

SLAVE:

A

Address

R

Control Out

N

P

A

A

Data

Figure 15-6 Read Sequence to a Small EEPROM


Data EEPROM Programming

479

Note in Figure 15-6 that the first command from master to slave is a Control In byte to indicate that an address follows. Once the address is acknowledged, the master sends the restart command (R condition) and a Control Out command indicating that the master is requesting a read operation. The EEPROM then acknowledges and sends the data, to which the master responds with NACK to instruct the

EEPROM that no more data is required.

In both read and write sequences, each byte transmitted requires a response from the other element. This response can be either an ACK (condition A) or a NACK (condition N). The RESTART bit that preceded the Control Out command is necessary since the P condition (STOP bit) has not been sent. The I2C protocol requires that the START condition be sent only on an idle bus, and never in the middle of a transmission.

Read and write operations to large EEPROMS, those with a two-byte address space, are identical to the ones described, except that there are two address bytes. The first one holds the high-order element and the second one the low-order. Here again, each byte transmitted must be acknowledged by the receiver.

15.1.6 I2C EEPROM Devices

EEPROM ICs that conform to the I2C specification are available to the PIC circuit designer. Microchip (the same company that manufactures the PIC microcontrollers) markets a series of I2C chips for this purpose. The line is designated as the 24XXX series of serial EEPROM devices. Table 15.1 lists several I2C EEPROM devices available from Microchip.

Table 15.1

I2C Compatible Serial EEPROM Devices from Microchip

MAX

CAPACITY

PAGE SIZE

DESIGNATION

CLOCK

BITS

BYTES

(IN BYTES)

CASCADE

24xx00

400kHz

128

16

0

No

24xx01

400kHz

1K

128

8/16

No/8

24xx02

400kHz

2K

256

8/16/22

No/8

24xx04

400KHz

4K

512

16

No

24xx08

400Khz

8K

1K

16

No

24xx16

400Khz

16K

2K

16

No

24xx32

400Khz

32K

4K

32

8

24xx64

400Khz

64K

8K

8/32

8

24xx128

400kHz

128K

16K

64

8

24xx256

400kHz

256K

32K

64

8

24xx512

400kHz

512K

64K

64/128

No/4/8

24xx1025

1MHz

1024

128K

128

4

The memory capacity of the various EEPROM ICs ranges from 16 bytes to 128K. Since up to four 128K devices can be cascaded, the total accessible memory goes up to 512K. In selecting a particular IC one must take into account several parameters, since the page size, the maximum clock speed, and the number of similar devices that can be grouped changes within the same device type. In the example developed later in this chapter, we used the 24LC04B EEPROM with a total of 512 bytes located in two memory banks.


480

Chapter 15

A0

A1

A2

GND

24xxx EEPROM PINOUT

Vcc

1

8

A0-A2 - Chip address input

2

24xxx

7

WP

GND - Ground

Vcc - +1.8 to 5.5V power supply

3

6

SCL

WP - Write protect

4

5

SDA

SCL - Serial clock

SDA - Serial address/data I/O

Figure 15-7 Pin Out of the 24xxx EEPROM Line

The standard DIP package of the 24xxx EEPROMS consists of eight pins, as shown in Figure 15-7.

Lines A0 to A2 are used to encode the chip’s address when supported by the device. The three lines allow up to eight possible combinations to identify up to eight similar cascaded devices, as shown in the corresponding column of Table 15.1. Also note in Table 15.1 that several EEPROMs do not support more than one device per address bus. In these cases, pins A0 to A2 are not meaningful. In devices that support this function, the pins must be hardwired to logic 0 or logic 1. If the pins are left floating the device could malfunction.

The SDA pin is bidirectional and is used to transfer addresses and data into and out of the device. Since it is an open drain it requires a pull-up resistor to Vcc. The resistor is typically 10 kÙ for 100 kHz, 2 kÙ for 400 kHz. During data transfer the SDA line is allowed to change only while SCL is low. SDA line changes while SCL is high are used for indicating the START and STOP conditions.

The SCL line is used to synchronize the data transfer to and from the device. The WP (Write-Protect) pin provides this function when tied to ground. For normal read/write operation the WP pin is tied to the Vcc line. Read operations are not affected by this pin. The write protect function allows using the EEPROM as a serial ROM.

15.1.7 PIC Master Synchronous Serial Port (MSSP)

Although I2C interfaces can and have been implemented in software, this emulation is not an attractive option now that more efficient and simpler hardware versions of I2C are available in many PICs. For this reason we do not discuss the software emulation of I2C in this book.

Some PIC microcontrollers come equipped with hardware modules to implement EEPROM serial protocols, including SPI and I2C. The module that provides these interfaces is named the Master Synchronous Serial Port, or MSSP. Although the MSSP module operates in slave or master mode, in the context of EEPROM programming the MSSP is set in master mode. The MSSP module can operate in a free bus mode,


Data EEPROM Programming

481

also called the multi-master function. In this section we discuss MSSP master mode operations.

I2C uses two communications lines, labeled the SDA or data line, and the SCL or clock line. The PICs that contain I2C hardware implementation multiplex two pins for these functions. In the case of the 16F877 (which we use in the forthcoming examples) the SCL line is attached to bit 3 in Port-C (16F877 pin number 18) and the SDA line to bit 4 in Port-C (16F877 pin number 23). When the PIC is used in MSSP mode, these two pins must be initialized for input by setting the corresponding TRIS register bits. The pull-up resistors for these lines must be provided externally. Figure 15-8 shows the minimal wiring diagram between a 16F877 PIC and a 24LC04B EEPROM IC.

+5v

RESET

R=10K

1

40

RB7/PGD

2

!MCLR/VPP 16F877

39

3

RA0/AN0

RG6/PGC

38

RB5

4

RA1/AN1

37

RB4

RA2/AN2.VREF-

5

RB3/PGM

36

6

RA3/AN3/VREF+

35

+5v

RB2

RA4/TOCKI

7

RB1

34

8

8

RA5/AN4/SS

1

A0

+5v

33

RB0/INT

24LC04B

7

RE0/!RD/AN5

2

WP

9

VDD

32

A1

6

RE1/!WR/AN6

3

A2

SCL

10

31

VSS

5

RE2/!CS/AN7

4

GND

SDA

11

+5v

RD7/PSP7

30

12

VDD

29

VSS

RD6/PSP6

13

OSC1/CLKIN

RD5/PSP5

28

14

OS2/CLKOUT

RD4/PSP4

27

10 MHz

15

RC0/T1OSO/T1CKI

RC7/RX/DT

26

Osc

16

25

RC1/T1OSI/CCP2

RC6/TX/CK

17

RC2/CCP1

RC5/SD0

24

18

23

RC3/SCK/SCL

RC4/SDI/SDA

19

RD0/PSP0

RD3/PSP3

22

20

RD1/PSP1

RD2/PSP2

21

+5v

R=4.7K

R=4.7K

Figure 15-8 Wiring Diagram between a 16F877 PIC and 24LC04B EEPROM.

Note in Figure 15-8 that the address lines (A0 to A2) in the 24LC04B IC are wired to ground. The reason is that these lines are not used in this particular EEPROM (see Table 15.1). Also wired to ground is the write protect line. This allows read and write operations. Only two connections are required between the PIC and the EEPROM: the SLK and SDL lines. The 4.7K resistors are pull-ups to implement the open drain operation on these lines.


482

Chapter 15

REGISTER

NAME

7

6

5

4

3

2

1

0

bits

INTCON

GIE

PEIE

PIR1

SSPIF

PIE1

SSPIE

PIR2

BCLIF

PIE2

BCLIE

SSPBUF

(Receive Buffer/Transmit Register)

SSPCON

WCOL

SSPOV

SSPEN

CKP

SSPM3

SSPM2

SSPM1

SSPM0

SSPCON2

GCEN

ACKSTAT

ACKDT

ACKEN

RCEN

PEN

RSEN

SEN

SSPADD

(I2C Slave Address/Master Baud Rate Register)

SSPSTAT

SMP

CKE

D/_A

P

S

R/_W

UA

BF

Figure 15-9 SFRs Associated with I2C Operations

Several 16F87x registers relate to MSSP operation in I2C mode. Figure 15-9 shows these SFRs.

In the following subsection we discuss the registers and bits that apply to MSSP operation in Master Mode.

MSSP in Master Mode

In the context of accessing EEPROM circuits, the MSSP is operated in master mode. At this point we should consider that although the EEPROM device operates as a slave, it is a “smart” slave since it has a control engine capable of performing operations on its own, including reading and writing to its address space, recognizing commands, and issuing the corresponding responses. For example, in a data write operation the master sends the corresponding command code, followed by the address to which the data is to be written, followed by the data itself. The peripheral (in this case the EEPROM IC) receives and acknowledges the various bytes and executes the requested operations. In the case of a read command the EEPROM fetches and returns the data from the memory address requested in the command.

One of the special function registers most used in MSSP master mode operations is the SSPCON. Figure 15-10 is a bitmap of this register when operating in I2C master mode.