I2C slave recieve 2 bytes at once

Status
Not open for further replies.

arvinfx

Member
Hi , i connect 2 pic (16f877a as master - 16f819 as slave) and can sent one byte only to my slave side correctly...

How can i read the second byte on my slave side? the code is but not work!:

master:
Code:
  I2C1_Start()
  I2C1_Wr(0xD0)                the slave code (16f819)
  I2C1_Wr(lo(send))           the low part of send word
  I2C1_Wr(hi(send))            the high part of send word
  I2C1_Stop()

slave:
Code:
if sspstat.D_A = 1 then   ' data arrived no address
              lo(rxbuffer)= sspbuf       read first sent byte
              hi(rxbuffer) = sspbuf      read second sent byte
               j = SSPBUF                    empty ssbuf 
            return
          else
              j = SSPBUF         if address arrived empty the SSBUF 
               return
          end if
 
You are waiting for a byte to arrive and then immediately reading 2 bytes. Your problem is that the second byte takes time to arrive.

Mike.
 
The D_A flag signals if the data is address or data. The BF flag signals when a byte arrives.

Mike.
 
Great.

This code work correctly

Then pir1.sspif set to 1 the one byte ready to read! i read 2 byte immediately! we must set pointer for string of data then recieveing and add the pointer and save the bytes in my sample is 2 byte only but is clearly to expand the code . good luck and enjoy

Slave code:
Code:
sub procedure interrupt


    if pir1.sspif = 1 then     
         pir1.sspif = 0
          INTCON = %00000000
          if (sspstat.D_A = 1) and (sspstat.bf=1)  then  
             if pointer=0 then
                lo(rxbuffer)=sspbuf
                pointer=1
                goto exit_int
             end if
             
             if pointer=1 then
                hi(rxbuffer)=sspbuf
                pointer=0
               goto exit_int
             end if
            goto exit_int
            return
          end if

    end if
    
  exit_int:
  
 j = SSPBUF
  INTCON = %11000000
 return
end sub
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…