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

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

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

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

Добавлен: 14.06.2025

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

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

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

Data EEPROM Programming

489

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

;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:

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


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

490

Chapter 15

; WRITE operation has completed successfully.

Bank0

return

The procedures Send1I2c, WaitI2C, and the label FailI2C are listed in the sub-sec- tion on the read procedure.

I2C Read Byte Procedure

The following procedure, from the I2CEEP program, reads a byte of data from the 24LC04B device. The address read is stored in the local variable EEMemAdd. The value read is returned in the w register. The listing also includes the support routines used by all three I2C procedures listed.

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

;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

;

5.

Switch to receive mode. Get data.

;

6.

Send NACK

;

7.

Send STOP

;

8.

Retreive 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


SSPCON2,ACKDT ; ACK DATA to send is 1 (NACK) SSPCON2,ACKEN ; Send ACK DATA now.

Data EEPROM Programming

491

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

bsf bsf

;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

fail:

goto

fail

; Procedure to transmit one byte Send1I2C


492

Chapter 15

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

15.2 Sample Programs

The following sections contain the code listing for the programs discussed in this chapter.

15.2.1 EECounter Program

;File name: EECounter.asm

;Last Update: May 22, 2006

;Author: Julio Sanchez

;Processor: 16F84A

;Description:

;Program to demonstrate on chip EEPROM data memory read

;and write operation. Program uses LCD display to output

;results.

;Operation:

;The program keeps track and displays the inNum of times

;the code has been started.

;For LCD display parameters see the LCDTest2 program.

;WARNING:

;Code assumes 4Mhz clock. Delay routines must be

;edited for faster clock

;

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

;switches ;===========================

;Switches used in __config directive:

;

_CP_ON

Code protection ON/OFF

; *

_CP_OFF

;

*

_PWRTE_ON

Power-up timer ON/OFF

;_PWRTE_OFF

;

_WDT_ON

Watchdog timer ON/OFF

; * _WDT_OFF

;

_LP_OSC

Low power crystal oscillator