How to I2C for many data?

Status
Not open for further replies.

slurp

Newbie level 6
Joined
Apr 19, 2011
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,491
How to I2C for many data?
;the part hightlighted in different color are added by myself

call Data_Seq

Data_Seq
movlw LC01ADDR ;ADDRESS LOCATION 12H
movwf FSR ;fSR = 12h
movlw 34h
movwf INDF ;@12h = 34h
incf FSR,F
movlw 44h
movwf INDF ;@13h = 44h
incf FSR,F
movlw 55h ;@14h = 55h
movwf INDF

return

;************************************************* *
I2CWrite
movlw 0x0F
movwf PORTB
call DELAY_V_LONG
call DELAY_V_LONG
clrf PORTB
banksel SSPCON2
bsf SSPCON2,SEN
call WaitMSSP

; Send and Check CONTROL BYTE
movlw LC01CTRLIN ;LOAD CONTROL BYTE FOR input mode
call Send_I2C_Byte ;SEND BYTE
call WaitMSSP ;WAIT FOR I2C OPERATION

BANKSEL SSPCON2
btfsc SSPCON2,ACKSTAT ;CHECK ACK STATUS BIT
goto I2CFail ;FAILED, SKIP IF SUCCEED

;Send and Check ADDRESS BYTE
movlw LC01ADDR ;LOAD THE ADDRESS location
call Send_I2C_Byte ;send byte
call WaitMSSP ;wait for the i2c operation

banksel SSPCON2
btfsc SSPCON2,ACKSTAT ;check ACK status bit
goto I2CFail ;failed, skip if success

;Send and Check Data byte
movlw LC01ADDR ;SENDING 0011-0100
movwf FSR
movlw buff_size
movwf N

I2CWrite_1
movf INDF,W ;Load Data Byte
call Send_I2C_Byte ;Send Byte
call WaitMSSP ;Wait for I2C operation

banksel SSPCON2
btfsc SSPCON2,ACKSTAT ;check ACK Status bit
goto I2CFail ;failed, skip if successful

incf FSR,F
decfsz N,F
goto I2CWrite_1
;Send and Check the STOP condition
banksel SSPCON2
bsf SSPCON2,PEN
call WaitMSSP

Done1
movlw 0xAA ; 1010-1010
movwf PORTB
call DELAY_V_LONG
call DELAY_V_LONG

;The WRITE has now completed successfully

;************************************************* ********
I2CRead
;Send Restart Condition and wait for it to complete
movlw 0x0F
movwf PORTB
call DELAY_V_LONG
call DELAY_V_LONG
clrf PORTB
;ACK
banksel SSPCON2
bsf SSPCON2,RSEN
call WaitMSSP

;Send and Check CONTROL BYTE
movlw LC01CTRLIN
call Send_I2C_Byte
call WaitMSSP

;Now check to see if I2C EEPROM is ready
;ACK
banksel SSPCON2
btfsc SSPCON2,ACKSTAT
goto I2CRead

;Send and Check ADDRESS BYTE
movlw LC01ADDR
call Send_I2C_Byte
call WaitMSSP
;ACK
banksel SSPCON2
btfsc SSPCON2,ACKSTAT
goto I2CFail

;Send REPEATED START condition and wait for it to complete
bsf SSPCON2,RSEN
call WaitMSSP

;Send and Check Control BYTE (OUT)
movlw LC01CTRLOUT
call Send_I2C_Byte
call WaitMSSP
;ACK
banksel SSPCON2
btfsc SSPCON2,ACKSTAT
goto I2CFail

;Switch MSSP module to I2C Receive MODE

I2CRead_1

movlw LC01ADDR ;@12h
movwf FSR ;fsr= 12H
movlw buff_size ;.4
movwf N ; N =.4

i2cread_1a
bsf SSPCON2,RCEN ;IN RECEIVE MODE, WHERE DATA IS STORED AT SSPBUF
call WaitMSSP
banksel SSPBUF
movf SSPBUF,W
movwf PORTB
call DELAY_V_LONG

incf FSR,F
decfsz N,F
goto I2CRead_2
I2CRead_2
;ACK
banksel SSPCON2
btfsc SSPCON2,ACKSTAT
goto I2CFail
CLRF PORTB

goto I2CRead_1
;Send NACK but for Acknowledge Sequence
banksel SSPCON2
bsf SSPCON2,ACKDT
bsf SSPCON2,ACKEN
;once ACK or NACK is sent, ACKEN is automatically cleared

;Send and Check the STOP condition and wait for it
bsf SSPCON2,PEN
call WaitMSSP

;I2C Write and Read have both finished, value is on LEDs
banksel SSPBUF
movf SSPBUF,W
movwf PORTB
goto $


I have edited, where the Red color
call Data_Seq refers to the transfer of the data
@12h = 34h
@13h = 44h
@14h = 55h

THEN WE COME TO THE
;sEND AND CHECK DATA byte,
where I intend to WRITE all three data bytes (34h,44h and 55h) into
the External EEPROM (12h,13h and 14h)

{this is done by the I2CWrite_1}

then the Blue area is on READING them.

Kindly advise.
cheers,
slurp
 

There's no highlighted text in your post.

What are you exactly asking for?
 

hi FvM, i am so glad you took notice of my post.

I have read and have followed the I2C PDF presentaiton by Microchip.

However, the presentation shows I2C in assembly language
(which is the language I want to use) but it only Write/Read a data
(in this case, 34h written into 12h and then read and displayed to PORTB)

The problem occured when I re-edit it to write in more data into the external EEPROM 24LC04B.
but it turns out, the whole code was SUCCESSFULLY SIMULATED by MPLAB IDE but became malfunction when I plug it into the circuitry...no LEDs light up.

I wrote a Loop using INDR and FSR to place in 3 data. (for example into location23h,24h,25h with vaules 33h,44h,55h.)

then after each location, i would light up the LED to indicate it has written.
and also the same when i wrote the looping for READ.

Is there a certain register/ SETTING THAT i need to define, or set?

thanking you all for your great help and time.
PLease advise,
Cheers,
Slurp

---------- Post added at 16:45 ---------- Previous post was at 16:42 ----------

FvM, I will attached the code for you to see...

;the part hightlighted in different color are added by myself

call Data_Seq

Data_Seq
movlw LC01ADDR ;ADDRESS LOCATION 12H
movwf FSR ;fSR = 12h
movlw 34h
movwf INDF ;@12h = 34h
incf FSR,F
movlw 44h
movwf INDF ;@13h = 44h
incf FSR,F
movlw 55h ;@14h = 55h
movwf INDF

return

;************************************************* *
I2CWrite
movlw 0x0F
movwf PORTB
call DELAY_V_LONG
call DELAY_V_LONG
clrf PORTB
banksel SSPCON2
bsf SSPCON2,SEN
call WaitMSSP

; Send and Check CONTROL BYTE
movlw LC01CTRLIN ;LOAD CONTROL BYTE FOR input mode
call Send_I2C_Byte ;SEND BYTE
call WaitMSSP ;WAIT FOR I2C OPERATION

BANKSEL SSPCON2
btfsc SSPCON2,ACKSTAT ;CHECK ACK STATUS BIT
goto I2CFail ;FAILED, SKIP IF SUCCEED

;Send and Check ADDRESS BYTE
movlw LC01ADDR ;LOAD THE ADDRESS location
call Send_I2C_Byte ;send byte
call WaitMSSP ;wait for the i2c operation

banksel SSPCON2
btfsc SSPCON2,ACKSTAT ;check ACK status bit
goto I2CFail ;failed, skip if success

;Send and Check Data byte
movlw LC01ADDR ;SENDING 0011-0100
movwf FSR
movlw buff_size
movwf N

I2CWrite_1
movf INDF,W ;Load Data Byte
call Send_I2C_Byte ;Send Byte
call WaitMSSP ;Wait for I2C operation

banksel SSPCON2
btfsc SSPCON2,ACKSTAT ;check ACK Status bit
goto I2CFail ;failed, skip if successful

incf FSR,F
decfsz N,F
goto I2CWrite_1


Send and Check the STOP condition
banksel SSPCON2
bsf SSPCON2,PEN
call WaitMSSP

Done1
movlw 0xAA ; 1010-1010
movwf PORTB
call DELAY_V_LONG
call DELAY_V_LONG

;The WRITE has now completed successfully

;************************************************* ********
I2CRead
;Send Restart Condition and wait for it to complete
movlw 0x0F
movwf PORTB
call DELAY_V_LONG
call DELAY_V_LONG
clrf PORTB
;ACK
banksel SSPCON2
bsf SSPCON2,RSEN
call WaitMSSP

;Send and Check CONTROL BYTE
movlw LC01CTRLIN
call Send_I2C_Byte
call WaitMSSP

;Now check to see if I2C EEPROM is ready
;ACK
banksel SSPCON2
btfsc SSPCON2,ACKSTAT
goto I2CRead

;Send and Check ADDRESS BYTE
movlw LC01ADDR
call Send_I2C_Byte
call WaitMSSP
;ACK
banksel SSPCON2
btfsc SSPCON2,ACKSTAT
goto I2CFail

;Send REPEATED START condition and wait for it to complete
bsf SSPCON2,RSEN
call WaitMSSP

;Send and Check Control BYTE (OUT)
movlw LC01CTRLOUT
call Send_I2C_Byte
call WaitMSSP
;ACK
banksel SSPCON2
btfsc SSPCON2,ACKSTAT
goto I2CFail

;Switch MSSP module to I2C Receive MODE
I2CRead_1

movlw LC01ADDR ;@12h
movwf FSR ;fsr= 12H
movlw buff_size ;.4
movwf N ; N =.4

i2cread_1a
bsf SSPCON2,RCEN ;IN RECEIVE MODE, WHERE DATA IS STORED AT SSPBUF
call WaitMSSP
banksel SSPBUF
movf SSPBUF,W
movwf PORTB
call DELAY_V_LONG

incf FSR,F
decfsz N,F
goto I2CRead_2
I2CRead_2
;ACK
banksel SSPCON2
btfsc SSPCON2,ACKSTAT
goto I2CFail
CLRF PORTB

goto I2CRead_1


;Send NACK but for Acknowledge Sequence
banksel SSPCON2
bsf SSPCON2,ACKDT
bsf SSPCON2,ACKEN
;once ACK or NACK is sent, ACKEN is automatically cleared

;Send and Check the STOP condition and wait for it
bsf SSPCON2,PEN
call WaitMSSP

;I2C Write and Read have both finished, value is on LEDs
banksel SSPBUF
movf SSPBUF,W
movwf PORTB
goto $
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…