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

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

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

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

Добавлен: 14.06.2025

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

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

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

538

Chapter 15

;

|

|

|

| |__|__|__|___

Serial

port select bits

;

|

|

|

|

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

; This is 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

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

;I2C write procedure ;============================

;Write one byte to I2C EEPROM 24LC04B

;Steps:

;

1.

Send START

;

2.

Send control. Wait for ACK

;

3.

Send address. Wait for ACK

;

4.

Send data. Wait for ACK

;

5.

Send STOP

; STEP 1:

WriteI2C:


Data EEPROM Programming

539

Bank1

bsf

SSPCON2,SEN

; Produce START Condition

call

WaitI2C ; Wait for I2C to complete

;STEP 2:

;Send control byte. Wait for ACK

movlw

LC04READ

; Control byte

call

Send1I2C

; Send Byte

call

WaitI2C

; Wait for I2C to complete

btfsc

SSPCON2,ACKSTAT ; Check ACK bit to see if

; I2C failed, skip if not

goto

FailI2C

;STEP 3:

;Send address. Wait for ACK Bank0

movf

EEMemAdd,w

; Load Address Byte

call

Send1I2C

; Send Byte

call

WaitI2C ; Wait for I2C operation to complete

Bank1

btfsc

SSPCON2,ACKSTAT ; Check ACK Status bit to see

; If I2C failed, skip if not

goto

FailI2C

;STEP 4:

;Send data. Wait for ACK Bank0

movf

EEByte,w

; Load Data Byte

call

Send1I2C

; Send Byte

call

WaitI2C

; Wait for I2C operation to complete

Bank1

btfsc

SSPCON2,ACKSTAT ; Check ACK Status bit to see

; if I2C failed, skip if not

goto

FailI2C

;STEP 5:

;Send STOP. Wait for ACK

bsf

SSPCON2,PEN

; Send STOP condition

call

WaitI2C ; Wait for I2C operation to complete

; WRITE operation has completed successfully. Bank0

return

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

;I2C read procedure ;============================

;Procedure to read one byte from 24LC04B EEPROM

;Steps:

;1. Send START

;2. Send control. Wait for ACK

;3. Send address. Wait for ACK

;4. Send RESTART + control. Wait for ACK


SSPCON2,ACKSTAT ; Check ACK Status bit ReadI2C ; ACK Poll waiting for EEPROM
; write to complete

540

Chapter 15

;5. Switch to receive mode. Get data.

;6. Send NACK

;7. Send STOP

;8. Retrieve data into w register

;STEP 1:

ReadI2C

; Send RESTART. Wait for ACK

Bank1

bsf

SSPCON2,RSEN ; RESTART Condition

call

WaitI2C ; Wait for I2C operation

;STEP 2:

;Send control byte. Wait for ACK

movlw

LC04READ

;

Control byte

call

Send1I2C

;

Send Byte

call

WaitI2C

; Wait for I2C operation

; Now check to see if I2C EEPROM is ready Bank1

btfsc goto

;STEP 3:

;Send address. Wait for ACK Bank0

movf

EEMemAdd,w

; Load from

address register

call

Send1I2C

; Send Byte

call

WaitI2C

; Wait for I2C operation

Bank1

btfsc

SSPCON2,ACKSTAT

; Check ACK

Status bit

goto

FailI2C ; failed, skipped if successful

;STEP 4:

;Send RESTART. Wait for ACK

bsf

SSPCON2,RSEN

; Generate RESTART Condition

call

WaitI2C

; Wait for I2C operation

; Send output control. Wait for ACK

movlw

LC04WRITE

; Load CONTROL BYTE (output)

call

Send1I2C

; Send Byte

call

WaitI2C

; Wait for I2C operation

Bank1

btfsc

SSPCON2,ACKSTAT ; Check ACK Status bit

goto

FailI2C ; failed, skipped if successful

;STEP 5:

;Switch MSSP to I2C Receive mode

bsf

SSPCON2,RCEN

;

Enable Receive Mode (I2C)

; Get the data. Wait for ACK

call

WaitI2C

;

Wait for I2C operation

;STEP 6:

;Send NACK to acknowledge Bank1


Data EEPROM Programming

541

bsf

SSPCON2,ACKDT

;

ACK DATA

to send is 1 (NACK)

bsf

SSPCON2,ACKEN

;

Send ACK

DATA now.

;Once ACK or NACK is sent, ACKEN is automatically cleared

;STEP 7:

;Send STOP. Wait for ACK

bsf

SSPCON2,PEN

;

Send

STOP condition

call

WaitI2C

;

Wait

for I2C operation

;STEP 8:

;Read operation has finished Bank0

movf SSPBUF,W ; Get data from SSPBUF into W

; Procedure has finished and completed successfully. return

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

;I2C support procedures ;============================

;I2C Operation failed code sequence

;Procedure hangs up. User should provide error handling. FailI2C

Bank1

bsf

SSPCON2,PEN

; Send STOP condition

call

WaitI2C

; Wait for I2C operation

goto

fail

fail:

; Procedure to transmit one byte

Send1I2C

Bank0

movwf

SSPBUF

; Value to send to SSPBUF

return

;Procedure to wait for the last I2C operation to complete.

;Code polls the SSPIF flag in PIR1.

WaitI2C

Bank0

btfss

PIR1,SSPIF

; Check if I2C operation done

goto

$-1

;

I2C

module

is not ready yet

bcf

PIR1,SSPIF

;

I2C

ready,

clear flag

return

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

end ; END OF PROGRAM

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