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

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

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

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

Добавлен: 14.06.2025

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

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

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

Data EEPROM Programming

483

bits:

7

6

5

4

3

2

1

0

WCOL

SSPOV

SSPEN

SSPM3

SSPM2

SSPM1

SSPM0

bit 7 WCOL:

Write

Collision Detect bit

Master mode:

1

= A write to SSPBUF was attempted while the I2C

conditions were not valid

0

= No collision

bit 6 SSPOV: Receive Overflow Indicator bit

In I2 C mode:

1

= A byte is received while the SSPBUF is holding

the previous byte.

SSPOV is a "don’t care" in Transmit mode. (Must be

cleared in software.)

0

= No overflow

bit 5 SSPEN: Synchronous Serial Port Enable bit

In I2 C mode,

When enabled, these pins must be properly configured

as input or output

1

= Enables the serial port and configures the SDA

and SCL pins as the source of the serial port

pins

0

= Disables serial port and configures these pins

as normal I/O ports

bit 4

UNUSED IN 12C MASTER MODE

bit 3-0 SSPM3:SSPM0:

Synchronous Serial Port Mode Select bits 1000 = I2C Master mode,

clock = FOSC / (4 * (SSPADD+1)) 1001, 1010, 1100, 1101 = Reserved

Figure 15-10 SSPCON Register Bitmap in I2C Master Mode

The WCOL bit is an error flag that indicates that a Write Collision has occurred. Write collisions do not take place when programming an EEPROM device. This bit is useful in multi-master systems since it can detect when more than one master device is attempting to write to the bus.

The SSPOV bit (Synchronous Serial Port Overflow) is set by the microcontroller whenever there is an overflow error. An overflow occurs whenever an I2C transfer finishes but the previous data has not been read from SSPBUF. If SSPOV bit is set, it must be cleared by application code. Data in SSPBUF is not updated until the overflow condition is cleared.

The SSPEN bit (Synchronous Serial Port Enable) is set to turn on the SSP module, as is the case in I2C communications.

The bits SSPM0 through SSPM3 (Synchronous Serial Port mode bits) determine whether the MSSP module is configured for SPI or I2C and whether it is in slave or master mode. In the master mode, the MSSP module handles all details of I2C communications, such as generating the various conditions and sending and receiving data. The Master Mode is enabled by entering the binary value 1000 in this bit field.

Another frequently used register in I2C communications is SSPCON2. Figure 15-11 is a bitmap of this register in the I2C master mode.


484

Chapter 15

bits:

7

6

5

4

3

2

1

0

ACKSTAT

ACKDT

ACKEN

RCEN

PEN

RSEN

SEN

Bit 7

UNSED IN I2C MASTER MODE

bit 6

ACKSTAT:

Acknowledge Status bit

In Master Transmit mode:

1

= Acknowledge was not received from slave

0

= Acknowledge was received from slave

bit 5

ACKDT:

Acknowledge Data bit

In Master Receive mode:

Value that will be transmitted when the user

initiates an Acknowledge sequence at the end of

a

receive.

1 = Not Acknowledge

0 = Acknowledge

bit 4

ACKEN:

Acknowledge Sequence Enable bit

In Master Receive mode:

1

= Initiate Acknowledge sequence on SDA and SCL

pins and transmit ACKDT data bit.

Automatically cleared by hardware.

0

= Acknowledge sequence idle

bit 3

RCEN:

Receive Enable bit

1

= Enables Receive mode for I2C

0

= Receive idle

bit 2

PEN:

STOP Condition Enable bit (In I2C Master mode only)

SCK Release Control:

1

= Initiate STOP condition on SDA and SCL pins.

Automatically cleared by hardware.

0

= STOP condition idle

bit 1

RSEN:

Repeated START Condition Enable bit

1

= Initiate Repeated START condition on SDA

and SCL pins.

Automatically cleared by hardware.

0

= Repeated START condition idle

bit 0

SEN:

START Condition Enable bit

1

= Initiate START condition on SDA and SCL pins.

Automatically cleared by hardware.

0

= START condition idle

Figure 15-11 SSPON2 Register Bitmap in I2C Master Mode

The ACKSTAT bit is set when an ACK or NACK has been received. This bit can be tested by application code to determine if an ACK or NACK condition was received.

When the master reads data from a device, it must acknowledge the transfer by sending an ACK or NACK condition. The ACKDT bit determines the value of the condition to be sent: if it is clear an ACK is sent; otherwise a NACK is sent.

The ACKEN bit determines when the acknowledge condition is sent.

The RCEN bit places the MSSP module into I2C receive mode. When one byte of data is received, this bit automatically clears and the PIC returns to transmit mode. Code must ACK or NACK the data then reset this bit.


Data EEPROM Programming

485

Setting the PEN bit automatically sends a stop condition. This bit is automatically cleared at the end of the start condition.

The RSEN bit sends a restart condition. After the bit is set, application code must wait for the transfer to complete. This bit is reset automatically when the condition or data transfer finishes.

The SEN bit (for Start condition Enable) is equivalent to sending a start or restart condition. The SEN bit is reset after the start condition completes.

The SSPSTAT (Synchronous Serial Port Status) register contains three bits related to IC2 communications in master mode. The SMP bit controls the slew rate. The slew rate is a squelch filter for the I2C waveform that improves performance when transmission takes place at 400 kbps. This bit should be set at the 400 kbps transmission rate and reset at any slower rate. The CKE bit is used to allow the MSSP module to handle SMBus peripherals. Normally, this bit should be cleared. The BF bit (buffer full) indicates the SSPBUF contains unread data. In either the master or slave mode this data must be read before any other data is sent or received. The BF flag is set and cleared by the PIC. If SSPBUF is not read before another byte is received the buffer overflows and the SSPOV bit will be set.

Finally, the SSPADD (Synchronous Serial Port Address) register has a unique function in the I2C master mode: it controls the bus speed. The value entered into the SSPADD register determines the Baud Rate according to the following formula:

= Fosc

BaudRate

4 • (SSPADDVAL + 1)

where Fosc is the oscillator speed in MHz. Solving this formula in terms of the value to be entered into SSPADD, we have:

SSPADDVAL

=

Fosc

− 1

• Baud Rate

4

For a baud rate of 100 kbps (equal to 100,000Mhz) the formula is:

SSPADDVAL

=

10,000,000

− 1 =

100

− 1 = 24

4 •100,000

4

In this case, the value to be entered into the SSPADD register while using a communications speed of 100 kbps, in a PIC with a 10 KHz oscillator, is 24. The calculations can be checked by substituting into the original formula:

Baud Rate =

Fosc

=

10,000,000

= 100,000 Mhz = 100 kbps

• (24 + 1)

4

100


486

Chapter 15

15.1.8 I2C Serial EEPROM Programming on the 16F877

The 16F87x PIC family contains the Master Synchronous Serial Port module, which can be set in either Serial Peripheral Interface or Inter-Integrated Circuit mode. In the I2C mode the module performs either as a master, a multi-master, or a slave. In the context of driving an I2C EEPROM device, the MSSP module is initialized in the master mode. I2C firmware modes are provided for compatibility with other mid-range products.

The demonstration program named I2CEEP in the book’s on line software receives character data from a PC through the RS-232 line and stores these characters in a 24LC04B EEPROM IC. The program uses the I2C serial interface facilities provided by the PIC’s MSSP module. An on-board LCD echoes the received characters. When the PC user presses <Enter> text stored in the EEPROM IC is retrieved and displayed on the LCD.

On startup, the top LCD line displays the prompt: “Receiving:”. At that time, a message “Rdy-” is sent through the serial line so as to test the connection. The program’s serial communications run at 2400 baud, no parity, 1 stop bit, and 8 character bits. The 24LC04B SDA line is wired to PIC RC4 (MSSP SDA) and the SCL line is wired to PIC RC3 (MSSP SCL). In the 24LC04B the A0-A2 are not used. In the demonstration circuit, the WP lines are wired to ground. Program provides little error checking. The circuit in Figure 15-12 is used with the demonstration program.

The I2CEEP program includes three I2C-related functions:

1.SetupI2C. Initializes MSSP module for I2C mode in hardware master mode, configures the I2C lines, sets the slew rate for 100kbps, and sets the baud rate for 10Mhz

2.WriteI2C. Writes one byte to I2C EEPROM device. Data and address are stored in local variables.

3.ReadI2C. Reads one byte from I2C EEPROM device. Address is stored in a local variable and read data is returned in the w register.

As in previous 16F877 examples, we have placed the most used variables in the common RAM area, that is, in GPRs located from 0x70 to 0x7f. All three procedures use bank changing macros described and listed previously.

IC2 Initialization Procedure

The following procedure from the I2CEEP program initializes the MSSP module for operation in I2C mode with a 24LC04B EEPROM IC. The module is initialized for master mode operation on a PIC with a 10MhZ baud rate. For use with a faster or slower oscillator the value stored in the SSPADD register must be modified according to the formula.

;============================

; I2C setup procedure

;============================

SetupI2C:

Bank1

Data EEPROM Programming

487

+5v

EEPROM

R=10K

READ

+5v

+5v

RESET

1

8

R=10K

A0

+5v

2 A1

24LC04B WP 7

3

6

A2

SCL

4

5

1

40

GND

SDA

!MCLR/VPP 16F877

RB7/PGD

2

39

RA0/AN0

RG6/PGC

3

38

RA1/AN1

RB5

4

37

RA2/AN2.VREF-

RB4

5

36

RA3/AN3/VREF+

RB3/PGM

6

35

LCD

RA4/TOCKI

RB2

7

34

2 rows x 20

RA5/AN4/SS

RB1

8

33

RE0/!RD/AN5

RB0/INT

9

32

RE1/!WR/AN6

VDD

14

10

31

RE2/!CS/AN7

VSS

+5v

11

30

12

VDD

RD7/PSP7

29

13

VSS

RD6/PSP6

28

OSC1/CLKIN

RD5/PSP5

14

27

OS2/CLKOUT

RD4/PSP4

10 MHz

15

26

RC0/T1OSO/T1CKI

RC7/RX/DT

Osc

16

25

RC1/T1OSI/CCP2

RC6/TX/CK

17

RC2/CCP1

24

RC5/SD0

18

RC3/SCK/SCL

23

RC4/SDI/SDA

19

RD0/PSP0

22

RD3/PSP3

20

RD1/PSP1

21

RD2/PSP2

+5 V

+5 V

1

DB-9

1

MAX203

+5v

16

(female)

C1+

2

15

GND

V+

5

4

3

2

1

3

T1out

14

C1-

9

8

7

6

4

R1in

13

C2+

HD44780

C2-

R1out

12

5

6

T1in

11

V-

7

T2in

10

T2out

8

R2out

9

R2in

+5v

R=4.7K

R=4.7K

RS

E

R/W

Figure 15-12

Circuit for I2CEEP Demonstration Program

movlw

b’00011000’

iorwf

TRISC,f

; OR into TRISC

; Setup MSSP module for Master Mode operation

Bank0

movlw

B’00101000’; Enables MSSP and uses appropriate

;

0

0

1

0

1

0

0

0

Value to install

;

7

6

5

4

3

2

1

0

<== SSPCON bits in this operation

;

|

|

|

|

|__|__|__|___ Serial port select bits


488

Chapter 15

;

|

|

|

|

1000 =

I2C master mode

;

|

|

|

|

Clock =

Fosc/(4*(SSPAD+1))

;

|

|

|

|_______________

UNUSED

IN MASTER MODE

;

|

|

|__________________

SSP

Enable

;

|

|

1 =

SDA

and SCL pins as serial

;

|

|_____________________

Receive

0verflow indicator

;

|

0 =

no

overflow

;|________________________ Write collision detect

;

0 = no collision detected

movwf

SSPCON

; Loaded into SSPCON

; Input levels

and slew rate

as standard I2C

Bank1

movlw

B’10000000’

;

;

1

0

0

0

0

0

0

0 Value to install

;

7

6

5

4

3

2

1

0 <== SSPSTAT bits in this operation

;

|

|

|

|

|

|

|

|___

Buffer full status bit READ ONLY

;

|

|

|

|

|

|

|______

UNUSED in present application

;

|

|

|

|

|

|_________

Read/write information READ ONLY

;

|

|

|

|

|____________

UNUSED IN MASTER MODE

;

|

|

|

|_______________

STOP bit READ ONLY

;

|

|

|__________________

Data address READ ONLY

;

|

|_____________________

SMP bus select

;

|

0 = use normal I2C specs

;|________________________ Slew rate control

;

0 = disabled

;

movwf

SSPSTAT

;Setup Baud Rate

;Baud Rate = Fosc/(4*(SSPADD+1))

;Fosc = 10Mhz

;Baud Rate = 24 for 100 kbps

movlw

.24

;

Value

to

use

movwf

SSPADD

;

Store

in

SSPADD

Bank0

return

The procedures Send1I2c, WaitI2C, and the label FailI2C are listed in the subsection on the read procedure.

I2C Write Byte Procedure

The following procedure, from the I2CEEP program, writes one byte of data to an 24LC04B EEPROM IC, at the memory address stored in the variable EEMemAdd. The value to write is stored in the local variable EEByte.